Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove a few smells in instrumentation
[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
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
23
24 SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action * action) {
25   totalBytes_ = totalBytes;
26   remaining_ = totalBytes;
27   action_ = action;
28 }
29
30 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket) {
31         return (SgFlow*)xbt_dict_get_or_null(flowFromSock, transformSocketPtr(socket));
32 }
33
34 static void receive_callback(ns3::Ptr<ns3::Socket> socket){
35   SgFlow* flow = getFlowFromSocket(socket);
36
37   if (flow->finished_ == false){
38     flow->finished_ = true;
39     XBT_DEBUG("recv_cb of F[%p, %p, %d]", 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_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace){
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         uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*txSpace);
53
54         while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable () > 0) {
55
56       uint32_t toWrite = std::min ({flow->remaining_, txSpace, sock->GetTxAvailable ()});
57       int amountSent = sock->Send (data, toWrite, 0);
58
59       if(amountSent < 0)
60           return;
61       flow->bufferedBytes_ += amountSent;
62       flow->remaining_ -= amountSent;
63
64       XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", flow, flow->action_, flow->totalBytes_,
65           flow->remaining_, flow->totalBytes_, amountSent);
66     }
67         free(data);
68
69         if ((flow->bufferedBytes_) >= flow->totalBytes_)
70                 sock->Close();
71 }
72
73 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent){
74   SgFlow* flow = getFlowFromSocket(socket);
75   flow->sentBytes_ += dataSent;
76   XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", flow, flow->action_, flow->totalBytes_, dataSent);
77 }
78
79 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket){
80   SgFlow* flow = getFlowFromSocket(socket);
81   XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
82   receive_callback (socket);
83 }
84
85 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket){
86   SgFlow* flow = getFlowFromSocket(socket);
87   XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
88   xbt_die("NS3: a socket was closed anormally");
89 }
90
91 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket){
92   SgFlow* flow = getFlowFromSocket(socket);
93   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
94 }
95
96 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket){
97   SgFlow* mysocket = getFlowFromSocket(socket);
98   XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action_, mysocket->totalBytes_);
99   xbt_die("NS3: a socket failed to connect");
100 }
101
102 void StartFlow(ns3::Ptr<ns3::Socket> sock, const char *to, uint16_t port_number)
103 {
104   ns3::InetSocketAddress serverAddr (to, port_number);
105
106   sock->Connect(serverAddr);
107   sock->SetSendCallback (MakeCallback (&send_callback));
108   sock->SetRecvCallback (MakeCallback (&receive_callback));
109   sock->SetDataSentCallback (MakeCallback (&datasent_callback));
110   sock->SetConnectCallback (MakeCallback (&succeededConnect_callback), MakeCallback (&failedConnect_callback));
111   sock->SetCloseCallbacks (MakeCallback (&normalClose_callback), MakeCallback (&errorClose_callback));
112
113   SgFlow* flow = getFlowFromSocket(sock);
114   XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
115 }