简体   繁体   中英

ns3 How to create a custom point to point topology

Creating a point to point topology involves two nodes and the code generating such topology is like this:

// Default Network Topology
//
//       10.1.1.0
// n0 -------------- n1
//    point-to-point
//
 
using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int
main (int argc, char *argv[])
{
  CommandLine cmd (__FILE__);
  cmd.Parse (argc, 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;
}

This code just creates two point to point nodes. But how we can create a point to point topology with more than 2 nodes like this image:

在此处输入图像描述

There areseveral layout helpers for PointToPointNetDevices , but none of them perfectly fit what you're trying to do.

The most straightforward way to achieve the topology you want is to create the nodes, and manually install PointToPointNetDevices. Something along the lines of

NodeContainer nodes(7);
PointToPointHelper pointToPoint;
NetDeviceContainer devices;

devices.Add(pointToPoint.Install(nodes.Get(0), nodes.Get(1)));
devices.Add(pointToPoint.Install(nodes.Get(0), nodes.Get(2)));
devices.Add(pointToPoint.Install(nodes.Get(1), nodes.Get(3)));
devices.Add(pointToPoint.Install(nodes.Get(2), nodes.Get(3)));
devices.Add(pointToPoint.Install(nodes.Get(2), nodes.Get(4)));
devices.Add(pointToPoint.Install(nodes.Get(2), nodes.Get(6)));
devices.Add(pointToPoint.Install(nodes.Get(4), nodes.Get(6)));
devices.Add(pointToPoint.Install(nodes.Get(5), nodes.Get(5)));

// the rest of your script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM