int main (int argc, char *argv[]) { Time::SetResolution (Time::NS); LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); NodeContainer nodes; nodes.Create (2); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); NetDeviceContainer devices; devices = pointToPoint.Install (nodes); InternetStackHelper stack; stack.Install (nodes); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer interfaces = address.Assign (devices); UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (nodes.Get (1)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (nodes.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); Simulator::Run (); Simulator::Destroy (); return 0; }
inline void RandomizeProducers (Ptr<UniformRandomVariable> uniVar, std::string serverDatasetsPath, NodeContainer& nodes, PointToPointHelper& p2p){ RouterEndPointMap pr; pr.clear(); size_t prodIndex = 0; while (prodIndex < ns3::N_PRODUCERS){ size_t rtrID = uniVar->GetInteger (0, ns3::N_GEANT_ROUTERS-1); if(prodIndex==0){ pr[prodIndex] = rtrID; ++prodIndex; } else{ size_t rtrID1=0; do{ rtrID1 = uniVar->GetInteger (0, ns3::N_GEANT_ROUTERS-1); }while(pr.find(rtrID1)!= pr.end()); pr[prodIndex] = rtrID1; ++prodIndex; } }//while //////////////////////////////////////////////////////////////// //****HERE I ATTACH PRODUCERS TO SOME OF ROUTERS RANDOMLY*****// /////////////////////////////////////////////////////////////// ndn::AppHelper producerHelper("ns3::ndn::Producer"); // Producer will reply to all requests starting with /videos/myvideos/video1 producerHelper.SetPrefix("/"); producerHelper.SetAttribute("PayloadSize", StringValue(PAYLOAD_SIZE)); std::string strID="id"; //choose randomly the producers NS_LOG_UNCOND("from among "<<ns3::N_PRODUCERS<<" producers:"); for (RouterEndPointMap::iterator it= pr.begin(); it!=pr.end(); it++) { p2p.SetDeviceAttribute ("DataRate", StringValue (ns3::PROD_LINK_DATA_RATE)); p2p.SetChannelAttribute ("Delay", StringValue (ns3::PROD_LINK_DELAY)); NS_LOG_UNCOND("PRODUCER "<<it->first<<" was attached to id"<<it->second<<" lINK DataRate= "<<ns3::PROD_LINK_DATA_RATE<<" and LINK Delay = "<<ns3::PROD_LINK_DELAY); p2p.Install(nodes.Get(it->first + ns3::N_TotalClients), Names::Find<Node>(strID + std::to_string(it->second))); producerHelper.Install(nodes.Get(it->first + ns3::N_TotalClients)).Start (Seconds(ns3::PROD_START)); }//for }//FUNCTION: RandomizeProducers
void Ns3TcpNoDelayTestCase::DoRun (void) { uint16_t sinkPort = 50000; double sinkStopTime = 8; // sec; will trigger Socket::Close double writerStopTime = 5; // sec; will trigger Socket::Close double simStopTime = 10; // sec Time sinkStopTimeObj = Seconds (sinkStopTime); Time writerStopTimeObj = Seconds (writerStopTime); Time simStopTimeObj= Seconds (simStopTime); Ptr<Node> n0 = CreateObject<Node> (); Ptr<Node> n1 = CreateObject<Node> (); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); NetDeviceContainer devices; devices = pointToPoint.Install (n0, n1); InternetStackHelper internet; internet.InstallAll (); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.252"); Ipv4InterfaceContainer ifContainer = address.Assign (devices); Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> (); Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort)); socketWriter->Setup (n0, sinkAddress); n0->AddApplication (socketWriter); socketWriter->SetStartTime (Seconds (0.)); socketWriter->SetStopTime (writerStopTimeObj); PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort)); ApplicationContainer apps = sink.Install (n1); // Start the sink application at time zero, and stop it at sinkStopTime apps.Start (Seconds (0.0)); apps.Stop (sinkStopTimeObj); Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx", MakeCallback (&Ns3TcpNoDelayTestCase::SinkRx, this)); // Enable or disable TCP no delay option Config::SetDefault ("ns3::TcpSocket::TcpNoDelay", BooleanValue (m_noDelay)); // Connect the socket writer Simulator::Schedule (Seconds (1), &SocketWriter::Connect, socketWriter); // Write 5 packets to get some bytes in flight and some acks going Simulator::Schedule (Seconds (2), &SocketWriter::Write, socketWriter, 2680); m_inputs.Add (536); m_inputs.Add (536); m_inputs.Add (536); m_inputs.Add (536); m_inputs.Add (536); // Write one byte after 10 ms to ensure that some data is outstanding // and the window is big enough Simulator::Schedule (Seconds (2.010), &SocketWriter::Write, socketWriter, 1); // If Nagle is not enabled, i.e. no delay is on, add an input for a 1-byte // packet to be received if (m_noDelay) { m_inputs.Add (1); } // One ms later, write 535 bytes, i.e. one segment size - 1 Simulator::Schedule (Seconds (2.012), &SocketWriter::Write, socketWriter, 535); // If Nagle is not enabled, add an input for a 535 byte packet, // otherwise, we should get a single "full" packet of 536 bytes if (m_noDelay) { m_inputs.Add (535); } else { m_inputs.Add (536); } // Close down the socket Simulator::Schedule (writerStopTimeObj, &SocketWriter::Close, socketWriter); if (m_writeResults) { std::ostringstream oss; if (m_noDelay) { oss << "tcp-no-delay-on-test-case"; pointToPoint.EnablePcapAll (oss.str ()); } else { oss << "tcp-no-delay-off-test-case"; pointToPoint.EnablePcapAll (oss.str ()); } } Simulator::Stop (simStopTimeObj); Simulator::Run (); Simulator::Destroy (); // Compare inputs and outputs NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events"); for (uint32_t i = 0; i < m_responses.GetN (); i++) { uint32_t in = m_inputs.Get (i); uint32_t out = m_responses.Get (i); NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes"); } }
int main (int argc, char *argv[]) { // // Set up some default values for the simulation. // Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137)); // ??? try and stick 15kb/s into the data rate Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s")); // // Default number of nodes in the star. Overridable by command line argument. // uint32_t nSpokes = 8; CommandLine cmd; cmd.AddValue ("nSpokes", "Number of nodes to place in the star", nSpokes); cmd.Parse (argc, argv); NS_LOG_INFO ("Build star topology."); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); PointToPointStarHelper star (nSpokes, pointToPoint); NS_LOG_INFO ("Install internet stack on all nodes."); InternetStackHelper internet; star.InstallStack (internet); NS_LOG_INFO ("Assign IP Addresses."); star.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0")); NS_LOG_INFO ("Create applications."); // // Create a packet sink on the star "hub" to receive packets. // uint16_t port = 50000; Address hubLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port)); PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress); ApplicationContainer hubApp = packetSinkHelper.Install (star.GetHub ()); hubApp.Start (Seconds (1.0)); hubApp.Stop (Seconds (10.0)); // // Create OnOff applications to send TCP to the hub, one on each spoke node. // OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ()); onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]")); onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); ApplicationContainer spokeApps; for (uint32_t i = 0; i < star.SpokeCount (); ++i) { AddressValue remoteAddress (InetSocketAddress (star.GetHubIpv4Address (i), port)); onOffHelper.SetAttribute ("Remote", remoteAddress); spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (i))); } spokeApps.Start (Seconds (1.0)); spokeApps.Stop (Seconds (10.0)); NS_LOG_INFO ("Enable static global routing."); // // Turn on global static routing so we can actually be routed across the star. // Ipv4GlobalRoutingHelper::PopulateRoutingTables (); NS_LOG_INFO ("Enable pcap tracing."); // // Do pcap tracing on all point-to-point devices on all nodes. // pointToPoint.EnablePcapAll ("star"); NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Done."); return 0; }