Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[NS3] unused variable removed
[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->bufferedBytes = 0;
65         mysocket->sentBytes = 0;
66         mysocket->finished = 0;
67         mysocket->action = action;
68         xbt_dict_set(dict_socket,(const char*)&sock, mysocket,NULL);
69         sock->Bind(InetSocketAddress(port_number));
70         Simulator::Schedule (Seconds(start),&StartFlow, sock, addr, port_number);
71 }
72
73 void* NS3Sim::get_action_from_socket(void *socket){
74         return ((MySocket *)socket)->action;
75 }
76
77 char NS3Sim::get_finished(void *socket){
78         return ((MySocket *)socket)->finished;
79 }
80
81 double NS3Sim::get_remains_from_socket(void *socket){
82         return ((MySocket *)socket)->remaining;
83 }
84
85 double NS3Sim::get_sent_from_socket(void *socket){
86   return ((MySocket *)socket)->sentBytes;
87 }
88
89 void NS3Sim::simulator_stop(double min){
90         if(min > 0.0)
91                 Simulator::Stop(Seconds(min));
92         else
93                 Simulator::Stop();
94 }
95
96 void NS3Sim::simulator_start(void){
97         XBT_DEBUG("Start simulator");
98         Simulator::Run ();
99 }
100
101 static void receive_callback(Ptr<Socket> localSocket){
102   Address addr;
103   localSocket->GetSockName (addr);
104   InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
105   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
106   mysocket->finished = 1;
107
108   //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Received [" << mysocket->totalBytes << "bytes],  from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << endl;
109         std::stringstream sstream;
110                 sstream << Simulator::Now ().GetSeconds();
111                 std::string s = sstream.str();
112                 size_t size = s.size() + 1;
113                 char * time_sec = new char[ size ];
114                 strncpy( time_sec, s.c_str(), size );
115   XBT_DEBUG("Stop simulator at %s seconds",time_sec);
116   Simulator::Stop();
117 }
118
119 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
120
121         Address addr;
122         localSocket->GetSockName (addr);
123         InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
124         MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
125         uint32_t totalBytes = mysocket->totalBytes;
126         while ((mysocket->bufferedBytes) < totalBytes && localSocket->GetTxAvailable () > 0){
127           uint32_t toWrite = min ((mysocket->remaining), writeSize);
128           toWrite = min (toWrite, localSocket->GetTxAvailable ());
129           int amountSent = localSocket->Send (&data[0], toWrite, 0);
130
131           if(amountSent < 0)
132             return;
133
134           (mysocket->bufferedBytes) += amountSent;
135           (mysocket->remaining) -= amountSent;
136           //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Send one packet, remaining "<<  mysocket->remaining << " bytes!" << endl;
137         }
138         if ((mysocket->bufferedBytes) >= totalBytes){
139                 localSocket->Close();
140         }
141
142 }
143
144 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
145   Address addr;
146   localSocket->GetSockName (addr);
147   InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
148   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
149   mysocket->sentBytes += dataSent;
150   //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "DATASENT [" << mysocket->totalBytes << "bytes],  from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << " dataSent " << dataSent <<endl;
151 }
152
153
154 static void StartFlow(Ptr<Socket> sock,
155     const char *to,
156     uint16_t port_number)
157 {
158   InetSocketAddress serverAddr (to, port_number);
159
160   //cout << "[" <<  Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
161
162   sock->Connect(serverAddr);
163   sock->SetSendCallback (MakeCallback (&send_callback));
164   sock->SetRecvCallback (MakeCallback (&receive_callback));
165   sock->SetDataSentCallback (MakeCallback (&datasent_callback));
166 }