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){
105 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
107 if (mysocket->finished == 0){
108 mysocket->finished = 1;
109 // cout << "[" << Simulator::Now ().GetSeconds() << "] " << "recv_cb of F[" << mysocket->totalBytes << "] " << endl;
110 XBT_DEBUG("Stop simulator at %f seconds", Simulator::Now().GetSeconds());
115 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
116 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
118 if (mysocket->remaining == 0){
119 //all data was already buffered (and socket was already closed), just return
123 uint32_t toWrite = min (mysocket->remaining, txSpace);
124 uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*toWrite);
125 int amountSent = localSocket->Send (&data[0], toWrite, 0);
128 mysocket->bufferedBytes += amountSent;
129 mysocket->remaining -= amountSent;
131 // cout << "[" << Simulator::Now ().GetSeconds() << "] " << "send_cb of F[" << mysocket->totalBytes << "] ("<< mysocket->remaining << " / " << mysocket->totalBytes << ") " << amountSent << " buffered." << endl;
133 if (mysocket->remaining == 0){
134 //everything was buffered to send, tell NS3 to close the socket
135 localSocket->Close();
140 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
141 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
142 mysocket->sentBytes += dataSent;
143 // cout << "[" << Simulator::Now ().GetSeconds() << "] " << "datasent_cb of F[" << mysocket->totalBytes << "] " << dataSent << " sent." << endl;
147 static void StartFlow(Ptr<Socket> sock,
149 uint16_t port_number)
151 InetSocketAddress serverAddr (to, port_number);
153 //cout << "[" << Simulator::Now().GetSeconds() << "] Starting flow to " << to << " using port " << port_number << endl;
155 sock->Connect(serverAddr);
156 sock->SetSendCallback (MakeCallback (&send_callback));
157 sock->SetRecvCallback (MakeCallback (&receive_callback));
158 sock->SetDataSentCallback (MakeCallback (&datasent_callback));