Logo AND Algorithmique Numérique Distribuée

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