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);
30 static inline void transformSocketPtr (Ptr<Socket> localSocket)
32 std::stringstream sstream;
33 sstream << localSocket ;
34 std::string s = sstream.str();
35 sprintf(socket_key,"%s",s.c_str());
38 static void delete_mysocket(void *p)
40 MySocket *sock = (MySocket *)p;
45 * This function creates a flow from src to dst
49 * dst: node destination
50 * port_number: The port number to use
51 * start: the time the communication start
53 * totalBytes: number of bytes to transmit
55 void NS3Sim::create_flow_NS3(Ptr<Node> src, Ptr<Node> dst, uint16_t port_number,
56 double startTime, const char *ipAddr, uint32_t totalBytes,
57 simgrid::surf::NetworkNS3Action * action)
60 dict_socket = xbt_dict_new_homogeneous(delete_mysocket);
62 PacketSinkHelper sink("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny(), port_number));
65 Ptr<Socket> sock = Socket::CreateSocket (src, TcpSocketFactory::GetTypeId());
67 MySocket *mysocket = new MySocket();
68 mysocket->totalBytes = totalBytes;
69 mysocket->remaining = totalBytes;
70 mysocket->action = action;
72 transformSocketPtr(sock);
73 xbt_dict_set(dict_socket,socket_key, mysocket,NULL);
75 sock->Bind(InetSocketAddress(port_number));
76 XBT_DEBUG("Create flow starting to %fs + %fs = %fs",
77 startTime-ns3::Simulator::Now().GetSeconds(), ns3::Simulator::Now().GetSeconds(), startTime);
79 Simulator::Schedule (Seconds(startTime-ns3::Simulator::Now().GetSeconds()),
80 &StartFlow, sock, ipAddr, port_number);
83 void NS3Sim::simulator_start(double min){
85 Simulator::Stop(Seconds(min));
86 XBT_DEBUG("Start simulator '%f'",min);
90 static MySocket* get_my_socket(Ptr<Socket> localSocket) {
91 transformSocketPtr(localSocket);
92 return (MySocket*)xbt_dict_get_or_null(dict_socket,socket_key);
95 static void receive_callback(Ptr<Socket> localSocket){
96 MySocket* mysocket = get_my_socket(localSocket);
98 if (mysocket->finished == false){
99 mysocket->finished = true;
100 XBT_DEBUG("recv_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
101 XBT_DEBUG("Stop simulator at %f seconds", Simulator::Now().GetSeconds());
102 Simulator::Stop(Seconds(0.0));
107 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
108 MySocket* mysocket = get_my_socket(localSocket);
110 if (mysocket->remaining == 0){
111 // all data was already buffered (and socket was already closed), just return
115 uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*txSpace);
117 while (mysocket->bufferedBytes < mysocket->totalBytes
118 && localSocket->GetTxAvailable () > 0)
120 uint32_t toWrite = min ((mysocket->remaining), txSpace);
121 toWrite = min (toWrite, localSocket->GetTxAvailable ());
122 int amountSent = localSocket->Send (data, toWrite, 0);
126 (mysocket->bufferedBytes) += amountSent;
127 (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);
134 if ((mysocket->bufferedBytes) >= mysocket->totalBytes)
135 localSocket->Close();
138 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
139 MySocket* mysocket = get_my_socket(localSocket);
140 mysocket->sentBytes += dataSent;
141 XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", mysocket, mysocket->action, mysocket->totalBytes, dataSent);
144 static void normalClose_callback(Ptr<Socket> localSocket){
145 MySocket* mysocket = get_my_socket(localSocket);
146 XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
147 receive_callback (localSocket);
150 static void errorClose_callback(Ptr<Socket> localSocket){
151 MySocket* mysocket = get_my_socket(localSocket);
152 XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
153 xbt_die("NS3: a socket was closed anormally");
156 static void succeededConnect_callback(Ptr<Socket> localSocket){
157 MySocket* mysocket = get_my_socket(localSocket);
158 XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
161 static void failedConnect_callback(Ptr<Socket> localSocket){
162 MySocket* mysocket = get_my_socket(localSocket);
163 XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
164 xbt_die("NS3: a socket failed to connect");
167 static void StartFlow(Ptr<Socket> sock, const char *to, uint16_t port_number)
169 InetSocketAddress serverAddr (to, port_number);
171 sock->Connect(serverAddr);
172 sock->SetSendCallback (MakeCallback (&send_callback));
173 sock->SetRecvCallback (MakeCallback (&receive_callback));
174 sock->SetDataSentCallback (MakeCallback (&datasent_callback));
175 sock->SetConnectCallback (MakeCallback (&succeededConnect_callback), MakeCallback (&failedConnect_callback));
176 sock->SetCloseCallbacks (MakeCallback (&normalClose_callback), MakeCallback (&errorClose_callback));
178 MySocket* mysocket = get_my_socket(sock);
179 XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", mysocket, mysocket->action, mysocket->totalBytes, to, port_number);