Logo AND Algorithmique Numérique Distribuée

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