Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
clang-format these files, no change
[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);
17 ; // ns3::sock -> SgFlow
18
19 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
20 static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace);
21 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
22
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
24
25 SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action* action)
26 {
27   totalBytes_ = totalBytes;
28   remaining_  = totalBytes;
29   action_     = action;
30 }
31
32 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
33 {
34   return (SgFlow*)xbt_dict_get_or_null(flowFromSock, transformSocketPtr(socket));
35 }
36
37 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
38 {
39   SgFlow* flow = getFlowFromSocket(socket);
40
41   if (flow->finished_ == false) {
42     flow->finished_ = true;
43     XBT_DEBUG("recv_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
44     XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
45     ns3::Simulator::Stop(ns3::Seconds(0.0));
46     ns3::Simulator::Run();
47   }
48 }
49
50 static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace)
51 {
52   SgFlow* flow = getFlowFromSocket(sock);
53
54   if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
55     return;
56
57   uint8_t* data = (uint8_t*)malloc(sizeof(uint8_t) * txSpace);
58
59   while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable() > 0) {
60
61     uint32_t toWrite = std::min({flow->remaining_, txSpace, sock->GetTxAvailable()});
62     int amountSent   = sock->Send(data, toWrite, 0);
63
64     if (amountSent < 0)
65       return;
66     flow->bufferedBytes_ += amountSent;
67     flow->remaining_ -= amountSent;
68
69     XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", flow, flow->action_, flow->totalBytes_, flow->remaining_,
70               flow->totalBytes_, amountSent);
71   }
72   free(data);
73
74   if ((flow->bufferedBytes_) >= flow->totalBytes_)
75     sock->Close();
76 }
77
78 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent)
79 {
80   SgFlow* flow = getFlowFromSocket(socket);
81   flow->sentBytes_ += dataSent;
82   XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", flow, flow->action_, flow->totalBytes_, dataSent);
83 }
84
85 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket)
86 {
87   SgFlow* flow = getFlowFromSocket(socket);
88   XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
89   receive_callback(socket);
90 }
91
92 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
93 {
94   SgFlow* flow = getFlowFromSocket(socket);
95   XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
96   xbt_die("NS3: a socket was closed anormally");
97 }
98
99 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
100 {
101   SgFlow* flow = getFlowFromSocket(socket);
102   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
103 }
104
105 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
106 {
107   SgFlow* mysocket = getFlowFromSocket(socket);
108   XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action_, mysocket->totalBytes_);
109   xbt_die("NS3: a socket failed to connect");
110 }
111
112 void StartFlow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
113 {
114   ns3::InetSocketAddress serverAddr(to, port_number);
115
116   sock->Connect(serverAddr);
117   sock->SetSendCallback(MakeCallback(&send_callback));
118   sock->SetRecvCallback(MakeCallback(&receive_callback));
119   sock->SetDataSentCallback(MakeCallback(&datasent_callback));
120   sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
121   sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));
122
123   SgFlow* flow = getFlowFromSocket(sock);
124   XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
125 }