1 /* Copyright (c) 2007-2014. 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 "src/surf/ns3/ns3_simulator.h"
10 #include "xbt/sysdep.h"
15 xbt_dict_t dict_socket = NULL;
18 static char socket_key[24];
20 static void receive_callback(Ptr<Socket> localSocket);
21 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace);
22 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent);
23 static void StartFlow(Ptr<Socket> sock, const char *to, uint16_t port_number);
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
32 static inline void transformSocketPtr (Ptr<Socket> localSocket)
34 std::stringstream sstream;
35 sstream << localSocket ;
36 std::string s = sstream.str();
37 sprintf(socket_key,"%s",s.c_str());
40 static void delete_mysocket(void *p)
42 MySocket *sock = (MySocket *)p;
47 * This function creates a flow from src to dst
51 * dst: node destination
52 * port_number: The port number to use
53 * start: the time the communication start
55 * totalBytes: number of bytes to transmit
57 void NS3Sim::create_flow_NS3(
64 simgrid::surf::NetworkNS3Action * action)
67 dict_socket = xbt_dict_new_homogeneous(delete_mysocket);
69 PacketSinkHelper sink ("ns3::TcpSocketFactory",
70 InetSocketAddress (Ipv4Address::GetAny(),
74 Ptr<Socket> sock = Socket::CreateSocket (src, TcpSocketFactory::GetTypeId());
76 MySocket *mysocket = new MySocket();
77 mysocket->totalBytes = totalBytes;
78 mysocket->remaining = totalBytes;
79 mysocket->action = action;
81 transformSocketPtr(sock);
82 xbt_dict_set(dict_socket,socket_key, mysocket,NULL);
84 sock->Bind(InetSocketAddress(port_number));
85 XBT_DEBUG("Create flow starting to %fs + %fs = %fs",start-ns3::Simulator::Now().GetSeconds(), ns3::Simulator::Now().GetSeconds(), start);
87 Simulator::Schedule (Seconds(start-ns3::Simulator::Now().GetSeconds()),&StartFlow, sock, addr, port_number);
90 void NS3Sim::simulator_start(double min){
92 Simulator::Stop(Seconds(min));
93 XBT_DEBUG("Start simulator '%f'",min);
97 static MySocket* get_my_socket(Ptr<Socket> localSocket) {
98 transformSocketPtr(localSocket);
99 return (MySocket*)xbt_dict_get_or_null(dict_socket,socket_key);
102 static void receive_callback(Ptr<Socket> localSocket){
103 MySocket* mysocket = get_my_socket(localSocket);
105 if (mysocket->finished == false){
106 mysocket->finished = true;
107 XBT_DEBUG("recv_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
108 XBT_DEBUG("Stop simulator at %f seconds", Simulator::Now().GetSeconds());
109 Simulator::Stop(Seconds(0.0));
114 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
115 MySocket* mysocket = get_my_socket(localSocket);
117 if (mysocket->remaining == 0){
118 // all data was already buffered (and socket was already closed), just return
122 uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*txSpace);
124 while (mysocket->bufferedBytes < mysocket->totalBytes
125 && localSocket->GetTxAvailable () > 0)
127 uint32_t toWrite = min ((mysocket->remaining), txSpace);
128 toWrite = min (toWrite, localSocket->GetTxAvailable ());
129 int amountSent = localSocket->Send (data, toWrite, 0);
133 (mysocket->bufferedBytes) += amountSent;
134 (mysocket->remaining) -= amountSent;
135 XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", mysocket, mysocket->action, mysocket->totalBytes, mysocket->remaining, mysocket->totalBytes, amountSent);
141 if ((mysocket->bufferedBytes) >= mysocket->totalBytes)
142 localSocket->Close();
145 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
146 MySocket* mysocket = get_my_socket(localSocket);
147 mysocket->sentBytes += dataSent;
148 XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", mysocket, mysocket->action, mysocket->totalBytes, dataSent);
151 static void normalClose_callback(Ptr<Socket> localSocket){
152 MySocket* mysocket = get_my_socket(localSocket);
153 XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
154 receive_callback (localSocket);
157 static void errorClose_callback(Ptr<Socket> localSocket){
158 MySocket* mysocket = get_my_socket(localSocket);
159 XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
160 xbt_die("NS3: a socket was closed anormally");
163 static void succeededConnect_callback(Ptr<Socket> localSocket){
164 MySocket* mysocket = get_my_socket(localSocket);
165 XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
168 static void failedConnect_callback(Ptr<Socket> localSocket){
169 MySocket* mysocket = get_my_socket(localSocket);
170 XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
171 xbt_die("NS3: a socket failed to connect");
174 static void StartFlow(Ptr<Socket> sock, const char *to, uint16_t port_number)
176 InetSocketAddress serverAddr (to, port_number);
178 sock->Connect(serverAddr);
179 sock->SetSendCallback (MakeCallback (&send_callback));
180 sock->SetRecvCallback (MakeCallback (&receive_callback));
181 sock->SetDataSentCallback (MakeCallback (&datasent_callback));
182 sock->SetConnectCallback (MakeCallback (&succeededConnect_callback), MakeCallback (&failedConnect_callback));
183 sock->SetCloseCallbacks (MakeCallback (&normalClose_callback), MakeCallback (&errorClose_callback));
185 MySocket* mysocket = get_my_socket(sock);
186 XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", mysocket, mysocket->action, mysocket->totalBytes, to, port_number);