How to Implement CGSR Protocol in NS2

 

To implement the Clusterhead Gateway Switch Routing (CGSR) protocol within NS2 has needs to encompass mimicking the hierarchical clustering and routing mechanisms which the CGSR uses. This protocol is a routing protocol that created for the mobile ad hoc networks (MANETs). It is according to the DSDV (Destination-Sequenced Distance Vector) routing protocol, with the more cluster-based architecture. Below, we provide step by step process to executing CGSR in NS2:

Step-by-Step Implementation:

Step 1: Understand CGSR

The CGSR protocol is splits the network into clusters, with every cluster having a clusterhead that handles the communication in the cluster and across clusters through the gateways. Routing in CGSR is complete over these cluster heads that maintain routing tables and manage communication among the clusters.

Step 2: Set Up NS2

Make sure that NS2 is installed on the computer. If required, we apply the patches or expand the NS2 to append support for CGSR features. This protocol is not natively consoled in NS2, thus we may want to change or expand existing code.

Step 3: Create the CGSR Agent Class (C++)

We may want to make a new C++ class in NS2 which executes the CGSR, or we can adjust existing DSDV code because the protocol CGSR is according to the DSDV.

Given below is a simple structure:

#include <agent.h>

#include <packet.h>

#include <trace.h>

#include <address.h>

#include <dsdv/dsdv.h>  // Include DSDV base class for CGSR

class CGSRAgent : public DSDV_Agent {

public:

CGSRAgent();

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

void clusterFormation();

void handleClusterheadChange();

protected:

int clusterhead_;  // ID of the current clusterhead

// Other CGSR-specific data structures

};

// Constructor

CGSRAgent::CGSRAgent() : DSDV_Agent() {

// Initialization code here

}

// Packet reception

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

// Handle incoming packets, using CGSR logic for routing

// Forward packets based on clusterhead routing

}

// Cluster formation

void CGSRAgent::clusterFormation() {

// Implement the logic for forming clusters and selecting clusterheads

}

// Handle clusterhead change

void CGSRAgent::handleClusterheadChange() {

// Logic to handle changes in clusterhead due to mobility or network changes

}

Step 4: Integrate the CGSR Agent into NS2

  1. Modify the Makefile: Append the new CGSR class to the NS2 Makefile so as to it acquires compiled with the rest of the simulator.
  2. Recompile NS2:

make clean

make

Step 5: Create a Tcl Script to Simulate CGSR

When the CGSR protocol is executed and compiled, make a Tcl script to replicate a network using CGSR.

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)     10

set val(x)      1000

set val(y)      1000

set val(stop)   100.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 CGSR \

-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 and mobility model

$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 6: Run the Simulation

  1. We can save the Tcl script such as cgsr_example.tcl.
  2. Open a terminal and navigate to the directory in which we can saved the Tcl script.
  3. Run the simulation using the below command:

ns cgsr_example.tcl

Step 7: Analyse the Results

We can use the trace files and the network animator (NAM) to examine the performance of the CGSR protocol then concentration on parameters like cluster stability, packet delivery ratio, end-to-end delay, and routing overhead.

Additional Considerations

  • Cluster Stability: Observe how stable clusters are over time, particularly in high-mobility scenarios.
  • Clusterhead Selection: Execute and investigate various procedures for choosing clusterheads to enhance the network performance.
  • Performance Metrics: Compare CGSR’s performance with various protocols such as DSDV and AODV within several network scenarios.

A stepwise method was executed for CGSR protocol and it was implemented and analysed using the simulation tool ns2. We will be provided more details related this topic if required. We provide assistance with the comparative analysis of your network project. Receive customized support from us for the implementation of the CGSR Protocol in NS2, as we will guide you through every step of the process. Additionally, obtain the most suitable project topics from ns2project.com.