Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement Mixed Topology in ns3

To implement a mixed topology in ns3, we have to combine involves multiple types of network topologies in a single simulation, such as star, mesh, and point-to-point links.

The following instructions will guide on how to implement this kind of mixed topology in ns3.

Step-by-step to implement Mixed Topology in ns3

Step 1: Install ns3

Make sure ns3 is installed on the system.

Step 2: Create a New Simulation Script

Create a new C++ script for the simulation process.

Step 3: Include Necessary Headers

In the script, include the necessary ns3 headers:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/applications-module.h”

Step 4: Set Up the Mixed Topology

Below an example given for setting up a mixed topology with a central node, peripheral nodes in a star configuration, and additional mesh and point-to-point links:

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“MixedTopology”);

int main (int argc, char *argv[])

{

// Configure command line parameters

CommandLine cmd;

cmd.Parse (argc, argv);

// Create nodes

NodeContainer centralNode;

centralNode.Create (1); // Create the central node

NodeContainer starNodes;

starNodes.Create (4); // Create 4 peripheral nodes in a star topology

NodeContainer meshNodes;

meshNodes.Create (4); // Create 4 nodes for the mesh network

NodeContainer allNodes = NodeContainer (centralNode, starNodes, meshNodes);

// Create point-to-point links for the star topology

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“1Gbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

 

NetDeviceContainer starDevices;

Ipv4AddressHelper starAddress;

Ipv4InterfaceContainer starInterfaces;

for (uint32_t i = 0; i < starNodes.GetN (); ++i)

{

NodeContainer pair (centralNode.Get (0), starNodes.Get (i));

NetDeviceContainer devicePair = pointToPoint.Install (pair);

starDevices.Add (devicePair);

std::ostringstream subnet;

subnet << “10.1.” << i + 1 << “.0”;

starAddress.SetBase (subnet.str ().c_str (), “255.255.255.0”);

starInterfaces.Add (starAddress.Assign (devicePair));

}

// Create mesh links

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

wifiPhy.SetChannel (wifiChannel.Create ());

MeshHelper mesh;

mesh.SetStackInstaller (“ns3::Dot11sStack”);

mesh.SetSpreadInterfaceChannels (MeshHelper::SPREAD_CHANNELS);

mesh.SetMacType (“RandomStart”, TimeValue (Seconds (0.1)));

mesh.SetNumberOfInterfaces (1);

NetDeviceContainer meshDevices = mesh.Install (wifiPhy, meshNodes);

Ipv4AddressHelper meshAddress;

meshAddress.SetBase (“10.2.0.0”, “255.255.255.0”);

Ipv4InterfaceContainer meshInterfaces = meshAddress.Assign (meshDevices);

// Create additional point-to-point links between star and mesh nodes

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“1Gbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

NodeContainer p2pNodes;

p2pNodes.Add (starNodes.Get (0));

p2pNodes.Add (meshNodes.Get (0));

NetDeviceContainer p2pDevices = pointToPoint.Install (p2pNodes);

Ipv4AddressHelper p2pAddress;

p2pAddress.SetBase (“10.3.0.0”, “255.255.255.0”);

Ipv4InterfaceContainer p2pInterfaces = p2pAddress.Assign (p2pDevices);

// Install the internet stack

InternetStackHelper stack;

stack.Install (allNodes);

 

// Set up mobility model (optional)

MobilityHelper mobility;

Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();

positionAlloc->Add (Vector (50.0, 50.0, 0.0)); // Central node position

positionAlloc->Add (Vector (100.0, 50.0, 0.0)); // Star node 1

positionAlloc->Add (Vector (50.0, 100.0, 0.0)); // Star node 2

positionAlloc->Add (Vector (0.0, 50.0, 0.0)); // Star node 3

positionAlloc->Add (Vector (50.0, 0.0, 0.0)); // Star node 4

positionAlloc->Add (Vector (150.0, 50.0, 0.0)); // Mesh node 1

positionAlloc->Add (Vector (150.0, 100.0, 0.0)); // Mesh node 2

positionAlloc->Add (Vector (150.0, 0.0, 0.0)); // Mesh node 3

positionAlloc->Add (Vector (200.0, 50.0, 0.0)); // Mesh node 4

mobility.SetPositionAllocator (positionAlloc);

mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);

mobility.Install (allNodes);

// Set up applications (e.g., UDP echo server and client)

uint16_t port = 9; // Port number for applications

// Install UDP Echo Server on the central node

UdpEchoServerHelper echoServer (port);

ApplicationContainer serverApps = echoServer.Install (centralNode.Get (0));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

// Install UDP Echo Clients on star and mesh nodes to communicate with the central node

for (uint32_t i = 0; i < starNodes.GetN (); ++i)

{

UdpEchoClientHelper echoClient (starInterfaces.GetAddress (0), port);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (starNodes.Get (i));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

}

for (uint32_t i = 0; i < meshNodes.GetN (); ++i)

{

UdpEchoClientHelper echoClient (starInterfaces.GetAddress (0), port);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

 

ApplicationContainer clientApps = echoClient.Install (meshNodes.Get (i));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

}

// Run simulation

Simulator::Stop (Seconds (10.0));

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 5: Build and Run the Simulation

  1. Save the script as mixed-topology.cc.
  2. Build the script using waf:

./waf configure –enable-examples

./waf build

  1. Run the simulation:

./waf –run scratch/mixed-topology

Explanation of the Script

  • Node Creation: Creates nodes for the central hub, peripheral nodes in a star topology, and nodes in a mesh network.
  • Star Topology: Configures point-to-point links to connect the central node to each peripheral node.
  • Mesh Topology: Configures mesh links using WiFi.
  • Point-to-Point Links: Adds additional point-to-point links between some star nodes and mesh nodes.
  • Internet Stack: Installs the internet stack on all nodes.
  • IP Addressing: Assigns IP addresses to the different parts of the network using different subnets.
  • Mobility Model: Sets the position of nodes in a specific layout using ListPositionAllocator and ConstantPositionMobilityModel.
  • Applications: Sets up a UDP echo server on the central node and UDP echo clients on the peripheral and mesh nodes to demonstrate communication between nodes.

Finally, we all get to know how to implement mixed topology in ns3 by combining multiple types of topologies and here we had used C++ script for the simulation process.

Kindly provide us with the details of your parameters, and we can support you in comparing networking mixed topology in ns3.