How to Begin Implement Network Traveling Path Length in NS3
To implement and examine the Network Traveling Path Length in NS3, we require monitoring the amount of distance travelled through packets by the network and it change the source to destination. This parameter metric is mainly useful for routing environment in which the minimizing path length is complex, like as in mobile ad hoc networks (MANETs) or wireless sensor networks (WSNs).
Steps to Implement Network Traveling Path Length in NS3
- Understand Traveling Path Length
- Definition: The total physical or logical distance the traveling path length is traversed through a packet with all intermediate nodes from source to destination.
- Types:
- Physical Distance: Amount of distance in meters according to their node positions.
- Hop Count: Number of intermediate nodes is navigated such as logical path length.
- Set up the NS3 Environment
- Install NS3:
- Assure the NS3 is installed. Download it from the NS3 website.
- Choose a Network Type:
- Use wireless networks for instance Adhoc, Wifi or routing protocols for sample AODV, DSR.
- Enable Routing Protocols:
- Use a dynamic routing protocol for introduce the routes and monitor the path lengths.
- Track Path Length
- Calculate Physical Path Length:
- Use the MobilityModel to get the placed nodes and compute the Euclidean distance among hops.
- Track Logical Path Length:
- Using the routing protocols for amount the number of hops tracing or sending logic.
- Implement Path Length Calculation
Follow for below is an sample script for implement the traveling path length in a wireless ad-hoc network using the AODV routing protocol.
Example Script: Network Traveling Path Length in NS3
#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/applications-module.h”
#include “ns3/aodv-module.h”
using namespace ns3;
class PathLengthCalculator {
public:
PathLengthCalculator() : m_totalPathLength(0), m_totalPackets(0) {}
void TrackPath(Ptr<const Packet> packet, Ptr<Ipv4Route> route) {
double pathLength = 0.0;
Ptr<Ipv4> ipv4 = route->GetOutputDevice()->GetNode()->GetObject<Ipv4>();
for (uint32_t i = 0; i < route->GetNAddresses() – 1; ++i) {
Ipv4Address srcAddress = route->GetAddress(i);
Ipv4Address dstAddress = route->GetAddress(i + 1);
Ptr<MobilityModel> srcMobility = GetNodeMobility(srcAddress, ipv4);
Ptr<MobilityModel> dstMobility = GetNodeMobility(dstAddress, ipv4);
if (srcMobility && dstMobility) {
double distance = srcMobility->GetDistanceFrom(dstMobility);
pathLength += distance;
}
}
m_totalPathLength += pathLength;
m_totalPackets++;
NS_LOG_UNCOND(“Packet Path Length: ” << pathLength << ” meters”);
}
void LogAveragePathLength() {
if (m_totalPackets > 0) {
NS_LOG_UNCOND(“Average Path Length: ” << m_totalPathLength / m_totalPackets << ” meters”);
} else {
NS_LOG_UNCOND(“No packets transmitted.”);
}
}
private:
double m_totalPathLength;
uint32_t m_totalPackets;
Ptr<MobilityModel> GetNodeMobility(Ipv4Address address, Ptr<Ipv4> ipv4) {
for (uint32_t i = 0; i < ipv4->GetNInterfaces(); ++i) {
if (ipv4->GetAddress(i, 0).GetLocal() == address) {
return ipv4->GetNetDevice(i)->GetNode()->GetObject<MobilityModel>();
}
}
return nullptr;
}
};
int main(int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse(argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create(10); // 10 nodes in the network
// Configure WiFi
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();
wifiPhy.SetChannel(wifiChannel.Create());
WifiHelper wifi;
wifi.SetStandard(WIFI_PHY_STANDARD_80211g);
WifiMacHelper mac;
mac.SetType(“ns3::AdhocWifiMac”);
NetDeviceContainer devices = wifi.Install(wifiPhy, mac, nodes);
// Configure mobility
MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::RandomRectanglePositionAllocator”,
“X”, StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”),
“Y”, StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”));
mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);
mobility.Install(nodes);
// Install Internet stack and AODV routing
AodvHelper aodv;
InternetStackHelper internet;
internet.SetRoutingHelper(aodv);
internet.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign(devices);
// Configure applications
uint16_t port = 9;
UdpEchoServerHelper server(port);
ApplicationContainer serverApp = server.Install(nodes.Get(9)); // Last node as server
serverApp.Start(Seconds(1.0));
serverApp.Stop(Seconds(10.0));
UdpEchoClientHelper client(interfaces.GetAddress(9), port);
client.SetAttribute(“MaxPackets”, UintegerValue(100));
client.SetAttribute(“Interval”, TimeValue(MilliSeconds(100)));
client.SetAttribute(“PacketSize”, UintegerValue(1024));
ApplicationContainer clientApp = client.Install(nodes.Get(0)); // First node as client
clientApp.Start(Seconds(2.0));
clientApp.Stop(Seconds(10.0));
// Track path length
PathLengthCalculator pathLengthCalculator;
Config::ConnectWithoutContext(“/NodeList/*/$ns3::Ipv4L3Protocol/Tx”, MakeCallback(&PathLengthCalculator::TrackPath, &pathLengthCalculator));
// Schedule average path length logging
Simulator::Schedule(Seconds(10.0), &PathLengthCalculator::LogAveragePathLength, &pathLengthCalculator);
Simulator::Run();
Simulator::Destroy();
return 0;
}
Explanation of the Script
- Routing Protocol:
- AODV is used to dynamically launch the routes for estimate the path lengths.
- Physical Path Length Calculation:
- Uses the MobilityModel for estimate the Euclidean distance among nodes along the route.
- Path Length Tracking:
- Logs the amount of path length for every packet and computes the average path length at the final for the replication.
- Mobility Configuration:
- Nodes are positioned arbitrarily, in this sample by fixed places.
Steps to Run the Simulation
- Make a replication of script:
./waf –run “traveling-path-length”
- Follow the length of logs in packet path lengths and average path length.
Here, we collects the essential information and delivers on how the network traveling path length will perform in ns3 simulation and we help to provide further information about how the network traveling path length will adapt in different environments.