Logo AND Algorithmique Numérique Distribuée

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