Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
258c43d6bde78d15636a10d8708a5d73edaca3bc
[simgrid.git] / src / surf / ns3 / ns3_simulator.cc
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf/ns3/ns3_simulator.h"
8 #include "xbt/dict.h"
9 #include "xbt/log.h"
10
11 using namespace ns3;
12 using namespace std;
13
14 static const uint32_t writeSize  = 1024; // limit the amout of data to write
15 uint8_t data[writeSize];
16 xbt_dict_t dict_socket = NULL;
17
18 NS3Sim SimulatorNS3;
19
20 static void receive_callback(Ptr<Socket> localSocket);
21 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace);
22 static void StartFlow(Ptr<Socket> sock,
23     const char *to,
24     uint16_t port_number);
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simulator_ns3, surf,
27                                 "Logging specific to the SURF network NS3 module");
28
29 // Constructor.
30 NS3Sim::NS3Sim(){
31 }
32 //Destructor.
33 NS3Sim::~NS3Sim(){
34 }
35
36 /*
37  * This function create a flow from src to dst
38  *
39  * Parameters
40  *              src: node source
41  *              dst: node destination
42  *              port_number: The port number to use
43  *              start: the time the communication start
44  *              addr:  ip address
45  *              TotalBytes: number of bytes to transmit
46  */
47 void NS3Sim::create_flow_NS3(
48                 Ptr<Node> src,
49                 Ptr<Node> dst,
50                 uint16_t port_number,
51                 double start,
52                 const char *addr,
53                 uint32_t TotalBytes,
54                 void * action)
55 {
56         if(!dict_socket) dict_socket = xbt_dict_new();
57         PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny(), port_number));
58         sink.Install (dst);
59         Ptr<Socket> sock = Socket::CreateSocket (src, TypeId::LookupByName ("ns3::TcpSocketFactory"));
60         MySocket *mysocket = new MySocket();
61         mysocket->TotalBytes = TotalBytes;
62         mysocket->remaining = TotalBytes;
63         mysocket->last_amount_sent = 0;
64         mysocket->sentBytes = 0;
65         mysocket->finished = 0;
66         mysocket->action = action;
67         xbt_dict_set(dict_socket,(const char*)&sock, mysocket,NULL);
68         sock->Bind(InetSocketAddress(port_number));
69         Simulator::Schedule (Seconds(start),&StartFlow, sock, addr, port_number);
70 }
71
72 void* NS3Sim::get_action_from_socket(void *socket){
73         return ((MySocket *)socket)->action;
74 }
75
76 char NS3Sim::get_finished(void *socket){
77         return ((MySocket *)socket)->finished;
78 }
79
80 double NS3Sim::get_remains_from_socket(void *socket){
81         return ((MySocket *)socket)->remaining;
82 }
83
84 double NS3Sim::get_last_amount_sent_from_socket(void *socket){
85         return ((MySocket *)socket)->last_amount_sent;
86 }
87
88 void NS3Sim::reset_last_amount_sent_from_socket(void *socket){
89         ((MySocket *)socket)->last_amount_sent = 0;
90 }
91
92 void NS3Sim::simulator_stop(double min){
93         if(min > 0.0)
94                 Simulator::Stop(Seconds(min));
95         else
96                 Simulator::Stop();
97 }
98
99 void NS3Sim::simulator_start(void){
100         XBT_DEBUG("Start simulator");
101         Simulator::Run ();
102 }
103
104 static void receive_callback(Ptr<Socket> localSocket){
105   Address addr;
106   localSocket->GetSockName (addr);
107   InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
108   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
109   mysocket->finished = 1;
110
111   //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Received [" << mysocket->TotalBytes << "bytes],  from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << endl;
112         std::stringstream sstream;
113                 sstream << Simulator::Now ().GetSeconds();
114                 std::string s = sstream.str();
115                 size_t size = s.size() + 1;
116                 char * time_sec = new char[ size ];
117                 strncpy( time_sec, s.c_str(), size );
118   XBT_DEBUG("Stop simulator at %s seconds",time_sec);
119   Simulator::Stop();
120 }
121
122 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
123
124         Address addr;
125         localSocket->GetSockName (addr);
126         InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
127         MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
128         uint32_t totalBytes = mysocket->TotalBytes;
129         while ((mysocket->sentBytes) < totalBytes && localSocket->GetTxAvailable () > 0){
130           uint32_t toWrite = min ((mysocket->remaining), writeSize);
131           toWrite = min (toWrite, localSocket->GetTxAvailable ());
132           int amountSent = localSocket->Send (&data[0], toWrite, 0);
133
134           if(amountSent < 0)
135             return;
136
137           (mysocket->last_amount_sent) += amountSent;
138           (mysocket->sentBytes) += amountSent;
139           (mysocket->remaining) -= amountSent;
140           //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Send one packet, remaining "<<  mysocket->remaining << " bytes!" << endl;
141         }
142         if ((mysocket->sentBytes) >= totalBytes){
143                 localSocket->Close();
144         }
145
146 }
147
148 static void StartFlow(Ptr<Socket> sock,
149     const char *to,
150     uint16_t port_number)
151 {
152   InetSocketAddress serverAddr (to, port_number);
153
154   //cout << "[" <<  Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
155
156   sock->Connect(serverAddr);
157   sock->SetSendCallback (MakeCallback (&send_callback));
158   sock->SetRecvCallback (MakeCallback (&receive_callback));
159 }