Advertisement
MdSadmanSiraj

exp_second.cc

Oct 1st, 2022
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3.  * This program is free software; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License version 2 as
  5.  * published by the Free Software Foundation;
  6.  *
  7.  * This program is distributed in the hope that it will be useful,
  8.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  * GNU General Public License for more details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License
  13.  * along with this program; if not, write to the Free Software
  14.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  15.  */
  16.  
  17. #include "ns3/core-module.h"
  18. #include "ns3/network-module.h"
  19. #include "ns3/csma-module.h"
  20. #include "ns3/internet-module.h"
  21. #include "ns3/point-to-point-module.h"
  22. #include "ns3/applications-module.h"
  23. #include "ns3/ipv4-global-routing-helper.h"
  24. #include "ns3/netanim-module.h"
  25.  
  26. // Default Network Topology
  27. //
  28. //       10.1.1.0
  29. // n0 -------------- n1   n2   n3   n4
  30. //    point-to-point  |    |    |    |
  31. //                    ================
  32. //                      LAN 10.1.2.0
  33.  
  34.  
  35. using namespace ns3;
  36.  
  37. NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
  38.  
  39. int
  40. main (int argc, char *argv[])
  41. {
  42.   bool verbose = true;
  43.   uint32_t nCsma = 5;
  44.  
  45.   CommandLine cmd (__FILE__);
  46.   cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
  47.   cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
  48.  
  49.   cmd.Parse (argc,argv);
  50.  
  51.   if (verbose)
  52.     {
  53.       LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  54.       LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
  55.     }
  56.  
  57.   nCsma = nCsma == 0 ? 1 : nCsma;
  58.  
  59.   NodeContainer p2pNodes;
  60.   p2pNodes.Create (2);
  61.  
  62.   NodeContainer csmaNodes;
  63.   csmaNodes.Add (p2pNodes.Get (1));
  64.   csmaNodes.Create (nCsma);
  65.  
  66.   NodeContainer p2pNodes2;
  67.   p2pNodes2.Add (csmaNodes.Get (5));
  68.   p2pNodes2.Create (1);
  69.  
  70.   PointToPointHelper pointToPoint;
  71.   pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("100Mbps"));
  72.   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
  73.  
  74.   NetDeviceContainer p2pDevices;
  75.   p2pDevices = pointToPoint.Install (p2pNodes);
  76.  
  77.   CsmaHelper csma;
  78.   csma.SetChannelAttribute ("DataRate", StringValue ("500Mbps"));
  79.   csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
  80.  
  81.   NetDeviceContainer csmaDevices;
  82.   csmaDevices = csma.Install (csmaNodes);
  83.  
  84.   PointToPointHelper pointToPoint2;
  85.   pointToPoint2.SetDeviceAttribute ("DataRate", StringValue ("100Mbps"));
  86.   pointToPoint2.SetChannelAttribute ("Delay", StringValue ("2ms"));
  87.  
  88.   NetDeviceContainer p2pDevices2;
  89.   p2pDevices2 = pointToPoint2.Install (p2pNodes2);
  90.  
  91.   InternetStackHelper stack;
  92.   stack.Install (p2pNodes.Get (0));
  93.   stack.Install (p2pNodes2.Get (1));
  94.   stack.Install (csmaNodes);
  95.  
  96.   Ipv4AddressHelper address;
  97.   address.SetBase ("10.1.5.0", "255.255.255.0");
  98.   Ipv4InterfaceContainer p2pInterfaces;
  99.   p2pInterfaces = address.Assign (p2pDevices);
  100.  
  101.   address.SetBase ("10.1.4.0", "255.255.255.0");
  102.   Ipv4InterfaceContainer csmaInterfaces;
  103.   csmaInterfaces = address.Assign (csmaDevices);
  104.  
  105.   address.SetBase ("10.1.3.0", "255.255.255.0");
  106.   Ipv4InterfaceContainer p2pInterfaces2;
  107.   p2pInterfaces2 = address.Assign (p2pDevices2);
  108.  
  109.   UdpEchoServerHelper echoServer (9);
  110.  
  111.   ApplicationContainer serverApps = echoServer.Install (p2pNodes2.Get (1));
  112.   serverApps.Start (Seconds (1.0));
  113.   serverApps.Stop (Seconds (201.0));
  114.  
  115.   UdpEchoClientHelper echoClient (p2pInterfaces2.GetAddress (1), 9);
  116.   echoClient.SetAttribute ("MaxPackets", UintegerValue (200));
  117.   echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  118.   echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
  119.  
  120.   ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));
  121.   clientApps.Start (Seconds (2.0));
  122.   clientApps.Stop (Seconds (201.0));
  123.  
  124.   Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  125.  
  126.   pointToPoint.EnablePcapAll ("secondP2P");
  127.   csma.EnablePcapAll ("secondCSMA");
  128.   pointToPoint2.EnablePcapAll ("secondP2P");
  129.  
  130.   AnimationInterface anim ("second.xml");
  131.   anim.SetConstantPosition(p2pNodes.Get(0), 10.0, 25.0);
  132.   anim.SetConstantPosition(p2pNodes.Get(1), 30.0, 25.0);
  133.   anim.SetConstantPosition(csmaNodes.Get(1), 50.0, 25.0);
  134.   anim.SetConstantPosition(csmaNodes.Get(2), 70.0, 25.0);
  135.   anim.SetConstantPosition(csmaNodes.Get(3), 30.0, 45.0);
  136.   anim.SetConstantPosition(csmaNodes.Get(4), 50.0, 45.0);
  137.   anim.SetConstantPosition(csmaNodes.Get(5), 70.0, 45.0);
  138.   anim.SetConstantPosition(p2pNodes2.Get(1), 70.0, 65.0);
  139.   NS_LOG_INFO ("Run Simulation.");
  140.  
  141.   Simulator::Run ();
  142.   Simulator::Destroy ();
  143.   return 0;
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement