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"
10 #include "xbt/sysdep.h"
15 xbt_dict_t dict_socket = NULL;
19 static void receive_callback(Ptr<Socket> localSocket);
20 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace);
21 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent);
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();
58 PacketSinkHelper sink ("ns3::TcpSocketFactory",
59 InetSocketAddress (Ipv4Address::GetAny(),
62 Ptr<Socket> sock = Socket::CreateSocket (src,
63 TcpSocketFactory::GetTypeId());
65 MySocket *mysocket = new MySocket();
66 mysocket->totalBytes = totalBytes;
67 mysocket->remaining = totalBytes;
68 mysocket->bufferedBytes = 0;
69 mysocket->sentBytes = 0;
70 mysocket->finished = 0;
71 mysocket->action = action;
72 xbt_dict_set(dict_socket,(const char*)&sock, mysocket,NULL);
73 sock->Bind(InetSocketAddress(port_number));
74 Simulator::Schedule (Seconds(0.0),&StartFlow, sock, addr, port_number);
77 void* NS3Sim::get_action_from_socket(void *socket){
78 return ((MySocket *)socket)->action;
81 char NS3Sim::get_finished(void *socket){
82 return ((MySocket *)socket)->finished;
85 double NS3Sim::get_remains_from_socket(void *socket){
86 return ((MySocket *)socket)->remaining;
89 double NS3Sim::get_sent_from_socket(void *socket){
90 return ((MySocket *)socket)->sentBytes;
93 void NS3Sim::simulator_start(double min){
95 Simulator::Stop(Seconds(min));
96 XBT_DEBUG("Start simulator");
100 static void receive_callback(Ptr<Socket> localSocket){
101 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
103 if (mysocket->finished == 0){
104 mysocket->finished = 1;
105 XBT_DEBUG("recv_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
106 XBT_DEBUG("Stop simulator at %f seconds", Simulator::Now().GetSeconds());
107 Simulator::Stop(Seconds(0.0));
112 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
113 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
115 if (mysocket->remaining == 0){
116 //all data was already buffered (and socket was already closed), just return
120 uint32_t toWrite = min (mysocket->remaining, txSpace);
121 uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*toWrite);
122 int amountSent = localSocket->Send (&data[0], toWrite, 0);
125 mysocket->bufferedBytes += amountSent;
126 mysocket->remaining -= amountSent;
128 XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", mysocket, mysocket->action, mysocket->totalBytes, mysocket->remaining, mysocket->totalBytes, amountSent);
130 if (mysocket->remaining == 0){
131 //everything was buffered to send, tell NS3 to close the socket
132 localSocket->Close();
137 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
138 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
139 mysocket->sentBytes += dataSent;
140 XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", mysocket, mysocket->action, mysocket->totalBytes, dataSent);
143 static void normalClose_callback(Ptr<Socket> localSocket){
144 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
145 XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
146 receive_callback (localSocket);
149 static void errorClose_callback(Ptr<Socket> localSocket){
150 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
151 XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
152 xbt_die("NS3: a socket was closed anormally");
155 static void succeededConnect_callback(Ptr<Socket> localSocket){
156 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
157 XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
160 static void failedConnect_callback(Ptr<Socket> localSocket){
161 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
162 XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
163 xbt_die("NS3: a socket failed to connect");
166 static void StartFlow(Ptr<Socket> sock,
168 uint16_t port_number)
170 InetSocketAddress serverAddr (to, port_number);
172 sock->Connect(serverAddr);
173 sock->SetSendCallback (MakeCallback (&send_callback));
174 sock->SetRecvCallback (MakeCallback (&receive_callback));
175 sock->SetDataSentCallback (MakeCallback (&datasent_callback));
176 sock->SetConnectCallback (MakeCallback (&succeededConnect_callback), MakeCallback (&failedConnect_callback));
177 sock->SetCloseCallbacks (MakeCallback (&normalClose_callback), MakeCallback (&errorClose_callback));
179 MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&sock);
180 XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", mysocket, mysocket->action, mysocket->totalBytes, to, port_number);