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->last_amount_sent = 0;
64 mysocket->sentBytes = 0;
65 mysocket->finished = 0;
66 mysocket->action = action;
67 xbt_dict_set(dict_socket,(const char*)&sock, mysocket,NULL);
68 sock->Bind(InetSocketAddress(port_number));
69 Simulator::Schedule (Seconds(start),&StartFlow, sock, addr, port_number);
72 void* NS3Sim::get_action_from_socket(void *socket){
73 return ((MySocket *)socket)->action;
76 char NS3Sim::get_finished(void *socket){
77 return ((MySocket *)socket)->finished;
80 double NS3Sim::get_remains_from_socket(void *socket){
81 return ((MySocket *)socket)->remaining;
84 double NS3Sim::get_last_amount_sent_from_socket(void *socket){
85 return ((MySocket *)socket)->last_amount_sent;
88 void NS3Sim::reset_last_amount_sent_from_socket(void *socket){
89 ((MySocket *)socket)->last_amount_sent = 0;
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){
125 localSocket->GetSockName (addr);
126 InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
127 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
128 uint32_t totalBytes = mysocket->TotalBytes;
129 while ((mysocket->sentBytes) < totalBytes && localSocket->GetTxAvailable () > 0){
130 uint32_t toWrite = min ((mysocket->remaining), writeSize);
131 toWrite = min (toWrite, localSocket->GetTxAvailable ());
132 int amountSent = localSocket->Send (&data[0], toWrite, 0);
137 (mysocket->last_amount_sent) += amountSent;
138 (mysocket->sentBytes) += amountSent;
139 (mysocket->remaining) -= amountSent;
140 //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Send one packet, remaining "<< mysocket->remaining << " bytes!" << endl;
142 if ((mysocket->sentBytes) >= totalBytes){
143 localSocket->Close();
148 static void StartFlow(Ptr<Socket> sock,
150 uint16_t port_number)
152 InetSocketAddress serverAddr (to, port_number);
154 //cout << "[" << Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
156 sock->Connect(serverAddr);
157 sock->SetSendCallback (MakeCallback (&send_callback));
158 sock->SetRecvCallback (MakeCallback (&receive_callback));