Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ns3: further cleanups
authorMartin Quinson <martin.quinson@loria.fr>
Mon, 21 Mar 2016 00:25:50 +0000 (01:25 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Mon, 21 Mar 2016 00:44:16 +0000 (01:44 +0100)
src/surf/network_ns3.cpp
src/surf/ns3/ns3_simulator.cc
src/surf/ns3/ns3_simulator.h

index 871a39c..cd4e51b 100644 (file)
@@ -29,7 +29,7 @@ static double time_to_next_flow_completion = -1;
  * Crude globals *
  *****************/
 
-extern xbt_dict_t dict_socket;
+extern xbt_dict_t flowFromSock;
 
 static ns3::InternetStackHelper stack;
 static ns3::NodeContainer nodes;
@@ -286,7 +286,7 @@ NetworkNS3Model::NetworkNS3Model() : NetworkModel() {
 NetworkNS3Model::~NetworkNS3Model() {
   delete ns3_sim;
   xbt_dynar_free_container(&IPV4addr);
-  xbt_dict_free(&dict_socket);
+  xbt_dict_free(&flowFromSock);
 }
 
 Link* NetworkNS3Model::createLink(const char *name, double bandwidth, double latency, e_surf_link_sharing_policy_t policy,
@@ -349,7 +349,7 @@ void NetworkNS3Model::updateActionsState(double now, double delta)
   }
 
   NetworkNS3Action *action;
-  xbt_dict_foreach(dict_socket,cursor,key,data){
+  xbt_dict_foreach(flowFromSock,cursor,key,data){
     action = static_cast<NetworkNS3Action*>(ns3_get_socket_action(data));
     XBT_DEBUG("Processing socket %p (action %p)",data,action);
     action->setRemains(action->getCost() - ns3_get_socket_sent(data));
@@ -380,10 +380,10 @@ void NetworkNS3Model::updateActionsState(double now, double delta)
   while (!xbt_dynar_is_empty(socket_to_destroy)){
     xbt_dynar_pop(socket_to_destroy,&key);
 
-    void *data = xbt_dict_get (dict_socket, key);
+    void *data = xbt_dict_get (flowFromSock, key);
     action = static_cast<NetworkNS3Action*>(ns3_get_socket_action(data));
     XBT_DEBUG ("Removing socket %p of action %p", key, action);
-    xbt_dict_remove(dict_socket, key);
+    xbt_dict_remove(flowFromSock, key);
   }
   return;
 }
@@ -466,19 +466,19 @@ void ns3_simulator(double min){
 }
 
 simgrid::surf::NetworkNS3Action* ns3_get_socket_action(void *socket){
-  return ((MySocket *)socket)->action;
+  return ((SgFlow *)socket)->action_;
 }
 
 double ns3_get_socket_remains(void *socket){
-  return ((MySocket *)socket)->remaining;
+  return ((SgFlow *)socket)->remaining_;
 }
 
 double ns3_get_socket_sent(void *socket){
-  return ((MySocket *)socket)->sentBytes;
+  return ((SgFlow *)socket)->sentBytes_;
 }
 
 bool ns3_get_socket_is_finished(void *socket){
-  return ((MySocket *)socket)->finished;
+  return ((SgFlow *)socket)->finished_;
 }
 
 int ns3_create_flow(const char* a,const char *b,double start,u_int32_t TotalBytes,simgrid::surf::NetworkNS3Action * action)
index 5ca5b4c..81ac50c 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2014. The SimGrid Team.
+/* Copyright (c) 2007-2016. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -9,14 +9,17 @@
 #include "xbt/log.h"
 #include "xbt/sysdep.h"
 
-xbt_dict_t dict_socket = NULL;
+static void delete_mysocket(void *p)
+{
+  delete (SgFlow *)p;
+}
+xbt_dict_t flowFromSock = xbt_dict_new_homogeneous(delete_mysocket);; // ns3::sock -> SgFlow
 
 NS3Sim SimulatorNS3;
-static char socket_key[24];
 
-static void receive_callback(ns3::Ptr<ns3::Socket> localSocket);
-static void send_callback(ns3::Ptr<ns3::Socket> localSocket, uint32_t txSpace);
-static void datasent_callback(ns3::Ptr<ns3::Socket> localSocket, uint32_t dataSent);
+static void receive_callback(ns3::Ptr<ns3::Socket> socket);
+static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace);
+static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
 static void StartFlow(ns3::Ptr<ns3::Socket> sock, const char *to, uint16_t port_number);
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
@@ -24,20 +27,21 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
 NS3Sim::NS3Sim(){
 }
 
-static inline void transformSocketPtr (ns3::Ptr<ns3::Socket> localSocket)
+static inline const char *transformSocketPtr (ns3::Ptr<ns3::Socket> localSocket)
 {
+  static char key[24];
   std::stringstream sstream;
   sstream << localSocket ;
-  std::string s = sstream.str();
-  sprintf(socket_key,"%s",s.c_str());
-}
+  sprintf(key,"%s",sstream.str().c_str());
 
-static void delete_mysocket(void *p)
-{
-  MySocket *sock = (MySocket *)p;
-  delete(sock);
+  return key;
 }
 
+SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action * action) {
+  totalBytes_ = totalBytes;
+  remaining_ = totalBytes;
+  action_ = action;
+}
 /*
  * This function creates a flow from src to dst
  *
@@ -53,21 +57,12 @@ void NS3Sim::create_flow_NS3(ns3::Ptr<ns3::Node> src, ns3::Ptr<ns3::Node> dst, u
                double startTime, const char *ipAddr, uint32_t totalBytes,
                simgrid::surf::NetworkNS3Action * action)
 {
-       if(!dict_socket)
-         dict_socket = xbt_dict_new_homogeneous(delete_mysocket);
-
        ns3::PacketSinkHelper sink("ns3::TcpSocketFactory", ns3::InetSocketAddress (ns3::Ipv4Address::GetAny(), port_number));
        sink.Install (dst);
 
        ns3::Ptr<ns3::Socket> sock = ns3::Socket::CreateSocket (src, ns3::TcpSocketFactory::GetTypeId());
 
-       MySocket *mysocket = new MySocket();
-       mysocket->totalBytes = totalBytes;
-       mysocket->remaining = totalBytes;
-       mysocket->action = action;
-
-       transformSocketPtr(sock);
-       xbt_dict_set(dict_socket,socket_key, mysocket,NULL);
+       xbt_dict_set(flowFromSock, transformSocketPtr(sock), new SgFlow(totalBytes, action), NULL);
 
        sock->Bind(ns3::InetSocketAddress(port_number));
        XBT_DEBUG("Create flow starting to %fs + %fs = %fs",
@@ -84,80 +79,75 @@ void NS3Sim::simulator_start(double min){
   ns3::Simulator::Run ();
 }
 
-static MySocket* get_my_socket(ns3::Ptr<ns3::Socket> localSocket) {
-       transformSocketPtr(localSocket);
-       return (MySocket*)xbt_dict_get_or_null(dict_socket,socket_key);
+static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket) {
+       return (SgFlow*)xbt_dict_get_or_null(flowFromSock, transformSocketPtr(socket));
 }
 
-static void receive_callback(ns3::Ptr<ns3::Socket> localSocket){
-  MySocket* mysocket = get_my_socket(localSocket);
+static void receive_callback(ns3::Ptr<ns3::Socket> socket){
+  SgFlow* flow = getFlowFromSocket(socket);
 
-  if (mysocket->finished == false){
-    mysocket->finished = true;
-    XBT_DEBUG("recv_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
+  if (flow->finished_ == false){
+    flow->finished_ = true;
+    XBT_DEBUG("recv_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
     XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
     ns3::Simulator::Stop(ns3::Seconds(0.0));
     ns3::Simulator::Run();
   }
 }
 
-static void send_callback(ns3::Ptr<ns3::Socket> localSocket, uint32_t txSpace){
-       MySocket* mysocket = get_my_socket(localSocket);
+static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace){
+       SgFlow* flow = getFlowFromSocket(sock);
 
-       if (mysocket->remaining == 0){
-                 // all data was already buffered (and socket was already closed), just return
-                 return;
-       }
+       if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
+         return;
 
        uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*txSpace);
 
-       while (mysocket->bufferedBytes < mysocket->totalBytes
-                       && localSocket->GetTxAvailable () > 0)
-       {
-      uint32_t toWrite = std::min ((mysocket->remaining), txSpace);
-      toWrite = std::min (toWrite, localSocket->GetTxAvailable ());
-      int amountSent = localSocket->Send (data, toWrite, 0);
+       while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable () > 0) {
+
+      uint32_t toWrite = std::min ({flow->remaining_, txSpace, sock->GetTxAvailable ()});
+      int amountSent = sock->Send (data, toWrite, 0);
 
       if(amountSent < 0)
          return;
-      (mysocket->bufferedBytes) += amountSent;
-      (mysocket->remaining) -= amountSent;
-      XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", mysocket, mysocket->action, mysocket->totalBytes, mysocket->remaining, mysocket->totalBytes, amountSent);
+      flow->bufferedBytes_ += amountSent;
+      flow->remaining_ -= amountSent;
 
+      XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", flow, flow->action_, flow->totalBytes_,
+          flow->remaining_, flow->totalBytes_, amountSent);
     }
-
        free(data);
 
-       if ((mysocket->bufferedBytes) >= mysocket->totalBytes)
-               localSocket->Close();
+       if ((flow->bufferedBytes_) >= flow->totalBytes_)
+               sock->Close();
 }
 
-static void datasent_callback(ns3::Ptr<ns3::Socket> localSocket, uint32_t dataSent){
-  MySocket* mysocket = get_my_socket(localSocket);
-  mysocket->sentBytes += dataSent;
-  XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", mysocket, mysocket->action, mysocket->totalBytes, dataSent);
+static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent){
+  SgFlow* flow = getFlowFromSocket(socket);
+  flow->sentBytes_ += dataSent;
+  XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", flow, flow->action_, flow->totalBytes_, dataSent);
 }
 
-static void normalClose_callback(ns3::Ptr<ns3::Socket> localSocket){
-  MySocket* mysocket = get_my_socket(localSocket);
-  XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
-  receive_callback (localSocket);
+static void normalClose_callback(ns3::Ptr<ns3::Socket> socket){
+  SgFlow* flow = getFlowFromSocket(socket);
+  XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
+  receive_callback (socket);
 }
 
-static void errorClose_callback(ns3::Ptr<ns3::Socket> localSocket){
-  MySocket* mysocket = get_my_socket(localSocket);
-  XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
+static void errorClose_callback(ns3::Ptr<ns3::Socket> socket){
+  SgFlow* flow = getFlowFromSocket(socket);
+  XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
   xbt_die("NS3: a socket was closed anormally");
 }
 
-static void succeededConnect_callback(ns3::Ptr<ns3::Socket> localSocket){
-  MySocket* mysocket = get_my_socket(localSocket);
-  XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
+static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket){
+  SgFlow* flow = getFlowFromSocket(socket);
+  XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
 }
 
-static void failedConnect_callback(ns3::Ptr<ns3::Socket> localSocket){
-  MySocket* mysocket = get_my_socket(localSocket);
-  XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action, mysocket->totalBytes);
+static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket){
+  SgFlow* mysocket = getFlowFromSocket(socket);
+  XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action_, mysocket->totalBytes_);
   xbt_die("NS3: a socket failed to connect");
 }
 
@@ -172,6 +162,6 @@ static void StartFlow(ns3::Ptr<ns3::Socket> sock, const char *to, uint16_t port_
   sock->SetConnectCallback (MakeCallback (&succeededConnect_callback), MakeCallback (&failedConnect_callback));
   sock->SetCloseCallbacks (MakeCallback (&normalClose_callback), MakeCallback (&errorClose_callback));
 
-  MySocket* mysocket = get_my_socket(sock);
-  XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", mysocket, mysocket->action, mysocket->totalBytes, to, port_number);
+  SgFlow* flow = getFlowFromSocket(sock);
+  XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
 }
index ddf1ab6..0a030f9 100644 (file)
 #include "ns3/inet-socket-address.h"
 #include "ns3/tcp-socket-factory.h"
 
-struct MySocket{
-  std::uint32_t bufferedBytes = 0;
-  std::uint32_t sentBytes = 0;
-  std::uint32_t remaining;
-  std::uint32_t totalBytes;
-  bool finished = false;
-  simgrid::surf::NetworkNS3Action* action;
+class SgFlow {
+public:
+  SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action * action);
+
+//private:
+  std::uint32_t bufferedBytes_ = 0;
+  std::uint32_t sentBytes_ = 0;
+  std::uint32_t remaining_;
+  std::uint32_t totalBytes_;
+  bool finished_ = false;
+  simgrid::surf::NetworkNS3Action* action_;
 };
 
 //Simulator s;