Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix NS3 builds
[simgrid.git] / src / surf / ns3 / ns3_simulator.cc
1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/surf/ns3/ns3_simulator.h"
7 #include "xbt/dict.h"
8 #include "xbt/log.h"
9 #include "xbt/sysdep.h"
10
11 #include <algorithm>
12
13 xbt_dict_t flowFromSock = xbt_dict_new_homogeneous([](void *p) {
14   delete (SgFlow*)p;
15 }); // ns3::sock -> SgFlow
16
17 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
18 static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace);
19 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
22
23 SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action* action)
24 {
25   totalBytes_ = totalBytes;
26   remaining_  = totalBytes;
27   action_     = action;
28 }
29
30 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
31 {
32   return (SgFlow*)xbt_dict_get_or_null(flowFromSock, transformSocketPtr(socket));
33 }
34
35 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
36 {
37   SgFlow* flow = getFlowFromSocket(socket);
38
39   if (flow->finished_ == false) {
40     flow->finished_ = true;
41     XBT_DEBUG("recv_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
42     XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
43     ns3::Simulator::Stop(ns3::Seconds(0.0));
44     ns3::Simulator::Run();
45   }
46 }
47
48 static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace)
49 {
50   SgFlow* flow = getFlowFromSocket(sock);
51
52   if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
53     return;
54
55   uint8_t* data = (uint8_t*)malloc(sizeof(uint8_t) * txSpace);
56
57   while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable() > 0) {
58
59     uint32_t toWrite = std::min({flow->remaining_, txSpace, sock->GetTxAvailable()});
60     int amountSent   = sock->Send(data, toWrite, 0);
61
62     if (amountSent < 0)
63       return;
64     flow->bufferedBytes_ += amountSent;
65     flow->remaining_ -= amountSent;
66
67     XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", flow, flow->action_, flow->totalBytes_, flow->remaining_,
68               flow->totalBytes_, amountSent);
69   }
70   free(data);
71
72   if ((flow->bufferedBytes_) >= flow->totalBytes_)
73     sock->Close();
74 }
75
76 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent)
77 {
78   SgFlow* flow = getFlowFromSocket(socket);
79   flow->sentBytes_ += dataSent;
80   XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", flow, flow->action_, flow->totalBytes_, dataSent);
81 }
82
83 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket)
84 {
85   SgFlow* flow = getFlowFromSocket(socket);
86   XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
87   receive_callback(socket);
88 }
89
90 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
91 {
92   SgFlow* flow = getFlowFromSocket(socket);
93   XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
94   xbt_die("NS3: a socket was closed anormally");
95 }
96
97 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
98 {
99   SgFlow* flow = getFlowFromSocket(socket);
100   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
101 }
102
103 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
104 {
105   SgFlow* mysocket = getFlowFromSocket(socket);
106   XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action_, mysocket->totalBytes_);
107   xbt_die("NS3: a socket failed to connect");
108 }
109
110 void StartFlow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
111 {
112   ns3::InetSocketAddress serverAddr(to, port_number);
113
114   sock->Connect(serverAddr);
115   sock->SetSendCallback(MakeCallback(&send_callback));
116   sock->SetRecvCallback(MakeCallback(&receive_callback));
117   sock->SetDataSentCallback(MakeCallback(&datasent_callback));
118   sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
119   sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));
120
121   SgFlow* flow = getFlowFromSocket(sock);
122   XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
123 }