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 xbt_dict_t dict_socket = NULL;
18 static void receive_callback(Ptr<Socket> localSocket);
19 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace);
20 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent);
21 static void StartFlow(Ptr<Socket> sock,
23 uint16_t port_number);
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simulator_ns3, surf,
26 "Logging specific to the SURF network NS3 module");
36 * This function create a flow from src to dst
40 * dst: node destination
41 * port_number: The port number to use
42 * start: the time the communication start
44 * totalBytes: number of bytes to transmit
46 void NS3Sim::create_flow_NS3(
55 if(!dict_socket) dict_socket = xbt_dict_new();
57 PacketSinkHelper sink ("ns3::TcpSocketFactory",
58 InetSocketAddress (Ipv4Address::GetAny(),
61 Ptr<Socket> sock = Socket::CreateSocket (src,
62 TcpSocketFactory::GetTypeId());
64 MySocket *mysocket = new MySocket();
65 mysocket->totalBytes = totalBytes;
66 mysocket->remaining = totalBytes;
67 mysocket->bufferedBytes = 0;
68 mysocket->sentBytes = 0;
69 mysocket->finished = 0;
70 mysocket->action = action;
71 xbt_dict_set(dict_socket,(const char*)&sock, mysocket,NULL);
72 sock->Bind(InetSocketAddress(port_number));
73 Simulator::Schedule (Seconds(start),&StartFlow, sock, addr, port_number);
76 void* NS3Sim::get_action_from_socket(void *socket){
77 return ((MySocket *)socket)->action;
80 char NS3Sim::get_finished(void *socket){
81 return ((MySocket *)socket)->finished;
84 double NS3Sim::get_remains_from_socket(void *socket){
85 return ((MySocket *)socket)->remaining;
88 double NS3Sim::get_sent_from_socket(void *socket){
89 return ((MySocket *)socket)->sentBytes;
92 void NS3Sim::simulator_stop(double min){
94 Simulator::Stop(Seconds(min));
99 void NS3Sim::simulator_start(void){
100 XBT_DEBUG("Start simulator");
104 static void receive_callback(Ptr<Socket> localSocket){
106 localSocket->GetSockName (addr);
107 InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
108 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
109 mysocket->finished = 1;
111 //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Received [" << mysocket->totalBytes << "bytes], from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << endl;
112 std::stringstream sstream;
113 sstream << Simulator::Now ().GetSeconds();
114 std::string s = sstream.str();
115 size_t size = s.size() + 1;
116 char * time_sec = new char[ size ];
117 strncpy( time_sec, s.c_str(), size );
118 XBT_DEBUG("Stop simulator at %s seconds",time_sec);
122 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
123 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
125 if (mysocket->remaining == 0){
126 //all data was already buffered (and socket was already closed), just return
130 uint32_t toWrite = min (mysocket->remaining, txSpace);
131 uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*toWrite);
132 int amountSent = localSocket->Send (&data[0], toWrite, 0);
135 mysocket->bufferedBytes += amountSent;
136 mysocket->remaining -= amountSent;
138 // cout << "[" << Simulator::Now ().GetSeconds() << "] " << "send_cb of F[" << mysocket->totalBytes << "] ("<< mysocket->remaining << " / " << mysocket->totalBytes << ") " << amountSent << " buffered." << endl;
140 if (mysocket->remaining == 0){
141 //everything was buffered to send, tell NS3 to close the socket
142 localSocket->Close();
147 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
148 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
149 mysocket->sentBytes += dataSent;
150 // cout << "[" << Simulator::Now ().GetSeconds() << "] " << "datasent_cb of F[" << mysocket->totalBytes << "] " << dataSent << " sent." << 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));