Logo AND Algorithmique Numérique Distribuée

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