int main (int argc, char *argv[]) { NodeContainer nodes; nodes.Create (2); PointToPointHelper pointToPoint; NetDeviceContainer devices; devices = pointToPoint.Install (nodes); InternetStackHelper stack; stack.Install (nodes); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.252"); Ipv4InterfaceContainer interfaces = address.Assign (devices); uint16_t sinkPort = 8080; Address sinkAddress (InetSocketAddress (interfaces.GetAddress (1), sinkPort)); Ptr<PacketSink> receiverApplication = CreateObject<PacketSink> (); receiverApplication->SetAttribute ("Local", AddressValue (InetSocketAddress (Ipv4Address::GetAny(), 8080))); receiverApplication->SetAttribute ("Protocol", TypeIdValue(TcpSocketFactory::GetTypeId())); receiverApplication->TraceConnectWithoutContext ("Rx", MakeCallback (&CountRx)); nodes.Get(1)->AddApplication(receiverApplication); Ptr<MyApp> app = CreateObject<MyApp> (nodes.Get (0), sinkAddress); nodes.Get (0)->AddApplication (app); Simulator::Stop (); Simulator::Run (); Simulator::Destroy (); return 0; }
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[]) { uint32_t packetSize = 1000; // Application bytes per packet double interval = 1.0; // Time between events uint32_t generationSize = 5; // RLNC generation size double errorRate = 0.3; // Error rate for all the links Time interPacketInterval = Seconds (interval); CommandLine cmd; cmd.AddValue ("packetSize", "Size of application packet sent", packetSize); cmd.AddValue ("interval", "Interval (seconds) between packets", interval); cmd.AddValue ("generationSize", "Set the generation size to use", generationSize); cmd.AddValue ("errorRate", "Packet erasure rate for the links", errorRate); cmd.Parse (argc, argv); Time::SetResolution (Time::NS); // Set the basic helper for a single link PointToPointHelper pointToPoint; // Two receivers against a centralized hub. Note: DO NOT CHANGE THIS LINE PointToPointStarHelper star (2, pointToPoint); // Set error model for the net devices Config::SetDefault ("ns3::RateErrorModel::ErrorUnit", StringValue ("ERROR_UNIT_PACKET")); Ptr<RateErrorModel> error_model = CreateObject<RateErrorModel> (); error_model->SetAttribute ("ErrorRate", DoubleValue (errorRate)); star.GetSpokeNode (0)->GetDevice (0)-> SetAttribute ("ReceiveErrorModel", PointerValue (error_model)); star.GetSpokeNode (1)->GetDevice (0)-> SetAttribute ("ReceiveErrorModel", PointerValue (error_model)); error_model->Enable (); // Setting IP protocol stack InternetStackHelper internet; star.InstallStack(internet); // Set IP addresses star.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0")); // Creation of RLNC encoder and decoder objects rlnc_encoder::factory encoder_factory(generationSize, packetSize); rlnc_decoder::factory decoder_factory(generationSize, packetSize); // The member build function creates differents instances of each object KodoSimulation kodoSimulator(encoder_factory.build(), decoder_factory.build(), decoder_factory.build()); // Setting up application sockets for receivers and senders uint16_t port = 80; TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), port); // Receivers Ptr<Socket> recvSink1 = Socket::CreateSocket (star.GetSpokeNode (0), tid); recvSink1->Bind (local); recvSink1->SetRecvCallback (MakeCallback (&KodoSimulation::ReceivePacket1, &kodoSimulator)); Ptr<Socket> recvSink2 = Socket::CreateSocket (star.GetSpokeNode (1), tid); recvSink2->Bind (local); recvSink2->SetRecvCallback (MakeCallback (&KodoSimulation::ReceivePacket2, &kodoSimulator)); // Sender Ptr<Socket> source = Socket::CreateSocket (star.GetHub (), tid); InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), port); source->SetAllowBroadcast (true); source->Connect (remote); // Turn on global static routing so we can actually be routed across the star Ipv4GlobalRoutingHelper::PopulateRoutingTables (); // Do pcap tracing on all point-to-point devices on all nodes pointToPoint.EnablePcapAll ("star"); Simulator::ScheduleWithContext (source->GetNode ()->GetId (), Seconds (1.0), &KodoSimulation::GenerateTraffic, &kodoSimulator, source, interPacketInterval); Simulator::Run (); Simulator::Destroy (); return 0; }
int main (int argc, char *argv[]) { uint32_t packetSize = 1000; // Application bytes per packet double interval = 1.0; // Time between events uint32_t generationSize = 3; // RLNC generation size double errorRateEncoderRecoder = 0.4; // Error rate for encoder-recoder link double errorRateRecoderDecoder = 0.2; // Error rate for recoder-decoder link bool recodingFlag = true; // Flag to control recoding Time interPacketInterval = Seconds (interval); CommandLine cmd; cmd.AddValue ("packetSize", "Size of application packet sent", packetSize); cmd.AddValue ("interval", "Interval (seconds) between packets", interval); cmd.AddValue ("generationSize", "Set the generation size to use", generationSize); cmd.AddValue ("errorRateEncoderRecoder", "Packet erasure rate for the encoder-recoder link", errorRateEncoderRecoder); cmd.AddValue ("errorRateRecoderDecoder", "Packet erasure rate for the recoder-decoder link", errorRateRecoderDecoder); cmd.AddValue ("recodingFlag", "Enable packet recoding", recodingFlag); cmd.Parse (argc, argv); Time::SetResolution (Time::NS); // Set the basic helper for a single link PointToPointHelper pointToPoint; // Create node containers NodeContainer nodes; nodes.Create (3); NodeContainer encoderRecoder = NodeContainer (nodes.Get (0), nodes.Get (1)); NodeContainer recoderDecoder = NodeContainer (nodes.Get (1), nodes.Get (2)); // Internet stack for the nodes InternetStackHelper internet; internet.Install (nodes); // Create net device containers NetDeviceContainer encoderRecoderDevs = pointToPoint.Install (encoderRecoder); NetDeviceContainer recoderDecoderDevs = pointToPoint.Install (recoderDecoder); NetDeviceContainer devices = NetDeviceContainer (encoderRecoderDevs, recoderDecoderDevs); // Set IP addresses Ipv4AddressHelper ipv4("10.1.1.0", "255.255.255.0"); ipv4.Assign (devices); // Turn on global static routing so we can actually be routed across the hops Ipv4GlobalRoutingHelper::PopulateRoutingTables (); // Do pcap tracing on all point-to-point devices on all nodes. File naming // convention is: multihop-[NODE_NUMBER]-[DEVICE_NUMBER].pcap pointToPoint.EnablePcapAll ("multihop"); // Set error model for the net devices Config::SetDefault ("ns3::RateErrorModel::ErrorUnit", StringValue ("ERROR_UNIT_PACKET")); Ptr<RateErrorModel> errorEncoderRecoder = CreateObject<RateErrorModel> (); errorEncoderRecoder->SetAttribute ("ErrorRate", DoubleValue (errorRateEncoderRecoder)); devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (errorEncoderRecoder)); Ptr<RateErrorModel> errorRecoderDecoder = CreateObject<RateErrorModel> (); errorRecoderDecoder->SetAttribute ("ErrorRate", DoubleValue (errorRateRecoderDecoder)); devices.Get (3)->SetAttribute ("ReceiveErrorModel", PointerValue (errorRecoderDecoder)); errorEncoderRecoder->Enable (); errorRecoderDecoder->Enable (); // Creation of RLNC encoder and decoder objects rlnc_encoder::factory encoder_factory(generationSize, packetSize); rlnc_decoder::factory decoder_factory(generationSize, packetSize); // The member build function creates differents instances of each object KodoSimulation kodoSimulator(encoder_factory.build(), decoder_factory.build(), decoder_factory.build(), recodingFlag); // Setting up application sockets for recoder and decoder uint16_t port = 80; TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); // Get node Ipv4 addresses Ipv4Address recoderAddress = nodes.Get (1)->GetObject<Ipv4>()-> GetAddress(1,0).GetLocal(); Ipv4Address decoderAddress = nodes.Get (2)->GetObject<Ipv4>()-> GetAddress(1,0).GetLocal(); // Socket connection addresses InetSocketAddress recoderSocketAddress = InetSocketAddress (recoderAddress, port); InetSocketAddress decoderSocketAddress = InetSocketAddress (decoderAddress, port); // Socket bind address InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny(), port); // Encoder Ptr<Socket> encoderSocket = Socket::CreateSocket (nodes.Get (0), tid); encoderSocket->Connect (recoderSocketAddress); // Recoder Ptr<Socket> recoderSocket = Socket::CreateSocket (nodes.Get (1), tid); recoderSocket->Bind(local); recoderSocket->Connect (decoderSocketAddress); recoderSocket-> SetRecvCallback (MakeCallback (&KodoSimulation::ReceivePacketRecoder, &kodoSimulator)); // Decoder Ptr<Socket> decoderSocket = Socket::CreateSocket (nodes.Get (2), tid); decoderSocket->Bind(local); decoderSocket-> SetRecvCallback (MakeCallback (&KodoSimulation::ReceivePacketDecoder, &kodoSimulator)); // Simulation setup // Schedule encoding process Simulator::ScheduleWithContext (encoderSocket->GetNode ()->GetId (), Seconds (1.0), &KodoSimulation::SendPacketEncoder, &kodoSimulator, encoderSocket, interPacketInterval); Simulator::ScheduleWithContext (recoderSocket->GetNode ()->GetId (), Seconds (1.5), &KodoSimulation::SendPacketRecoder, &kodoSimulator, recoderSocket, interPacketInterval); Simulator::Run (); Simulator::Destroy (); return 0; }
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; }
inline void RandomizeConsumers (Ptr<UniformRandomVariable> uniVar, std::string topoFilePath, AnnotatedTopologyReader& topologyReader, NodeContainer& nodes,PointToPointHelper& p2p){ /////////////////////////////////////////////////////////////////////////////// //we aim adding 56 clients randomly anywhere so that nNode becomes 39+56=95. //we can later add up to some servers so that we will have 95 nodes in total. //we choose randomly some core routers to which we attach Randomly between //three to six clients. Now I add five servers, so 100 nodes in total. /////////////////////////////////////////////////////////////////////////////// RouterEndPointMap rc; rc.clear(); size_t totalClientCount = 0; while (totalClientCount < ns3::N_TotalClients){ size_t key = uniVar->GetInteger (0, ns3::N_GEANT_ROUTERS-1); if (totalClientCount==0){ rc[key] = uniVar->GetInteger (3,6); totalClientCount += rc[key]; } else{ //make sure a new router is chosen ! size_t key1=0; do{ key1 = uniVar->GetInteger (0, ns3::N_GEANT_ROUTERS-1); }while (rc.find(key1)!=rc.end()); rc[key1] = uniVar->GetInteger (3,6); totalClientCount += rc[key1]; } if (ns3::N_TotalClients - totalClientCount <= 6){ size_t key2=0; do{ key2 = uniVar->GetInteger (0, ns3::N_GEANT_ROUTERS-1); }while (rc.find(key2)!=rc.end()); rc[key2] = N_TotalClients - totalClientCount; totalClientCount+=rc[key2]; break; } }//while for (RouterEndPointMap::iterator it=rc.begin(); it!=rc.end();it++){ NS_LOG_UNCOND("rcTable["<<it->first <<"] = " <<it->second); } NS_LOG_UNCOND("total number of added clinets = "<< totalClientCount); /////////////////////////////////////// //***Attaching consumers*** /////////////////////////////////////// //now let's attach clients to core routers size_t counter=0; std::string strID = "id"; for (RouterEndPointMap::iterator it= rc.begin(); it!=rc.end();it++){ //NS_LOG_UNCOND(it->second<<" clients were attached to router "<<it->first); for (std::size_t i=0; i < it->second; i++){ p2p.Install(nodes.Get(counter), Names::Find<Node>(strID + std::to_string(it->first))); ++counter; }//for int }//for ext /* Simulator::Schedule(Seconds(firstFailureTime), ndn::LinkControlHelper::FailLink, Names::Find<Node>("id2"), Names::Find<Node>("id4")); Simulator::Schedule(Seconds(firstRecoveryTime), ndn::LinkControlHelper::UpLink, Names::Find<Node>("id2"), Names::Find<Node>("id4")); Simulator::Schedule(Seconds(secondFailureTime), ndn::LinkControlHelper::FailLink, Names::Find<Node>("id2"), Names::Find<Node>("id4")); Simulator::Schedule(Seconds(secondRecoveryTime), ndn::LinkControlHelper::UpLink, Names::Find<Node>("id2"), Names::Find<Node>("id4")); Simulator::Schedule(Seconds(thirdFailureTime), ndn::LinkControlHelper::FailLink, Names::Find<Node>("id2"), Names::Find<Node>("id4")); Simulator::Schedule(Seconds(thirdRecoveryTime), ndn::LinkControlHelper::UpLink, Names::Find<Node>("id2"), Names::Find<Node>("id4")); */ }//FUNCTION: RandomizeConsumers