Logo AND Algorithmique Numérique Distribuée

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