Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix some memleack for ns3
[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 #include "xbt/sysdep.h"
11
12 using namespace ns3;
13 using namespace std;
14
15 xbt_dict_t dict_socket = NULL;
16
17 NS3Sim SimulatorNS3;
18 static char socket_key[24];
19
20 static void receive_callback(Ptr<Socket> localSocket);
21 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace);
22 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent);
23 static void StartFlow(Ptr<Socket> sock,
24     const char *to,
25     uint16_t port_number);
26
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simulator_ns3, surf,
28                                 "Logging specific to the SURF network NS3 module");
29
30 // Constructor.
31 NS3Sim::NS3Sim(){
32 }
33 //Destructor.
34 NS3Sim::~NS3Sim(){
35 }
36
37 static XBT_INLINE void transformSocketPtr (Ptr<Socket> localSocket){
38   std::stringstream sstream;
39   sstream << localSocket ;
40   std::string s = sstream.str();
41   sprintf(socket_key,"%s",s.c_str());
42 }
43
44 static void delete_mysocket(void *p)
45 {
46   MySocket *sock = (MySocket *)p;
47   delete(sock);
48 }
49
50 /*
51  * This function create a flow from src to dst
52  *
53  * Parameters
54  *              src: node source
55  *              dst: node destination
56  *              port_number: The port number to use
57  *              start: the time the communication start
58  *              addr:  ip address
59  *              totalBytes: number of bytes to transmit
60  */
61 void NS3Sim::create_flow_NS3(
62                 Ptr<Node> src,
63                 Ptr<Node> dst,
64                 uint16_t port_number,
65                 double start,
66                 const char *addr,
67                 uint32_t totalBytes,
68                 void * action)
69 {
70         if(!dict_socket) dict_socket = xbt_dict_new_homogeneous(delete_mysocket);
71
72         PacketSinkHelper sink ("ns3::TcpSocketFactory",
73                                                         InetSocketAddress (Ipv4Address::GetAny(),
74                                                         port_number));
75         sink.Install (dst);
76
77         Ptr<Socket> sock = Socket::CreateSocket (src,
78                                                         TcpSocketFactory::GetTypeId());
79
80         MySocket *mysocket = new MySocket();
81         mysocket->totalBytes = totalBytes;
82         mysocket->remaining = totalBytes;
83         mysocket->bufferedBytes = 0;
84         mysocket->sentBytes = 0;
85         mysocket->finished = 0;
86         mysocket->action = action;
87
88         transformSocketPtr(sock);
89         xbt_dict_set(dict_socket,socket_key, mysocket,NULL);
90
91         sock->Bind(InetSocketAddress(port_number));
92         XBT_DEBUG("Create flow starting to %fs + %fs = %fs",start-ns3_time(), ns3_time(), start);
93
94         Simulator::Schedule (Seconds(start-ns3_time()),&StartFlow, sock, addr, port_number);
95 //      Simulator::Schedule (Seconds(0.0),&StartFlow, sock, addr, port_number);
96
97 }
98
99 void* NS3Sim::get_action_from_socket(void *socket){
100         return ((MySocket *)socket)->action;
101 }
102
103 char NS3Sim::get_finished(void *socket){
104         return ((MySocket *)socket)->finished;
105 }
106
107 double NS3Sim::get_remains_from_socket(void *socket){
108         return ((MySocket *)socket)->remaining;
109 }
110
111 double NS3Sim::get_sent_from_socket(void *socket){
112   return ((MySocket *)socket)->sentBytes;
113 }
114
115 void NS3Sim::simulator_start(double min){
116   if(min > 0.0)
117     Simulator::Stop(Seconds(min));
118   XBT_DEBUG("Start simulator '%f'",min);
119   Simulator::Run ();
120 }
121
122 static MySocket* get_my_socket(Ptr<Socket> localSocket) {
123         transformSocketPtr(localSocket);
124         return (MySocket*)xbt_dict_get_or_null(dict_socket,socket_key);
125 }
126
127 static void receive_callback(Ptr<Socket> localSocket){
128   MySocket* mysocket = get_my_socket(localSocket);
129
130   if (mysocket->finished == 0){
131     mysocket->finished = 1;
132     XBT_DEBUG("recv_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
133     XBT_DEBUG("Stop simulator at %f seconds", Simulator::Now().GetSeconds());
134     Simulator::Stop(Seconds(0.0));
135     Simulator::Run();
136   }
137 }
138
139 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
140         MySocket* mysocket = get_my_socket(localSocket);
141
142         if (mysocket->remaining == 0){
143                   //all data was already buffered (and socket was already closed), just return
144                   return;
145         }
146
147         uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*txSpace);
148
149         while (mysocket->bufferedBytes < mysocket->totalBytes
150                         && localSocket->GetTxAvailable () > 0)
151         {
152       uint32_t toWrite = min ((mysocket->remaining), txSpace);
153       toWrite = min (toWrite, localSocket->GetTxAvailable ());
154       int amountSent = localSocket->Send (data, toWrite, 0);
155
156       if(amountSent < 0)
157           return;
158       (mysocket->bufferedBytes) += amountSent;
159       (mysocket->remaining) -= amountSent;
160       XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", mysocket, mysocket->action, mysocket->totalBytes, mysocket->remaining, mysocket->totalBytes, amountSent);
161
162     }
163
164         free(data);
165
166         if ((mysocket->bufferedBytes) >= mysocket->totalBytes){
167                 localSocket->Close();
168         }
169 }
170
171 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
172   MySocket* mysocket = get_my_socket(localSocket);
173   mysocket->sentBytes += dataSent;
174   XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", mysocket, mysocket->action, mysocket->totalBytes, dataSent);
175 }
176
177 static void normalClose_callback(Ptr<Socket> localSocket){
178   MySocket* mysocket = get_my_socket(localSocket);
179   XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
180   receive_callback (localSocket);
181 }
182
183 static void errorClose_callback(Ptr<Socket> localSocket){
184   MySocket* mysocket = get_my_socket(localSocket);
185   XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
186   xbt_die("NS3: a socket was closed anormally");
187 }
188
189 static void succeededConnect_callback(Ptr<Socket> localSocket){
190   MySocket* mysocket = get_my_socket(localSocket);
191   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
192 }
193
194 static void failedConnect_callback(Ptr<Socket> localSocket){
195   MySocket* mysocket = get_my_socket(localSocket);
196   XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
197   xbt_die("NS3: a socket failed to connect");
198 }
199
200 static void StartFlow(Ptr<Socket> sock,
201     const char *to,
202     uint16_t port_number)
203 {
204   InetSocketAddress serverAddr (to, port_number);
205
206   sock->Connect(serverAddr);
207   sock->SetSendCallback (MakeCallback (&send_callback));
208   sock->SetRecvCallback (MakeCallback (&receive_callback));
209   sock->SetDataSentCallback (MakeCallback (&datasent_callback));
210   sock->SetConnectCallback (MakeCallback (&succeededConnect_callback), MakeCallback (&failedConnect_callback));
211   sock->SetCloseCallbacks (MakeCallback (&normalClose_callback), MakeCallback (&errorClose_callback));
212
213   MySocket* mysocket = get_my_socket(sock);
214   XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", mysocket, mysocket->action, mysocket->totalBytes, to, port_number);
215 }