This post will show how one can simulate AODV, DSDV and DSR by using only one tcl script. In normal procedure separate tcl script is required for each routing protocol but with this script any one of the three can be simulated. In this arguments are passed by command line. All the parameters and scenarios are same as in How to simulate and evaluate AODV DSDV routing protocols in NS-2.
# ============================================================# Define options
# ============================================================
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(x) 500 ;# X dimension of the topography
set val(y) 500 ;# Y dimension of the topography
set val(ifqlen) 50 ;# max packet in ifq
set val(seed) 0.0
set val(nn) 100 ;# how many nodes are simulated
set val(finish) 100.0 ;# simulation time
# ============================================================# Main Program
# ============================================================if { $argc != 10 } {
puts "Wrong no. of cmdline args."
puts "Usage: ns compare.tcl -sc <sc> -cp <cp> -tr <tr> -nm <nm> -rp<rp>"
exit 0
}
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
if {[string range $arg 0 0] != "-"} continue
set name [string range $arg 1 end]
set val($name) [lindex $argv [expr $i+1]]
}
set val(sc) [lindex $argv 1]
set val(cp) [lindex $argv 3]
if {$val(rp) == 1} {
set val(rp) DSR
set val(ifq) CMUPriQueue
} elseif {$val(rp) == 2} {
set val(rp) AODV
set val(ifq) Queue/DropTail/PriQueue
} else {
set val(rp) DSDV
set val(ifq) Queue/DropTail/PriQueue
}
puts $val(sc)
puts $val(cp)
puts $val(tr)
puts $val(nm)
puts $val(rp)
# Initialize Global Variables
# create simulator instance
set ns_ [new Simulator]
#Define different colors for data flows (for NAM)
$ns_ color 1 Blue
$ns_ color 2 Red
# set wireless channel, radio-model and topography objects
set topo [new Topography]
#create trace object for ns and nam
set tracefd [open $val(tr) w]
set namtrace [open $val(nm) w]
set out $val(nm)
$ns_ trace-all $tracefd
# use new trace file format
$ns_ use-newtrace
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
# Define a 'finish' procedure
proc finish {} {
global ns_ tracefd namtrace out
$ns_ flush-trace
close $tracefd
close $namtrace
#Execute NAM on the trace file
exec nam $out &
exit 0
}
$topo load_flatgrid $val(x) $val(y)
# Create God
set god_ [create-god $val(nn)]
# define how node should be created
#global node setting
$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 the specified number of nodes [$val(nn)] and "attach" them to the channel.
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0 ;# disable random motion
}
# Define node movement model
puts "Loading connection pattern..."
source $val(sc)
# Define traffic model
puts "Loading traffic file..."
source $val(cp)
# Define node initial position in nam
for {set i 0} {$i < $val(nn)} {incr i} {
# 20 defines the node size in nam, must adjust it according to your scenario
# The function must be called after mobility model is defined
$ns_ initial_node_pos $node_($i) 20
}
# Telling nodes when the simulation ends
for {set i 0 } {$i < $val(nn) } {incr i} {
$ns_ at $val(finish) "$node_($i) reset";
}
# Ending nam and the simulation
$ns_ at $val(finish) "$ns_ nam-end-wireless $val(finish)"
$ns_ at $val(finish) "finish"
$ns_ at $val(finish).01 "puts \"ns EXITING...\" ; $ns_ halt"
puts "Starting Simulation..."
$ns_ run
Usage: ns compare.tcl -sc <sc> -cp<cp> -tr <tr> -nm <nm> -rp <rp>
where
compare.tcl --- tcl script for running protocol
sc --- movement file
cp --- traffic file
tr --- trace file
nm --- nam file
rp --- routing protocol
Use 1 for DSR, 2 for AODV and any number for DSDV
Pass these
parameters form command line like as shown in figure
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
download script compare.tcl (3.9KB)