Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[NS3] rewriting receive_callback to mark a flow finished only if it has not done...
[simgrid.git] / src / surf / ns3 / ns3_simulator.cc
index bd52efc..54d45c8 100644 (file)
@@ -11,8 +11,6 @@
 using namespace ns3;
 using namespace std;
 
-static const uint32_t writeSize  = 1024; // limit the amout of data to write
-uint8_t data[writeSize];
 xbt_dict_t dict_socket = NULL;
 
 NS3Sim SimulatorNS3;
@@ -55,13 +53,17 @@ void NS3Sim::create_flow_NS3(
                void * action)
 {
        if(!dict_socket) dict_socket = xbt_dict_new();
-       PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny(), port_number));
+
+       PacketSinkHelper sink ("ns3::TcpSocketFactory",
+                                                       InetSocketAddress (Ipv4Address::GetAny(),
+                                                       port_number));
        sink.Install (dst);
-       Ptr<Socket> sock = Socket::CreateSocket (src, TypeId::LookupByName ("ns3::TcpSocketFactory"));
+       Ptr<Socket> sock = Socket::CreateSocket (src,
+                                                       TcpSocketFactory::GetTypeId());
+
        MySocket *mysocket = new MySocket();
        mysocket->totalBytes = totalBytes;
        mysocket->remaining = totalBytes;
-       mysocket->last_amount_sent = 0;
        mysocket->bufferedBytes = 0;
        mysocket->sentBytes = 0;
        mysocket->finished = 0;
@@ -87,14 +89,6 @@ double NS3Sim::get_sent_from_socket(void *socket){
   return ((MySocket *)socket)->sentBytes;
 }
 
-double NS3Sim::get_last_amount_sent_from_socket(void *socket){
-       return ((MySocket *)socket)->last_amount_sent;
-}
-
-void NS3Sim::reset_last_amount_sent_from_socket(void *socket){
-       ((MySocket *)socket)->last_amount_sent = 0;
-}
-
 void NS3Sim::simulator_stop(double min){
        if(min > 0.0)
                Simulator::Stop(Seconds(min));
@@ -108,56 +102,45 @@ void NS3Sim::simulator_start(void){
 }
 
 static void receive_callback(Ptr<Socket> localSocket){
-  Address addr;
-  localSocket->GetSockName (addr);
-  InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
-  mysocket->finished = 1;
-
-  //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Received [" << mysocket->totalBytes << "bytes],  from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << endl;
-       std::stringstream sstream;
-               sstream << Simulator::Now ().GetSeconds();
-               std::string s = sstream.str();
-               size_t size = s.size() + 1;
-               char * time_sec = new char[ size ];
-               strncpy( time_sec, s.c_str(), size );
-  XBT_DEBUG("Stop simulator at %s seconds",time_sec);
-  Simulator::Stop();
+
+  if (mysocket->finished == 0){
+    mysocket->finished = 1;
+//    cout << "[" << Simulator::Now ().GetSeconds() << "] " << "recv_cb of F[" << mysocket->totalBytes << "] " << endl;
+    XBT_DEBUG("Stop simulator at %f seconds", Simulator::Now().GetSeconds());
+    Simulator::Stop();
+  }
 }
 
 static void send_callback(Ptr<Socket> localSocket, uint32_t txSpace){
-
-       Address addr;
-       localSocket->GetSockName (addr);
-       InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
        MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
-       uint32_t totalBytes = mysocket->totalBytes;
-       while ((mysocket->bufferedBytes) < totalBytes && localSocket->GetTxAvailable () > 0){
-         uint32_t toWrite = min ((mysocket->remaining), writeSize);
-         toWrite = min (toWrite, localSocket->GetTxAvailable ());
-         int amountSent = localSocket->Send (&data[0], toWrite, 0);
-
-         if(amountSent < 0)
-           return;
-
-         (mysocket->last_amount_sent) += amountSent;
-         (mysocket->bufferedBytes) += amountSent;
-         (mysocket->remaining) -= amountSent;
-         //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "Send one packet, remaining "<<  mysocket->remaining << " bytes!" << endl;
+
+       if (mysocket->remaining == 0){
+         //all data was already buffered (and socket was already closed), just return
+         return;
        }
-       if ((mysocket->bufferedBytes) >= totalBytes){
-               localSocket->Close();
+
+       uint32_t toWrite = min (mysocket->remaining, txSpace);
+       uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*toWrite);
+       int amountSent = localSocket->Send (&data[0], toWrite, 0);
+       free (data);
+       if (amountSent > 0){
+         mysocket->bufferedBytes += amountSent;
+         mysocket->remaining -= amountSent;
        }
+//     cout << "[" << Simulator::Now ().GetSeconds() << "] " << "send_cb of F[" << mysocket->totalBytes << "] ("<<  mysocket->remaining << " / " << mysocket->totalBytes << ") " << amountSent << " buffered." << endl;
 
+  if (mysocket->remaining == 0){
+    //everything was buffered to send, tell NS3 to close the socket
+    localSocket->Close();
+  }
+       return;
 }
 
 static void datasent_callback(Ptr<Socket> localSocket, uint32_t dataSent){
-  Address addr;
-  localSocket->GetSockName (addr);
-  InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
   MySocket* mysocket = (MySocket*)xbt_dict_get_or_null(dict_socket,(char*)&localSocket);
   mysocket->sentBytes += dataSent;
-  //cout << "[" << Simulator::Now ().GetSeconds() << "] " << "DATASENT [" << mysocket->totalBytes << "bytes],  from: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << " dataSent " << dataSent <<endl;
+//  cout << "[" << Simulator::Now ().GetSeconds() << "] " << "datasent_cb of F[" << mysocket->totalBytes << "] " << dataSent << " sent." << endl;
 }