How to Implement Greedy Perimeter Stateless Routing in NS2
To implement the Greedy Perimeter Stateless Routing (GPSR) in ns2, which is a geographic routing protocol used in Wireless Sensor Networks (WSNs) and Mobile ad-hoc Networks (MANETs). Due to ns2 lacks support for GPSR, we can either install the patch that will be attached into GPSR mechanisms or manually generating simulation script to approximate GPSR activities.
Follow the step-by-step guide to implementing GPSR (Greedy Perimeter Stateless Routing) in NS2:
Step-by-Step Implementation:
- Install and Set Up NS2
Make sure to install the ns2 on your computer and check if it is properly configured based on your operating system.
- Install GPSR Patch
We need to install a GPSR patch because ns2 does not support GPSR natively. Few versions of NS2 have a community-designed patch that executes GPSR. We can download the patch from the proper source (like research groups or open repositories like GitHub) and attach it to the ns2 simulation.
Install GPSR Patch:
- Download the GPSR patch from a reliable source.
- Include the patch by duplicating the files to the NS2 directory and recompiling NS2.
- Recompile NS2 to incorporate GPSR into your simulation environment:
cd ns-allinone-2.x/ns-2.x
./configure
make clean
make
- Modify Tcl Script for GPSR
You can write a Tcl script that replicates GPSR routing, once the patch is installed. The script should state a mobile ad-hoc network (MANET) or wireless sensor network where nodes are allocated in a plane, and routing decisions are depends on the geographic locations of the nodes.
Follow the below sample of Tcl script that executes a simple network using GPSR:
Example of GPSR Tcl Script:
# Create a new NS2 simulator instance
set ns [new Simulator]
# Open the network topology (trace file) and NAM visualization
set tracefile [open gpsr_trace.tr w]
set namfile [open gpsr_simulation.nam w]
$ns trace-all $tracefile
$ns namtrace-all-wireless $namfile 1000 1000 0.1
# Set up the topography object (define the simulation space)
set topo [new Topography]
$topo load_flatgrid 1000 1000
# Define the communication channel (wireless)
create-god 20
set chan_1_ [new Channel/WirelessChannel]
set prop_ [new Propagation/TwoRayGround]
set netif_ [new Phy/WirelessPhy]
set mac_ [new Mac/802_11]
set ifq_ [new Queue/DropTail/PriQueue]
set ll_ [new LL]
set ant_ [new Antenna/OmniAntenna]
set X_ 1000
set Y_ 1000
set Z_ 0.0
# Define the movement model (mobility)
set opt(adhocRouting) GPSR
set opt(nn) 20 ;# number of nodes
set opt(cp) “./scen/cbr-20ns-10s”
set opt(sc) “./scen/scen-20-test”
# Create mobile nodes
for {set i 0} {$i < $opt(nn)} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0
}
# Set node initial positions (Example)
$node_(0) set X_ 50
$node_(0) set Y_ 100
$node_(1) set X_ 200
$node_(1) set Y_ 300
# Add more nodes…
# Define a UDP agent and attach it to a node
set udp [new Agent/UDP]
$ns attach-agent $node_(0) $udp
# Define a traffic source (CBR)
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ 512
$cbr set interval_ 0.1
# Attach a sink agent to the destination node
set null [new Agent/Null]
$ns attach-agent $node_(1) $null
# Connect the UDP agent and the sink
$ns connect $udp $null
# Start traffic at 1 second
$ns at 1.0 “$cbr start”
# End simulation after 100 seconds
$ns at 100.0 “finish”
# Execute the simulation
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exit 0
}
$ns run
- Key Components of the GPSR Tcl Script:
- Nodes: Mobile nodes indicate the network components. Each node has its own geographic locations (X_ and Y_ coordinates).
- GPSR Routing: The routing protocol for nodes is fixed to GPSR using the line set opt(adhocRouting) GPSR. This line uses the GPSR protocol (allowed through patch).
- Traffic Source: Use CBR (Constant Bit Rate) application to produce traffic amongst nodes.
- Mobility: You can state the node movement model, which could involve random or predefined movements. This simulates a realistic environment where nodes are in motion, and GPSR needs to dynamically modify routing.
- Simulation Space: The network operates inside a defined simulation area (1000 x 1000 units).
- Run the Simulation
Once your Tcl script is ready, run the simulation using the given command:
ns gpsr_simulation.tcl
This will simulate the GPSR-based routing in your configured network topology and produce output files like gpsr_trace.tr and gpsr_simulation.nam.
- Analyze Results
You can use the result trace files and the NAM visualizer to evaluate the GPSR routing performance. The trace file gpsr_trace.tr will contain details of the packet transmissions, node movements, and routing paths picked by GPSR.
- Visualize in NAM
Use the NAM (Network Animator) to visualize the simulation:
nam gpsr_simulation.nam
- Testing Perimeter Mode
GPSR works in two modes: Greedy Mode (which forwards packets to the neighbor nearer to the destination) and Perimeter Mode (which is used when Greedy Mode fails). You can examine perimeter mode by configuring a topology where direct greedy forwarding is impossible (for instance: because of voids in the network or node failures).
- Post-Simulation Analysis
You can assess the GPSR protocol activities by monitoring:
- Packet delivery ratio
- End-to-end delay
- Routing overhead
- Path optimization (using greedy and perimeter forwarding)
This demonstration had showcased more insights about how to implement Greedy Perimeter Stateless Routing (GPSR) protocol using Simulation tool that is NS2. We also offered some examples and evaluation process of this protocol. If you need any extra information of the GPSR protocol, we will provide them too.
We possess all the tools necessary to ensure your project is completed successfully. For top-notch project guidance, reach out to us. Visit ns2project.com for expert implementation advice on Greedy Perimeter Stateless Routing in NS2.