Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more C++ around ns3
[simgrid.git] / src / surf / ns3 / ns3_simulator.cpp
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.hpp"
7 #include "xbt/log.h"
8 #include "xbt/sysdep.h"
9
10 #include <algorithm>
11
12 std::map<std::string, SgFlow*> flowFromSock; // ns3::sock -> SgFlow
13
14 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
15 static void datasent_cb(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
18
19 SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action* action)
20 {
21   totalBytes_ = totalBytes;
22   remaining_  = totalBytes;
23   action_     = action;
24 }
25
26 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
27 {
28   auto it = flowFromSock.find(transformSocketPtr(socket));
29   return (it == flowFromSock.end()) ? nullptr : it->second;
30 }
31
32 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
33 {
34   SgFlow* flow = getFlowFromSocket(socket);
35   XBT_DEBUG("received on F[%p, total: %u, remain: %u]", flow, flow->totalBytes_, flow->remaining_);
36
37   if (flow->finished_ == false) {
38     flow->finished_ = true;
39     XBT_DEBUG("recv_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
40     XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
41     ns3::Simulator::Stop(ns3::Seconds(0.0));
42     ns3::Simulator::Run();
43   }
44 }
45
46 static void send_cb(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace)
47 {
48   SgFlow* flow = getFlowFromSocket(sock);
49   XBT_DEBUG("Asked to write on F[%p, total: %u, remain: %u]", flow, flow->totalBytes_, flow->remaining_);
50
51   if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
52     return;
53
54   /* While not all is buffered and there remain space in the buffers */
55   while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable() > 0) {
56
57     uint32_t toWrite = std::min({flow->remaining_, sock->GetTxAvailable()});
58     if (toWrite == 0) { // buffer full
59       XBT_DEBUG("%f: buffer full on flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), flow,
60                 flow->remaining_);
61       return;
62     }
63     int amountSent = sock->Send(0, toWrite, 0);
64
65     xbt_assert(amountSent > 0, "Since TxAvailable>0, amountSent should also >0");
66     flow->bufferedBytes_ += amountSent;
67     flow->remaining_ -= amountSent;
68
69     XBT_DEBUG("%f: sent %d bytes over flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), amountSent, flow,
70               flow->remaining_);
71   }
72
73   if (flow->bufferedBytes_ >= flow->totalBytes_)
74     sock->Close();
75 }
76
77 static void datasent_cb(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent)
78 {
79   /* The tracing wants to know */
80   SgFlow* flow = getFlowFromSocket(socket);
81   flow->sentBytes_ += dataSent;
82   XBT_DEBUG("datasent_cb of F[%p, %p, %u] %u sent (%u total)", flow, flow->action_, flow->totalBytes_, dataSent,
83             flow->sentBytes_);
84 }
85
86 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket)
87 {
88   SgFlow* flow = getFlowFromSocket(socket);
89   XBT_DEBUG("normalClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
90   receive_callback(socket);
91 }
92
93 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
94 {
95   SgFlow* flow = getFlowFromSocket(socket);
96   XBT_DEBUG("errorClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
97   xbt_die("NS3: a socket was closed anormally");
98 }
99
100 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
101 {
102   SgFlow* flow = getFlowFromSocket(socket);
103   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
104 }
105
106 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
107 {
108   SgFlow* mysocket = getFlowFromSocket(socket);
109   XBT_DEBUG("failedConnect_cb of F[%p, %p, %u]", mysocket, mysocket->action_, mysocket->totalBytes_);
110   xbt_die("NS3: a socket failed to connect");
111 }
112
113 void StartFlow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
114 {
115   SgFlow* flow = getFlowFromSocket(sock);
116   ns3::InetSocketAddress serverAddr(to, port_number);
117
118   sock->Connect(serverAddr);
119   // tell the tcp implementation to call send_cb again
120   // if we blocked and new tx buffer space becomes available
121   sock->SetSendCallback(MakeCallback(&send_cb));
122   // Notice when the send is over
123   sock->SetRecvCallback(MakeCallback(&receive_callback));
124   // Notice when we actually sent some data (mostly for the TRACING module)
125   sock->SetDataSentCallback(MakeCallback(&datasent_cb));
126
127   XBT_DEBUG("startFlow of F[%p, %p, %u] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
128
129   sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
130   sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));
131 }