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 StartFlow(Ptr<Socket> sock,
24 uint16_t port_number);
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simulator_ns3, surf,
27 "Logging specific to the SURF network NS3 module");
37 * This function create a flow from src to dst
41 * dst: node destination
42 * port_number: The port number to use
43 * start: the time the communication start
45 * TotalBytes: number of bytes to transmit
47 void NS3Sim::create_flow_NS3(
56 if(!dict_socket) dict_socket = xbt_dict_new();
57 PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny(), port_number));
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);
71 void* NS3Sim::get_action_from_socket(void *socket){
72 return ((MySocket *)socket)->action;
75 char NS3Sim::get_finished(void *socket){
76 return ((MySocket *)socket)->finished;
79 double NS3Sim::get_remains_from_socket(void *socket){
80 return ((MySocket *)socket)->remaining;
83 void NS3Sim::simulator_stop(double min){
85 Simulator::Stop(Seconds(min));
90 void NS3Sim::simulator_start(void){
91 XBT_DEBUG("Start simulator");
95 static void receive_callback(Ptr<Socket> localSocket){
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;
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);
113 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
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);
127 (mysocket->sentBytes) += amountSent;
128 (mysocket->remaining) -= amountSent;
129 //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Send one packet, remaining "<< mysocket->remaining << " bytes!" << endl;
131 if ((mysocket->sentBytes) >= totalBytes){
132 localSocket->Close();
137 static void StartFlow(Ptr<Socket> sock,
139 uint16_t port_number)
141 InetSocketAddress serverAddr (to, port_number);
143 //cout << "[" << Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
145 sock->Connect(serverAddr);
146 sock->SetSendCallback (MakeCallback (&send_callback));
147 sock->SetRecvCallback (MakeCallback (&receive_callback));