1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "surf/ns3/ns3_simulator.h"
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;
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,
25 uint16_t port_number);
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simulator_ns3, surf,
28 "Logging specific to the SURF network NS3 module");
38 * This function create a flow from src to dst
42 * dst: node destination
43 * port_number: The port number to use
44 * start: the time the communication start
46 * totalBytes: number of bytes to transmit
48 void NS3Sim::create_flow_NS3(
57 if(!dict_socket) dict_socket = xbt_dict_new();
59 PacketSinkHelper sink ("ns3::TcpSocketFactory",
60 InetSocketAddress (Ipv4Address::GetAny(),
63 Ptr<Socket> sock = Socket::CreateSocket (src,
64 TcpSocketFactory::GetTypeId());
66 MySocket *mysocket = new MySocket();
67 mysocket->totalBytes = totalBytes;
68 mysocket->remaining = totalBytes;
69 mysocket->bufferedBytes = 0;
70 mysocket->sentBytes = 0;
71 mysocket->finished = 0;
72 mysocket->action = action;
73 xbt_dict_set(dict_socket,(const char*)&sock, mysocket,NULL);
74 sock->Bind(InetSocketAddress(port_number));
75 Simulator::Schedule (Seconds(start),&StartFlow, sock, addr, port_number);
78 void* NS3Sim::get_action_from_socket(void *socket){
79 return ((MySocket *)socket)->action;
82 char NS3Sim::get_finished(void *socket){
83 return ((MySocket *)socket)->finished;
86 double NS3Sim::get_remains_from_socket(void *socket){
87 return ((MySocket *)socket)->remaining;
90 double NS3Sim::get_sent_from_socket(void *socket){
91 return ((MySocket *)socket)->sentBytes;
94 void NS3Sim::simulator_stop(double min){
96 Simulator::Stop(Seconds(min));
101 void NS3Sim::simulator_start(void){
102 XBT_DEBUG("Start simulator");
106 static void receive_callback(Ptr<Socket> localSocket){
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;
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);
124 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
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);
139 (mysocket->bufferedBytes) += amountSent;
140 (mysocket->remaining) -= amountSent;
141 //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Send one packet, remaining "<< mysocket->remaining << " bytes!" << endl;
143 if ((mysocket->bufferedBytes) >= totalBytes){
144 localSocket->Close();
149 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
151 localSocket->GetSockName (addr);
152 InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
153 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
154 mysocket->sentBytes += dataSent;
155 //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "DATASENT [" << mysocket->totalBytes << "bytes], from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << " dataSent " << dataSent <<endl;
159 static void StartFlow(Ptr<Socket> sock,
161 uint16_t port_number)
163 InetSocketAddress serverAddr (to, port_number);
165 //cout << "[" << Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
167 sock->Connect(serverAddr);
168 sock->SetSendCallback (MakeCallback (&send_callback));
169 sock->SetRecvCallback (MakeCallback (&receive_callback));
170 sock->SetDataSentCallback (MakeCallback (&datasent_callback));