examples/tcp-large-transfer.cc
changeset 3240 965da7165e78
parent 3239 be022cc7fec9
child 3241 284705f0775f
equal deleted inserted replaced
3239:be022cc7fec9 3240:965da7165e78
    25 // - Tracing of queues and packet receptions to file 
    25 // - Tracing of queues and packet receptions to file 
    26 //   "tcp-large-transfer.tr"
    26 //   "tcp-large-transfer.tr"
    27 // - pcap traces also generated in the following files
    27 // - pcap traces also generated in the following files
    28 //   "tcp-large-transfer.pcap-$n-$i" where n and i represent node and interface numbers respectively
    28 //   "tcp-large-transfer.pcap-$n-$i" where n and i represent node and interface numbers respectively
    29 //  Usage (e.g.): ./waf --run tcp-large-transfer
    29 //  Usage (e.g.): ./waf --run tcp-large-transfer
       
    30 
       
    31 //XXX this isn't working as described right now
       
    32 //it is just blasting away for 10 seconds, with no fixed amount of data
       
    33 //being sent
    30 
    34 
    31 #include <ctype.h>
    35 #include <ctype.h>
    32 #include <iostream>
    36 #include <iostream>
    33 #include <fstream>
    37 #include <fstream>
    34 #include <string>
    38 #include <string>
    71     }
    75     }
    72   }
    76   }
    73 #endif
    77 #endif
    74 }
    78 }
    75 
    79 
       
    80 void CloseConnection (Ptr<Socket> localSocket);
       
    81 void StartFlow(Ptr<Socket>, uint32_t, Ipv4Address, uint16_t);
       
    82 void WriteUntilBufferFull (Ptr<Socket>, uint32_t);
       
    83 
       
    84 int main (int argc, char *argv[])
       
    85 {
       
    86 
       
    87   // Users may find it convenient to turn on explicit debugging
       
    88   // for selected modules; the below lines suggest how to do this
       
    89   //  LogComponentEnable("TcpL4Protocol", LOG_LEVEL_ALL);
       
    90   //  LogComponentEnable("TcpSocket", LOG_LEVEL_ALL);
       
    91   //  LogComponentEnable("PacketSink", LOG_LEVEL_ALL);
       
    92   //  LogComponentEnable("TcpLargeTransfer", LOG_LEVEL_ALL);
       
    93 
       
    94   //
       
    95   // Make the random number generators generate reproducible results.
       
    96   //
       
    97   RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
       
    98 
       
    99   // Allow the user to override any of the defaults and the above
       
   100   // Bind()s at run-time, via command-line arguments
       
   101   CommandLine cmd;
       
   102   cmd.Parse (argc, argv);
       
   103 
       
   104   // Here, we will explicitly create three nodes. 
       
   105   NodeContainer c0;
       
   106   c0.Create (2);
       
   107 
       
   108   NodeContainer c1;
       
   109   c1.Add (c0.Get (1));
       
   110   c1.Create (1);
       
   111 
       
   112   // We create the channels first without any IP addressing information
       
   113   PointToPointHelper p2p;
       
   114   p2p.SetChannelParameter ("BitRate", DataRateValue (DataRate(100000)));
       
   115   p2p.SetChannelParameter ("Delay", TimeValue (MilliSeconds(10)));
       
   116   NetDeviceContainer dev0 = p2p.Install (c0);
       
   117   NetDeviceContainer dev1 = p2p.Install (c1);
       
   118 
       
   119   // add ip/tcp stack to nodes.
       
   120   NodeContainer c = NodeContainer (c0, c1.Get (1));
       
   121   InternetStackHelper internet;
       
   122   internet.Install (c);
       
   123 
       
   124   // Later, we add IP addresses.  
       
   125   Ipv4AddressHelper ipv4;
       
   126   ipv4.SetBase ("10.1.3.0", "255.255.255.0");
       
   127   ipv4.Assign (dev0);
       
   128   ipv4.SetBase ("10.1.2.0", "255.255.255.0");
       
   129   Ipv4InterfaceContainer ipInterfs = ipv4.Assign (dev1);
       
   130 
       
   131   // and setup ip routing tables to get total ip-level connectivity.
       
   132   GlobalRouteManager::PopulateRoutingTables ();
       
   133 
       
   134   ///////////////////////////////////////////////////////////////////////////
       
   135   // Simulation 1
       
   136   // 
       
   137   // Send 2000000 bytes over a connection to server port 50000 at time 0
       
   138   // Should observe SYN exchange, a lot of data segments, and FIN exchange
       
   139   //
       
   140   ///////////////////////////////////////////////////////////////////////////
       
   141 
       
   142   int nBytes = 2000000;
       
   143   uint16_t servPort = 50000;
       
   144 
       
   145   // Create a packet sink to receive these packets
       
   146   PacketSinkHelper sink ("ns3::Tcp",
       
   147                          InetSocketAddress (Ipv4Address::GetAny (), servPort));
       
   148 
       
   149   ApplicationContainer apps = sink.Install (c1.Get (1));
       
   150   apps.Start (Seconds (0.0));
       
   151 
       
   152   // and generate traffic to remote sink.
       
   153   Ptr<SocketFactory> socketFactory = 
       
   154     c0.Get (0)->GetObject<Tcp> ();
       
   155   Ptr<Socket> localSocket = socketFactory->CreateSocket ();
       
   156   localSocket->Bind ();
       
   157   Simulator::ScheduleNow (&StartFlow, localSocket, nBytes,
       
   158                           ipInterfs.GetAddress (1), servPort);
       
   159 
       
   160   Config::ConnectWithoutContext ("/NodeList/*/ApplicationList/*/Rx", 
       
   161                    MakeCallback (&ApplicationTraceSink));
       
   162 
       
   163   std::ofstream ascii;
       
   164   ascii.open ("tcp-large-transfer.tr");
       
   165   PointToPointHelper::EnableAsciiAll (ascii);
       
   166 
       
   167   InternetStackHelper::EnablePcapAll ("tcp-large-transfer");
       
   168 
       
   169   Simulator::StopAt (Seconds(10));
       
   170   Simulator::Run ();
       
   171   Simulator::Destroy ();
       
   172 }
       
   173 
    76 void CloseConnection (Ptr<Socket> localSocket)
   174 void CloseConnection (Ptr<Socket> localSocket)
    77 {
   175 {
    78   localSocket->Close ();
   176   localSocket->Close ();
    79 }
   177 }
    80 
   178 
    84 {
   182 {
    85  // NS_LOG_LOGIC("Starting flow at time " <<  Simulator::Now ().GetSeconds ());
   183  // NS_LOG_LOGIC("Starting flow at time " <<  Simulator::Now ().GetSeconds ());
    86   localSocket->Connect (InetSocketAddress (servAddress, servPort));//connect
   184   localSocket->Connect (InetSocketAddress (servAddress, servPort));//connect
    87   localSocket->SetConnectCallback (MakeCallback (&CloseConnection),
   185   localSocket->SetConnectCallback (MakeCallback (&CloseConnection),
    88                                    Callback<void, Ptr<Socket> > (),
   186                                    Callback<void, Ptr<Socket> > (),
    89                                    Callback<void, Ptr<Socket> > ());
   187                                        Callback<void, Ptr<Socket> > ());
    90   //we want to close as soon as the connection is established
   188   //we want to close as soon as the connection is established
    91   //the tcp state machine and outgoing buffer will assure that
   189   //the tcp state machine and outgoing buffer will assure that
    92   //all of the data is delivered
   190   //all of the data is delivered
    93 
   191   localSocket->SetSendCallback (MakeCallback (&WriteUntilBufferFull));
       
   192   WriteUntilBufferFull (localSocket, nBytes);
       
   193 }
       
   194 
       
   195 void WriteUntilBufferFull (Ptr<Socket> localSocket, uint32_t nBytes)
       
   196 {
    94   // Perform series of 1040 byte writes (this is a multiple of 26 since
   197   // Perform series of 1040 byte writes (this is a multiple of 26 since
    95   // we want to detect data splicing in the output stream)
   198   // we want to detect data splicing in the output stream)
       
   199   std::cout << "nBytes = " <<nBytes <<"@"<<Simulator::Now().GetSeconds()<<std::endl;
    96   uint32_t writeSize = 1040;
   200   uint32_t writeSize = 1040;
    97   uint8_t data[writeSize];
   201   uint8_t data[writeSize];
    98   while (nBytes > 0) {
   202   while (nBytes > 0) {
    99     uint32_t curSize= nBytes > writeSize ? writeSize : nBytes;
   203     uint32_t curSize= nBytes > writeSize ? writeSize : nBytes;
   100     for(uint32_t i = 0; i < curSize; ++i)
   204     for(uint32_t i = 0; i < curSize; ++i)
   104     }
   208     }
   105     localSocket->Send (data, curSize);
   209     localSocket->Send (data, curSize);
   106     nBytes -= curSize;
   210     nBytes -= curSize;
   107   }
   211   }
   108 }
   212 }
   109 
       
   110 int main (int argc, char *argv[])
       
   111 {
       
   112 
       
   113   // Users may find it convenient to turn on explicit debugging
       
   114   // for selected modules; the below lines suggest how to do this
       
   115   //  LogComponentEnable("TcpL4Protocol", LOG_LEVEL_ALL);
       
   116   //  LogComponentEnable("TcpSocket", LOG_LEVEL_ALL);
       
   117   //  LogComponentEnable("PacketSink", LOG_LEVEL_ALL);
       
   118   //  LogComponentEnable("TcpLargeTransfer", LOG_LEVEL_ALL);
       
   119 
       
   120   //
       
   121   // Make the random number generators generate reproducible results.
       
   122   //
       
   123   RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
       
   124 
       
   125   // Allow the user to override any of the defaults and the above
       
   126   // Bind()s at run-time, via command-line arguments
       
   127   CommandLine cmd;
       
   128   cmd.Parse (argc, argv);
       
   129 
       
   130   // Here, we will explicitly create three nodes. 
       
   131   NodeContainer c0;
       
   132   c0.Create (2);
       
   133 
       
   134   NodeContainer c1;
       
   135   c1.Add (c0.Get (1));
       
   136   c1.Create (1);
       
   137 
       
   138   // We create the channels first without any IP addressing information
       
   139   PointToPointHelper p2p;
       
   140   p2p.SetChannelParameter ("BitRate", DataRateValue (DataRate(10000000)));
       
   141   p2p.SetChannelParameter ("Delay", TimeValue (MilliSeconds(10)));
       
   142   NetDeviceContainer dev0 = p2p.Install (c0);
       
   143   NetDeviceContainer dev1 = p2p.Install (c1);
       
   144 
       
   145   // add ip/tcp stack to nodes.
       
   146   NodeContainer c = NodeContainer (c0, c1.Get (1));
       
   147   InternetStackHelper internet;
       
   148   internet.Install (c);
       
   149 
       
   150   // Later, we add IP addresses.  
       
   151   Ipv4AddressHelper ipv4;
       
   152   ipv4.SetBase ("10.1.3.0", "255.255.255.0");
       
   153   ipv4.Assign (dev0);
       
   154   ipv4.SetBase ("10.1.2.0", "255.255.255.0");
       
   155   Ipv4InterfaceContainer ipInterfs = ipv4.Assign (dev1);
       
   156 
       
   157   // and setup ip routing tables to get total ip-level connectivity.
       
   158   GlobalRouteManager::PopulateRoutingTables ();
       
   159 
       
   160   ///////////////////////////////////////////////////////////////////////////
       
   161   // Simulation 1
       
   162   // 
       
   163   // Send 2000000 bytes over a connection to server port 50000 at time 0
       
   164   // Should observe SYN exchange, a lot of data segments, and FIN exchange
       
   165   //
       
   166   ///////////////////////////////////////////////////////////////////////////
       
   167 
       
   168   int nBytes = 2000;
       
   169   uint16_t servPort = 50000;
       
   170 
       
   171   // Create a packet sink to receive these packets
       
   172   PacketSinkHelper sink ("ns3::Tcp",
       
   173                          InetSocketAddress (Ipv4Address::GetAny (), servPort));
       
   174 
       
   175   ApplicationContainer apps = sink.Install (c1.Get (1));
       
   176   apps.Start (Seconds (0.0));
       
   177 
       
   178   // and generate traffic to remote sink.
       
   179   Ptr<SocketFactory> socketFactory = 
       
   180     c0.Get (0)->GetObject<Tcp> ();
       
   181   Ptr<Socket> localSocket = socketFactory->CreateSocket ();
       
   182   localSocket->Bind ();
       
   183   Simulator::ScheduleNow (&StartFlow, localSocket, nBytes,
       
   184                           ipInterfs.GetAddress (1), servPort);
       
   185 
       
   186   Config::ConnectWithoutContext ("/NodeList/*/ApplicationList/*/Rx", 
       
   187                    MakeCallback (&ApplicationTraceSink));
       
   188 
       
   189   std::ofstream ascii;
       
   190   ascii.open ("tcp-large-transfer.tr");
       
   191   PointToPointHelper::EnableAsciiAll (ascii);
       
   192 
       
   193   InternetStackHelper::EnablePcapAll ("tcp-large-transfer");
       
   194 
       
   195   Simulator::StopAt (Seconds(1000));
       
   196   Simulator::Run ();
       
   197   Simulator::Destroy ();
       
   198 }