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();
58 PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny(), port_number));
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);
73 void* NS3Sim::get_action_from_socket(void *socket){
74 return ((MySocket *)socket)->action;
77 char NS3Sim::get_finished(void *socket){
78 return ((MySocket *)socket)->finished;
81 double NS3Sim::get_remains_from_socket(void *socket){
82 return ((MySocket *)socket)->remaining;
85 double NS3Sim::get_sent_from_socket(void *socket){
86 return ((MySocket *)socket)->sentBytes;
89 void NS3Sim::simulator_stop(double min){
91 Simulator::Stop(Seconds(min));
96 void NS3Sim::simulator_start(void){
97 XBT_DEBUG("Start simulator");
101 static void receive_callback(Ptr<Socket> localSocket){
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;
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);
119 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
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);
134 (mysocket->bufferedBytes) += amountSent;
135 (mysocket->remaining) -= amountSent;
136 //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Send one packet, remaining "<< mysocket->remaining << " bytes!" << endl;
138 if ((mysocket->bufferedBytes) >= totalBytes){
139 localSocket->Close();
144 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
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;
154 static void StartFlow(Ptr<Socket> sock,
156 uint16_t port_number)
158 InetSocketAddress serverAddr (to, port_number);
160 //cout << "[" << Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
162 sock->Connect(serverAddr);
163 sock->SetSendCallback (MakeCallback (&send_callback));
164 sock->SetRecvCallback (MakeCallback (&receive_callback));
165 sock->SetDataSentCallback (MakeCallback (&datasent_callback));