Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[NS3] rewriting receive_callback to mark a flow finished only if it has not done...
[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
11 using namespace ns3;
12 using namespace std;
13
14 xbt_dict_t dict_socket = NULL;
15
16 NS3Sim SimulatorNS3;
17
18 static void receive_callback(Ptr<Socket> localSocket);
19 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace);
20 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent);
21 static void StartFlow(Ptr<Socket> sock,
22     const char *to,
23     uint16_t port_number);
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simulator_ns3, surf,
26                                 "Logging specific to the SURF network NS3 module");
27
28 // Constructor.
29 NS3Sim::NS3Sim(){
30 }
31 //Destructor.
32 NS3Sim::~NS3Sim(){
33 }
34
35 /*
36  * This function create a flow from src to dst
37  *
38  * Parameters
39  *              src: node source
40  *              dst: node destination
41  *              port_number: The port number to use
42  *              start: the time the communication start
43  *              addr:  ip address
44  *              totalBytes: number of bytes to transmit
45  */
46 void NS3Sim::create_flow_NS3(
47                 Ptr<Node> src,
48                 Ptr<Node> dst,
49                 uint16_t port_number,
50                 double start,
51                 const char *addr,
52                 uint32_t totalBytes,
53                 void * action)
54 {
55         if(!dict_socket) dict_socket = xbt_dict_new();
56
57         PacketSinkHelper sink ("ns3::TcpSocketFactory",
58                                                         InetSocketAddress (Ipv4Address::GetAny(),
59                                                         port_number));
60         sink.Install (dst);
61         Ptr<Socket> sock = Socket::CreateSocket (src,
62                                                         TcpSocketFactory::GetTypeId());
63
64         MySocket *mysocket = new MySocket();
65         mysocket->totalBytes = totalBytes;
66         mysocket->remaining = totalBytes;
67         mysocket->bufferedBytes = 0;
68         mysocket->sentBytes = 0;
69         mysocket->finished = 0;
70         mysocket->action = action;
71         xbt_dict_set(dict_socket,(const char*)&sock, mysocket,NULL);
72         sock->Bind(InetSocketAddress(port_number));
73         Simulator::Schedule (Seconds(start),&StartFlow, sock, addr, port_number);
74 }
75
76 void* NS3Sim::get_action_from_socket(void *socket){
77         return ((MySocket *)socket)->action;
78 }
79
80 char NS3Sim::get_finished(void *socket){
81         return ((MySocket *)socket)->finished;
82 }
83
84 double NS3Sim::get_remains_from_socket(void *socket){
85         return ((MySocket *)socket)->remaining;
86 }
87
88 double NS3Sim::get_sent_from_socket(void *socket){
89   return ((MySocket *)socket)->sentBytes;
90 }
91
92 void NS3Sim::simulator_stop(double min){
93         if(min > 0.0)
94                 Simulator::Stop(Seconds(min));
95         else
96                 Simulator::Stop();
97 }
98
99 void NS3Sim::simulator_start(void){
100         XBT_DEBUG("Start simulator");
101         Simulator::Run ();
102 }
103
104 static void receive_callback(Ptr<Socket> localSocket){
105   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
106
107   if (mysocket->finished == 0){
108     mysocket->finished = 1;
109 //    cout << "[" << Simulator::Now ().GetSeconds() << "] " << "recv_cb of F[" << mysocket->totalBytes << "] " << endl;
110     XBT_DEBUG("Stop simulator at %f seconds", Simulator::Now().GetSeconds());
111     Simulator::Stop();
112   }
113 }
114
115 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
116         MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
117
118         if (mysocket->remaining == 0){
119           //all data was already buffered (and socket was already closed), just return
120           return;
121         }
122
123         uint32_t toWrite = min (mysocket->remaining, txSpace);
124         uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*toWrite);
125         int amountSent = localSocket->Send (&data[0], toWrite, 0);
126         free (data);
127         if (amountSent > 0){
128           mysocket->bufferedBytes += amountSent;
129           mysocket->remaining -= amountSent;
130         }
131 //      cout << "[" << Simulator::Now ().GetSeconds() << "] " << "send_cb of F[" << mysocket->totalBytes << "] ("<<  mysocket->remaining << " / " << mysocket->totalBytes << ") " << amountSent << " buffered." << endl;
132
133   if (mysocket->remaining == 0){
134     //everything was buffered to send, tell NS3 to close the socket
135     localSocket->Close();
136   }
137         return;
138 }
139
140 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
141   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
142   mysocket->sentBytes += dataSent;
143 //  cout << "[" << Simulator::Now ().GetSeconds() << "] " << "datasent_cb of F[" << mysocket->totalBytes << "] " << dataSent << " sent." << endl;
144 }
145
146
147 static void StartFlow(Ptr<Socket> sock,
148     const char *to,
149     uint16_t port_number)
150 {
151   InetSocketAddress serverAddr (to, port_number);
152
153   //cout << "[" <<  Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
154
155   sock->Connect(serverAddr);
156   sock->SetSendCallback (MakeCallback (&send_callback));
157   sock->SetRecvCallback (MakeCallback (&receive_callback));
158   sock->SetDataSentCallback (MakeCallback (&datasent_callback));
159 }