To implement the secondary users routing in ns3 has encompasses to emulate the scenarios where the secondary users like cognitive radio network can route enthusiastically in the traffic that is based on the accessible channels and the network conditions. The given below is the detailed procedure on how to setup the secondary user routing in ns3:
Step-by-Step Implementation:
Step 1: Install ns3
Make sure ns3 is installed in the computer.
Step 2: Set Up the Simulation Environment
Create a new simulation script or modify an existing one. This script will define the network topology, nodes, mobility models, and communication protocols.
Step 3: Define Network Topology
Make nodes and describe the network topology that concentrates on the setup of primary and secondary users.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/wifi-module.h”
#include “ns3/mobility-module.h”
#include “ns3/aodv-helper.h”
#include “ns3/applications-module.h”
#include “ns3/flow-monitor-module.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“SecondaryUsersRoutingExample”);
int main (int argc, char *argv[])
{
// Enable logging
LogComponentEnable (“SecondaryUsersRoutingExample”, LOG_LEVEL_INFO);
// Create nodes
NodeContainer primaryNodes;
primaryNodes.Create (2); // Primary users
NodeContainer secondaryNodes;
secondaryNodes.Create (5); // Secondary users
// Set up Wi-Fi for secondary users
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211a);
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiMacHelper wifiMac;
wifiMac.SetType (“ns3::AdhocWifiMac”);
NetDeviceContainer secondaryDevices;
secondaryDevices = wifi.Install (wifiPhy, wifiMac, secondaryNodes);
// Install Internet stack for secondary users
InternetStackHelper internet;
AodvHelper aodv;
internet.SetRoutingHelper (aodv);
internet.Install (secondaryNodes);
// Assign IP addresses to secondary users
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer secondaryInterfaces = address.Assign (secondaryDevices);
// Set up mobility model for secondary users
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (10.0),
“DeltaY”, DoubleValue (10.0),
“GridWidth”, UintegerValue (3),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,
“Bounds”, RectangleValue (Rectangle (0, 50, 0, 50)));
mobility.Install (secondaryNodes);
// Set up Wi-Fi for primary users
WifiHelper wifiPrimary;
wifiPrimary.SetStandard (WIFI_PHY_STANDARD_80211b);
YansWifiPhyHelper wifiPhyPrimary = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannelPrimary = YansWifiChannelHelper::Default ();
wifiPhyPrimary.SetChannel (wifiChannelPrimary.Create ());
WifiMacHelper wifiMacPrimary;
wifiMacPrimary.SetType (“ns3::AdhocWifiMac”);
NetDeviceContainer primaryDevices;
primaryDevices = wifiPrimary.Install (wifiPhyPrimary, wifiMacPrimary, primaryNodes);
// Install Internet stack for primary users
internet.Install (primaryNodes);
// Assign IP addresses to primary users
address.SetBase (“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer primaryInterfaces = address.Assign (primaryDevices);
// Set up mobility model for primary users
mobility.Install (primaryNodes);
// Install and start applications on secondary nodes
uint16_t port = 8080;
OnOffHelper onOff (“ns3::UdpSocketFactory”, InetSocketAddress (secondaryInterfaces.GetAddress (1), port));
onOff.SetAttribute (“DataRate”, DataRateValue (DataRate (“1Mbps”)));
onOff.SetAttribute (“PacketSize”, UintegerValue (1024));
onOff.SetAttribute (“OnTime”, StringValue (“ns3::ConstantRandomVariable[Constant=1]”));
onOff.SetAttribute (“OffTime”, StringValue (“ns3::ConstantRandomVariable[Constant=0]”));
ApplicationContainer app = onOff.Install (secondaryNodes.Get (0));
app.Start (Seconds (1.0));
app.Stop (Seconds (10.0));
PacketSinkHelper sink (“ns3::UdpSocketFactory”, InetSocketAddress (Ipv4Address::GetAny (), port));
app = sink.Install (secondaryNodes.Get (1));
app.Start (Seconds (1.0));
app.Stop (Seconds (10.0));
// Set up FlowMonitor
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
// Run the simulation
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
// Print statistics
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
NS_LOG_UNCOND (“Flow ” << i->first << ” (” << t.sourceAddress << ” -> ” << t.destinationAddress << “)”);
NS_LOG_UNCOND (” Tx Packets: ” << i->second.txPackets);
NS_LOG_UNCOND (” Tx Bytes: ” << i->second.txBytes);
NS_LOG_UNCOND (” Rx Packets: ” << i->second.rxPackets);
NS_LOG_UNCOND (” Rx Bytes: ” << i->second.rxBytes);
NS_LOG_UNCOND (” Throughput: ” << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds () – i->second.timeFirstTxPacket.GetSeconds ()) / 1024 / 1024 << ” Mbps”);
}
// Clean up
Simulator::Destroy ();
return 0;
}
Step 4: Set Up Mobility Models
In the example, the GridPositionAllocator is used to place nodes in a grid layout initially, and the RandomWalk2dMobilityModel is used for nodes to simulate random movement within a bounded area.
Step 5: Install and Start Applications
We set up an OnOff application to simulate traffic from a secondary user to another secondary user. The server receives packets sent by the client.
Step 6: Run the Simulation
Compile and run your simulation script to see the effect of secondary users’ routing on network performance. The output will include statistics such as the number of packets transmitted and received, throughput, and any packet loss.
At the conclusion we clearly learned about how to setup the simulation and how to implement the secondary user routing with the help of ns3 and further we support and provide the information regarding secondary user routing.
Explore project ideas focusing on Secondary Users routing within the ns3 tool. Delve into implementation strategies with in-depth discussions for your project. If you’re working on a cognitive radio network, feel free to reach out to us for further guidance.