Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bd52efcdc9aa28204115f65bedc83c2949ab69f9
[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 static const uint32_t writeSize  = 1024; // limit the amout of data to write
15 uint8_t data[writeSize];
16 xbt_dict_t dict_socket = NULL;
17
18 NS3Sim SimulatorNS3;
19
20 static void receive_callback(Ptr<Socket> localSocket);
21 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace);
22 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent);
23 static void StartFlow(Ptr<Socket> sock,
24     const char *to,
25     uint16_t port_number);
26
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simulator_ns3, surf,
28                                 "Logging specific to the SURF network NS3 module");
29
30 // Constructor.
31 NS3Sim::NS3Sim(){
32 }
33 //Destructor.
34 NS3Sim::~NS3Sim(){
35 }
36
37 /*
38  * This function create a flow from src to dst
39  *
40  * Parameters
41  *              src: node source
42  *              dst: node destination
43  *              port_number: The port number to use
44  *              start: the time the communication start
45  *              addr:  ip address
46  *              totalBytes: number of bytes to transmit
47  */
48 void NS3Sim::create_flow_NS3(
49                 Ptr<Node> src,
50                 Ptr<Node> dst,
51                 uint16_t port_number,
52                 double start,
53                 const char *addr,
54                 uint32_t totalBytes,
55                 void * action)
56 {
57         if(!dict_socket) dict_socket = xbt_dict_new();
58         PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny(), port_number));
59         sink.Install (dst);
60         Ptr<Socket> sock = Socket::CreateSocket (src, TypeId::LookupByName ("ns3::TcpSocketFactory"));
61         MySocket *mysocket = new MySocket();
62         mysocket->totalBytes = totalBytes;
63         mysocket->remaining = totalBytes;
64         mysocket->last_amount_sent = 0;
65         mysocket->bufferedBytes = 0;
66         mysocket->sentBytes = 0;
67         mysocket->finished = 0;
68         mysocket->action = action;
69         xbt_dict_set(dict_socket,(const char*)&sock, mysocket,NULL);
70         sock->Bind(InetSocketAddress(port_number));
71         Simulator::Schedule (Seconds(start),&StartFlow, sock, addr, port_number);
72 }
73
74 void* NS3Sim::get_action_from_socket(void *socket){
75         return ((MySocket *)socket)->action;
76 }
77
78 char NS3Sim::get_finished(void *socket){
79         return ((MySocket *)socket)->finished;
80 }
81
82 double NS3Sim::get_remains_from_socket(void *socket){
83         return ((MySocket *)socket)->remaining;
84 }
85
86 double NS3Sim::get_sent_from_socket(void *socket){
87   return ((MySocket *)socket)->sentBytes;
88 }
89
90 double NS3Sim::get_last_amount_sent_from_socket(void *socket){
91         return ((MySocket *)socket)->last_amount_sent;
92 }
93
94 void NS3Sim::reset_last_amount_sent_from_socket(void *socket){
95         ((MySocket *)socket)->last_amount_sent = 0;
96 }
97
98 void NS3Sim::simulator_stop(double min){
99         if(min > 0.0)
100                 Simulator::Stop(Seconds(min));
101         else
102                 Simulator::Stop();
103 }
104
105 void NS3Sim::simulator_start(void){
106         XBT_DEBUG("Start simulator");
107         Simulator::Run ();
108 }
109
110 static void receive_callback(Ptr<Socket> localSocket){
111   Address addr;
112   localSocket->GetSockName (addr);
113   InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
114   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
115   mysocket->finished = 1;
116
117   //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Received [" << mysocket->totalBytes << "bytes],  from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << endl;
118         std::stringstream sstream;
119                 sstream << Simulator::Now ().GetSeconds();
120                 std::string s = sstream.str();
121                 size_t size = s.size() + 1;
122                 char * time_sec = new char[ size ];
123                 strncpy( time_sec, s.c_str(), size );
124   XBT_DEBUG("Stop simulator at %s seconds",time_sec);
125   Simulator::Stop();
126 }
127
128 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
129
130         Address addr;
131         localSocket->GetSockName (addr);
132         InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
133         MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
134         uint32_t totalBytes = mysocket->totalBytes;
135         while ((mysocket->bufferedBytes) < totalBytes && localSocket->GetTxAvailable () > 0){
136           uint32_t toWrite = min ((mysocket->remaining), writeSize);
137           toWrite = min (toWrite, localSocket->GetTxAvailable ());
138           int amountSent = localSocket->Send (&data[0], toWrite, 0);
139
140           if(amountSent < 0)
141             return;
142
143           (mysocket->last_amount_sent) += amountSent;
144           (mysocket->bufferedBytes) += amountSent;
145           (mysocket->remaining) -= amountSent;
146           //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Send one packet, remaining "<<  mysocket->remaining << " bytes!" << endl;
147         }
148         if ((mysocket->bufferedBytes) >= totalBytes){
149                 localSocket->Close();
150         }
151
152 }
153
154 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
155   Address addr;
156   localSocket->GetSockName (addr);
157   InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
158   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
159   mysocket->sentBytes += dataSent;
160   //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "DATASENT [" << mysocket->totalBytes << "bytes],  from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << " dataSent " << dataSent <<endl;
161 }
162
163
164 static void StartFlow(Ptr<Socket> sock,
165     const char *to,
166     uint16_t port_number)
167 {
168   InetSocketAddress serverAddr (to, port_number);
169
170   //cout << "[" <<  Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
171
172   sock->Connect(serverAddr);
173   sock->SetSendCallback (MakeCallback (&send_callback));
174   sock->SetRecvCallback (MakeCallback (&receive_callback));
175   sock->SetDataSentCallback (MakeCallback (&datasent_callback));
176 }