How to implement Ad Hoc Protocols in NS2

 

To implement the ad hoc routing protocols within NS2 (Network Simulator 2) that has needs to contain set up the network simulation to use one of the obtainable ad hoc routing protocols, like AODV (Ad hoc On-Demand Distance Vector), DSR (Dynamic Source Routing), or DSDV (Destination-Sequenced Distance Vector). The simulation tool NS2 gives built-in aid for numerous of these protocols that creates it easier to mimic ad hoc networks.

Step-by-Step Guide to Implement Ad Hoc Protocols in NS2

Step 1: Install NS2

Make sure that NS2 is installed on the computer. We can download it from the NS2 website and pursue the installation instructions particular to the operating system.

Step 2: Choose an Ad Hoc Protocol

The simulation tool NS2 supports various ad hoc routing protocols out of the box, like:

  • AODV (Ad hoc On-Demand Distance Vector)
  • DSR (Dynamic Source Routing)
  • DSDV (Destination-Sequenced Distance Vector)

We can select the protocol that best suits the simulation requires.

Step 3: Create a Simulation Script

We can want to make a Tcl script that set up the network to use the chosen ad hoc routing protocol.

Example: Implementing AODV in NS2

  1. Create a new Tcl script: Open a text editor and make a new file, for instance, aodv_example.tcl.
  2. Set up the simulation environment: Describe the simulator, set up the network topology, and configure the parameters particular to the simulation.

# Create a simulator object

set ns [new Simulator]

# Define options for the simulation

set val(chan)   Channel/WirelessChannel    ;# Channel type

set val(prop)   Propagation/TwoRayGround   ;# Propagation model

set val(netif)  Phy/WirelessPhy            ;# Network interface type

set val(mac)    Mac/802_11                 ;# MAC type

set val(ifq)    Queue/DropTail/PriQueue    ;# Interface Queue type

set val(ll)     LL                         ;# Link layer type

set val(ant)    Antenna/OmniAntenna        ;# Antenna type

set val(ifqlen) 50                         ;# Max packet in ifq

set val(nn)     10                         ;# Number of mobile nodes

set val(rp)     AODV                       ;# Routing Protocol (AODV)

set val(x)      500                        ;# X dimension of topography

set val(y)      500                        ;# Y dimension of topography

set val(stop)   10.0                       ;# Simulation time

# 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 $val(rp) \

-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]

$node_($i) random-motion 0

}

# Define node movement (Optional for mobile nodes)

$node_(0) set X_ 50.0

$node_(0) set Y_ 50.0

$node_(0) set Z_ 0.0

$node_(1) set X_ 100.0

$node_(1) set Y_ 100.0

$node_(1) set Z_ 0.0

# (Add more movement patterns or use mobility models if needed)

  1. Setup traffic sources:

# Setup a UDP agent and attach it to node 0

set udp [new Agent/UDP]

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

# Setup CBR (Constant Bit Rate) application to generate traffic

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set packetSize_ 512

$cbr set interval_ 0.1

$cbr start

# Setup a Null agent (sink) on node 1

set null [new Agent/Null]

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

# Connect the agents

$ns connect $udp $null

  1. Setup simulation end:

# Define simulation end time

$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

Example: Implementing DSR in NS2

If we need to use DSR rather than AOD, we can simply modify the routing protocol setting:

set val(rp)     DSR  ;# Use DSR as the routing protocol

Example: Implementing DSDV in NS2

For DSDV, we could set up it similarly:

set val(rp)     DSDV ;# Use DSDV as the routing protocol

Step 4: Run the Simulation

  1. We can the Tcl script like aodv_example.tcl or the desired protocol script.
  2. Open a terminal and transverse to the directory in which we saved the Tcl script.
  3. Run the simulation using the below command:

ns aodv_example.tcl

This command will produce trace files and optionally a network animation file if enabled in the script.

Step 5: Analyse the Results

We can use the trace files and network animator (NAM) to evaluate the performance of the ad hoc protocol, concentrating on parameters like route discovery time, packet delivery ratio, and network overhead.

Step 6: Visualize the Results (Optional)

If we have permitted the network animator (NAM) in the script, we can visualize the simulation:

nam aodv_example.nam

It will open the NAM window, in which we can observe the network topology and the behaviour of the ad hoc routing protocol when the simulation.

Additional Considerations

  • Mobility: Investigate the protocol under numerous mobility scenarios to observe how it manages dynamic topologies.
  • Network Size: Change the number of nodes to monitor how the protocol scales.
  • Traffic Patterns: Test with various traffic patterns like CBR, FTP, and TCP to know how the protocol executes under various conditions.
  • Performance Metrics: Calculate and compare the performance of various ad hoc protocols such as latency, packet delivery ratio, routing overhead, and energy consumption.

In the above approach established the comprehensive procedure to execute and enforce the ad hoc protocols in the simulation ns2. More insights concerning these protocols will be provided.

To successfully implement Ad Hoc Protocols in NS2, we will provide comprehensive guidance at every stage of the process. Additionally, you can find the most suitable project topics at ns2project.com, where we will assist you in selecting the best project ideas.