Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
188bea131024bd052f1f6e2c700965178cdaf2a0
[simgrid.git] / src / surf / ns3 / ns3_simulator.cc
1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 "surf/ns3/ns3_simulator.h"
8 #include "xbt/dict.h"
9 #include "xbt/log.h"
10 #include "xbt/sysdep.h"
11
12 using namespace ns3;
13 using namespace std;
14
15 xbt_dict_t dict_socket = NULL;
16
17 NS3Sim SimulatorNS3;
18
19 static void receive_callback(Ptr<Socket> localSocket);
20 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace);
21 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent);
22 static void StartFlow(Ptr<Socket> sock,
23     const char *to,
24     uint16_t port_number);
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simulator_ns3, surf,
27                                 "Logging specific to the SURF network NS3 module");
28
29 // Constructor.
30 NS3Sim::NS3Sim(){
31 }
32 //Destructor.
33 NS3Sim::~NS3Sim(){
34 }
35
36 /*
37  * This function create a flow from src to dst
38  *
39  * Parameters
40  *              src: node source
41  *              dst: node destination
42  *              port_number: The port number to use
43  *              start: the time the communication start
44  *              addr:  ip address
45  *              totalBytes: number of bytes to transmit
46  */
47 void NS3Sim::create_flow_NS3(
48                 Ptr<Node> src,
49                 Ptr<Node> dst,
50                 uint16_t port_number,
51                 double start,
52                 const char *addr,
53                 uint32_t totalBytes,
54                 void * action)
55 {
56         if(!dict_socket) dict_socket = xbt_dict_new();
57
58         PacketSinkHelper sink ("ns3::TcpSocketFactory",
59                                                         InetSocketAddress (Ipv4Address::GetAny(),
60                                                         port_number));
61         sink.Install (dst);
62         Ptr<Socket> sock = Socket::CreateSocket (src,
63                                                         TcpSocketFactory::GetTypeId());
64
65         MySocket *mysocket = new MySocket();
66         mysocket->totalBytes = totalBytes;
67         mysocket->remaining = totalBytes;
68         mysocket->bufferedBytes = 0;
69         mysocket->sentBytes = 0;
70         mysocket->finished = 0;
71         mysocket->action = action;
72         xbt_dict_set(dict_socket,(const char*)&sock, mysocket,NULL);
73         sock->Bind(InetSocketAddress(port_number));
74         XBT_INFO("Create flow starting to %fs + %fs = %fs",start-ns3_time(), ns3_time(), start);
75         Simulator::Schedule (Seconds(start-ns3_time()),&StartFlow, sock, addr, port_number);
76 //      Simulator::Schedule (Seconds(0.0),&StartFlow, sock, addr, port_number);
77
78 }
79
80 void* NS3Sim::get_action_from_socket(void *socket){
81         return ((MySocket *)socket)->action;
82 }
83
84 char NS3Sim::get_finished(void *socket){
85         return ((MySocket *)socket)->finished;
86 }
87
88 double NS3Sim::get_remains_from_socket(void *socket){
89         return ((MySocket *)socket)->remaining;
90 }
91
92 double NS3Sim::get_sent_from_socket(void *socket){
93   return ((MySocket *)socket)->sentBytes;
94 }
95
96 void NS3Sim::simulator_start(double min){
97   if(min > 0.0)
98     Simulator::Stop(Seconds(min));
99   XBT_DEBUG("Start simulator");
100   Simulator::Run ();
101 }
102
103 static void receive_callback(Ptr<Socket> localSocket){
104   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
105
106   if (mysocket->finished == 0){
107     mysocket->finished = 1;
108     XBT_DEBUG("recv_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
109     XBT_DEBUG("Stop simulator at %f seconds", Simulator::Now().GetSeconds());
110     Simulator::Stop(Seconds(0.0));
111     Simulator::Run();
112   }
113 }
114
115 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
116         uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*txSpace);
117         MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
118         if (mysocket->remaining == 0){
119                   //all data was already buffered (and socket was already closed), just return
120                   return;
121         }
122         while (mysocket->sentBytes < mysocket->totalBytes
123                         && localSocket->GetTxAvailable () > 0)
124         {
125       uint32_t toWrite = min ((mysocket->remaining), txSpace);
126       toWrite = min (toWrite, localSocket->GetTxAvailable ());
127       int amountSent = localSocket->Send (&data[0], toWrite, 0);
128
129       if(amountSent < 0)
130           return;
131           (mysocket->sentBytes) += amountSent;
132           (mysocket->remaining) -= amountSent;
133       XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", mysocket, mysocket->action, mysocket->totalBytes, mysocket->remaining, mysocket->totalBytes, amountSent);
134
135     }
136         if ((mysocket->sentBytes) >= mysocket->totalBytes){
137                 localSocket->Close();
138         }
139 }
140
141 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
142   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
143   mysocket->sentBytes += dataSent;
144   XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", mysocket, mysocket->action, mysocket->totalBytes, dataSent);
145 }
146
147 static void normalClose_callback(Ptr<Socket> localSocket){
148   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
149   XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
150   receive_callback (localSocket);
151 }
152
153 static void errorClose_callback(Ptr<Socket> localSocket){
154   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
155   XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
156   xbt_die("NS3: a socket was closed anormally");
157 }
158
159 static void succeededConnect_callback(Ptr<Socket> localSocket){
160   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
161   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
162 }
163
164 static void failedConnect_callback(Ptr<Socket> localSocket){
165   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
166   XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
167   xbt_die("NS3: a socket failed to connect");
168 }
169
170 static void StartFlow(Ptr<Socket> sock,
171     const char *to,
172     uint16_t port_number)
173 {
174   InetSocketAddress serverAddr (to, port_number);
175
176   sock->Connect(serverAddr);
177   sock->SetSendCallback (MakeCallback (&send_callback));
178   sock->SetRecvCallback (MakeCallback (&receive_callback));
179   sock->SetDataSentCallback (MakeCallback (&datasent_callback));
180   sock->SetConnectCallback (MakeCallback (&succeededConnect_callback), MakeCallback (&failedConnect_callback));
181   sock->SetCloseCallbacks (MakeCallback (&normalClose_callback), MakeCallback (&errorClose_callback));
182
183   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&sock);
184   XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", mysocket, mysocket->action, mysocket->totalBytes, to, port_number);
185 }