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->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);
74 void* NS3Sim::get_action_from_socket(void *socket){
75 return ((MySocket *)socket)->action;
78 char NS3Sim::get_finished(void *socket){
79 return ((MySocket *)socket)->finished;
82 double NS3Sim::get_remains_from_socket(void *socket){
83 return ((MySocket *)socket)->remaining;
86 double NS3Sim::get_sent_from_socket(void *socket){
87 return ((MySocket *)socket)->sentBytes;
90 void NS3Sim::simulator_stop(double min){
92 Simulator::Stop(Seconds(min));
97 void NS3Sim::simulator_start(void){
98 XBT_DEBUG("Start simulator");
102 static void receive_callback(Ptr<Socket> localSocket){
104 localSocket->GetSockName (addr);
105 InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
106 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
107 mysocket->finished = 1;
109 //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Received [" << mysocket->totalBytes << "bytes], from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << endl;
110 std::stringstream sstream;
111 sstream << Simulator::Now ().GetSeconds();
112 std::string s = sstream.str();
113 size_t size = s.size() + 1;
114 char * time_sec = new char[ size ];
115 strncpy( time_sec, s.c_str(), size );
116 XBT_DEBUG("Stop simulator at %s seconds",time_sec);
120 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
123 localSocket->GetSockName (addr);
124 InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
125 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
126 uint32_t totalBytes = mysocket->totalBytes;
127 while ((mysocket->bufferedBytes) < totalBytes && localSocket->GetTxAvailable () > 0){
128 uint32_t toWrite = min ((mysocket->remaining), writeSize);
129 toWrite = min (toWrite, localSocket->GetTxAvailable ());
130 int amountSent = localSocket->Send (&data[0], toWrite, 0);
135 (mysocket->last_amount_sent) += amountSent;
136 (mysocket->bufferedBytes) += amountSent;
137 (mysocket->remaining) -= amountSent;
138 //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Send one packet, remaining "<< mysocket->remaining << " bytes!" << endl;
140 if ((mysocket->bufferedBytes) >= totalBytes){
141 localSocket->Close();
146 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
148 localSocket->GetSockName (addr);
149 InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
150 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
151 mysocket->sentBytes += dataSent;
152 //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "DATASENT [" << mysocket->totalBytes << "bytes], from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << " dataSent " << dataSent <<endl;
156 static void StartFlow(Ptr<Socket> sock,
158 uint16_t port_number)
160 InetSocketAddress serverAddr (to, port_number);
162 //cout << "[" << Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
164 sock->Connect(serverAddr);
165 sock->SetSendCallback (MakeCallback (&send_callback));
166 sock->SetRecvCallback (MakeCallback (&receive_callback));
167 sock->SetDataSentCallback (MakeCallback (&datasent_callback));