Logo AND Algorithmique Numérique Distribuée

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