How to Implement GPSR Protocol in NS2

 

To implement the GPSR (Greedy Perimeter Stateless Routing) protocol in NS2, we have to either find the available implementation or generate a new one from the scratch. These protocol is a geographic routing protocol used in mobile ad hoc networks (MANETs) and sensor networks where it utilize the locations of routers and destination of a packet to decide forwarding decisions. Here, we deliver the steps to implementation of GPSR protocol using ns2:

Step-by-Step Implementation:

Step 1: Install NS2 and Verify Installation

Make certain that you have ns2 installed and configured properly. You can download it from the NS2 website and follow the installation instructions. Validate the installation by executing a simple simulation.

Step 2: Obtain or Implement GPSR

  1. Search for Existing Implementations:
    • Sometimes, GPSR is already executed by other researchers, and you might find a patch or module to incorporate into NS2. Look for existed GPSR executions or patches for NS2.
  2. Implementing GPSR from Scratch:
    • If you cannot find an available implementation, you will need to generate your own. This encompasses writing a new routing agent in NS2 that adheres to the GPSR protocol.

Step 3: Implement GPSR in NS2

  1. Create the GPSR Agent Class (C++)

Accomplish GPSR by creating a new C++ class that manage packet forwarding depends on the geographic locations of nodes.

Here’s a basic structure:

#include <agent.h>

#include <packet.h>

#include <trace.h>

#include <address.h>

#include <cmath>

class GPSRAgent : public Agent {

public:

GPSRAgent();

void recv(Packet* p, Handler* h);

void forwardPacket(Packet* p);

void greedyForward(Packet* p);

void perimeterForward(Packet* p);

double distance(double x1, double y1, double x2, double y2);

protected:

double nodeX_, nodeY_; // Current node’s coordinates

};

// Constructor

GPSRAgent::GPSRAgent() : Agent(PT_UDP) {

// Initialization code here

}

// Packet reception

void GPSRAgent::recv(Packet* p, Handler* h) {

// Implement GPSR packet reception and forwarding logic

forwardPacket(p);

}

// Forwarding logic

void GPSRAgent::forwardPacket(Packet* p) {

// Greedy forwarding first, fall back to perimeter mode if necessary

if (!greedyForward(p)) {

perimeterForward(p);

}

}

// Greedy Forwarding

void GPSRAgent::greedyForward(Packet* p) {

// Implement the logic to forward the packet to the neighbor closest to the destination

}

// Perimeter Forwarding

void GPSRAgent::perimeterForward(Packet* p) {

// Implement perimeter forwarding (fallback when greedy forwarding fails)

}

// Calculate distance between two points

double GPSRAgent::distance(double x1, double y1, double x2, double y2) {

return sqrt(pow(x2 – x1, 2) + pow(y2 – y1, 2));

}

  1. Integrate the GPSR Agent into NS2

We need to combine the GPSR agent into NS2, after it is executed:

  1. Modify the Makefile: Attach your new GPSR class to the NS2 Makefile so that it gets compiled with the remaining of the simulator.
  2. Recompile NS2:

make clean

make

Step 4: Create a Tcl Script to Simulate GPSR

After the GPSR protocol is executed and compiled, sets up a Tcl script to recreate a network using GPSR.

Example Tcl Script:

# Create a simulator object

set ns [new Simulator]

# Define the topology

set val(chan)   Channel/WirelessChannel

set val(prop)   Propagation/TwoRayGround

set val(netif)  Phy/WirelessPhy

set val(mac)    Mac/802_11

set val(ifq)    Queue/DropTail/PriQueue

set val(ll)     LL

set val(ant)    Antenna/OmniAntenna

set val(ifqlen) 50

set val(nn)     5

set val(x)      1000

set val(y)      1000

set val(stop)   20.0

# Initialize the topology object

set topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

# Create the God object

create-god $val(nn)

# Configure the nodes

$ns node-config -adhocRouting GPSR \

-llType $val(ll) \

-macType $val(mac) \

-ifqType $val(ifq) \

-ifqLen $val(ifqlen) \

-antType $val(ant) \

-propType $val(prop) \

-phyType $val(netif) \

-channelType $val(chan) \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace ON \

-movementTrace ON

# Create nodes

for {set i 0} {$i < $val(nn)} {incr i} {

set node_($i) [$ns node]

}

# Define node positions

$node_(0) set X_ 100.0; $node_(0) set Y_ 200.0

$node_(1) set X_ 300.0; $node_(1) set Y_ 400.0

$node_(2) set X_ 500.0; $node_(2) set Y_ 600.0

$node_(3) set X_ 700.0; $node_(3) set Y_ 800.0

$node_(4) set X_ 900.0; $node_(4) set Y_ 1000.0

# Define traffic sources

set udp [new Agent/UDP]

$ns attach-agent $node_(0) $udp

set null [new Agent/Null]

$ns attach-agent $node_(4) $null

$ns connect $udp $null

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set packetSize_ 512

$cbr set interval_ 0.1

$cbr start

# Simulation end

$ns at $val(stop) “stop”

$ns at $val(stop) “$ns nam-end-wireless $val(stop)”

$ns at $val(stop) “exit 0”

proc stop {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

}

# Run the simulation

$ns run

Step 5: Run the Simulation

  1. Save the Tcl script (gpsr_example.tcl).
  2. Open a terminal and navigate to the directory where you saved the Tcl script.
  3. Use the given command to execute the simulation:

ns gpsr_example.tcl

Step 6: Analyze the Results

Assess the performance of the GPSR protocol, aiming on metrics contains packet delivery ratio, end-to-end delay and routing overhead by using trace files and the network animator (NAM).

Additional Considerations

  • Testing in Different Scenarios: Analyze its performance by examining the GPSR under different mobility models and network densities.
  • Using Extensions or Alternatives: If generating GPSR from scratch is too extreme, consider using available patches or switching to NS3, which might have more extensive support for latest protocols.

Overall, we showcased more insights about how to implement GPSR Protocol including examples and how to evaluate it using network simulator tool (ns2). If you need any extra projects information of the GPSR protocol, we will provide them too. Get in touch with us for the implementation of the GPSR Protocol in NS2, you can visit ns2project.com, where we provide excellent project ideas and topics. We also offer performance analysis services for your project.