How to Implement On Demand Protocol in NS2
To implement on-demand routing protocols within NS2 (Network Simulator 2) that has includes setup the network simulation to use one of the obtainable on-demand routing protocols, like AODV (Ad hoc On-Demand Distance Vector) or DSR (Dynamic Source Routing). This protocols ascertain routes only when required, that helps to decrease the overhead compared to the proactive routing protocols. Below is a simplest method to implementing an on-demand protocol like AODV or DSR in NS2:
Step-by-Step Implementation:
Step 1: Install NS2
Make sure that NS2 is installed on the computer. Then, we can download it from the NS2 webpage and follow the installation instructions particular to the operating system.
Step 2: Choose an On-Demand Protocol
The simulator NS2 that supports numerous on-demand routing protocols, containing:
- AODV (Ad hoc On-Demand Distance Vector)
- DSR (Dynamic Source Routing)
Select the protocol that we need to simulate.
Step 3: Create a Simulation Script
Make a Tcl script to set up the network and mimic on-demand routing using the selected protocol.
Example: Implementing AODV in NS2
- Create a new Tcl script: Here, open a text editor and make a new file, for specimen, aodv_on_demand_example.tcl.
- Set up the simulation environment: State that the simulator, set up the network topology, and configure the metrics exact 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 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_ 150.0
$node_(1) set Y_ 100.0
$node_(1) set Z_ 0.0
# (Add more movement patterns or use mobility models if needed)
- 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
- 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 AODV, then we can simply modify the routing protocol setting:
set val(rp) DSR ;# Use DSR as the routing protocol
Step 4: Run the Simulation
- We save the Tcl script such as aodv_on_demand_example.tcl or the desired protocol script.
- Open a terminal and navigate to the directory in which we can be saved the Tcl script.
- Run the simulation using the below command:
ns aodv_on_demand_example.tcl
The above command will make trace files and optionally a network animation file if permitted in the script.
Step 5: Analyse the Results
We can use the trace files and network animator (NAM) to estimate the performance of the on-demand protocol, concentrating on the parameters like route discovery time, packet delivery ratio, and network overhead.
Step 6: Visualize the Results (Optional)
If we have allowed the network animator (NAM) in the script, then we can visualize the simulation:
nam aodv_on_demand_example.nam
It will open the NAM window in which we can observe the network topology and the behaviour of the on-demand routing protocol when the simulation.
Additional Considerations
- Mobility: Investigate the protocol under numerous mobility scenarios to observe how it manages the dynamic topologies.
- Network Size: Change the number of nodes to monitor on how the protocol scales.
- Traffic Patterns: Analyse with various traffic patterns like CBR, FTP, and TCP to know how the protocol acts under various conditions.
- Performance Metrics: Compute and compare the performance of various on-demand protocols such as delay, packet delivery ratio, routing overhead, and energy consumption.
In the above informations, we had explained regarding On Demand Protocol that contains simple approaches, and some instances are helps to implement this protocol using ns2 tool. Also we will provide additional details about this topic in various tools.
Ns2project.com offer comprehensive performance analysis tailored to your needs. Our expertise in Demand Protocol within NS2 implementation ensures you receive exceptional results, supported by innovative research concepts.