Logo AND Algorithmique Numérique Distribuée

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