Logo AND Algorithmique Numérique Distribuée

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