Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ns2programs

10,407 views

Published on

Published in: Education
  • Be the first to comment

Ns2programs

  1. 1. Carrier sense# ======================================================================# Define options# ======================================================================set val(chan) Channel/WirelessChannel ;# channel typeset val(prop) Propagation/TwoRayGround ;# radio-propagationmodelset val(netif) Phy/WirelessPhy ;# network interfacetypeset val(mac) Mac/802_11 ;# MAC typeset val(ifq) Queue/DropTail/PriQueue ;# interface queuetypeset val(ll) LL ;# link layer typeset val(ant) Antenna/OmniAntenna ;# antenna modelset val(ifqlen) 50 ;# max packet in ifqset val(nn) 4 ;# number ofmobilenodesset val(rp) DSDV ;# routing protocolset val(x) 1000.0 ;set val(y) 1000.0 ;set val(simtime) 10.0 ; #sim timeset val(drate) 2.0e6 ; #default datarateset val(dist) 100.0 ;# ======================================================================# Main Program# ======================================================================if { $argc != 6} { puts "Wrong no. of cmdline args." puts "Usage: ns csense.tcl -RTSthresh <RTS_Threshold> -CSthresh<carrier-sense threshold> -dist <x>" exit 0}proc getopt {argc argv} { global val lappend optlist RTSthresh CSthresh dist 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]] }}getopt $argc $argv#
  2. 2. # Initialize Global Variables#set ns_ [new Simulator]set tracefd [open csense.tr w]$ns_ trace-all $tracefd#set namtrace [open single-hop.nam w] ;# for nam tracing#$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)$ns_ use-newtrace# set up topography objectset topo [new Topography]$topo load_flatgrid $val(x) $val(y)## Create God#set god_ [ create-god $val(nn) ]$val(mac) set dataRate_ $val(drate)if { $val(RTSthresh) >= 0 } { $val(mac) set RTSThreshold_ $val(RTSthresh)}if { $val(CSthresh) >= 0 } { $val(netif) set CSThresh_ $val(CSthresh) }puts "Setting RTS_Threshold to $val(RTSthresh)"puts "Setting CS_Threshold to $val(CSthresh)"## Create the specified number of mobilenodes [$val(nn)] and "attach"them# to the channel.# configure node $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 OFF
  3. 3. for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node] $node_($i) random-motion 0 ;# disable random motion }## Provide initial (X,Y, Z=0) co-ordinates for mobilenodes##Conversation 1: Node 0 is the source, Node 1 is the dst $node_(0) set X_ 50.0 $node_(0) set Y_ 0.0 $node_(0) set Z_ 0.0 $node_(1) set X_ 250.0 $node_(1) set Y_ 0.0 $node_(1) set Z_ 0.0#Conversation 2: Node 2 is the source, Node 3 is the dst $node_(2) set X_ 450.0 $node_(2) set Y_ $val(dist) $node_(2) set Z_ 0.0 $node_(3) set X_ 250.0 $node_(3) set Y_ $val(dist) $node_(3) set Z_ 0.0# Define node initial position in namfor {set i 0} {$i < $val(nn)} {incr i} { # 20 defines the node size in nam, must adjust it according to yourscenario # The function must be called after mobility model is defined $ns_ initial_node_pos $node_($i) 20}#Attach a data-sink to destination set null_(0) [new Agent/Null] $ns_ attach-agent $node_(1) $null_(0) set null_(1) [new Agent/Null] $ns_ attach-agent $node_(3) $null_(1)
  4. 4. #traffic...make src talk to dst set udp_(0) [new Agent/UDP] $ns_ attach-agent $node_(0) $udp_(0) set cbr_(0) [new Application/Traffic/CBR] $cbr_(0) set packetSize_ 1024 $udp_(0) set packetSize_ 1024 $cbr_(0) set interval_ 0.001 $cbr_(0) set random_ 0.18372 $cbr_(0) set maxpkts_ 1000000 $cbr_(0) attach-agent $udp_(0) $ns_ connect $udp_(0) $null_(0) $ns_ at 0.0 "$cbr_(0) start" set udp_(1) [new Agent/UDP] $ns_ attach-agent $node_(2) $udp_(1) set cbr_(1) [new Application/Traffic/CBR] $cbr_(1) set packetSize_ 1024 $udp_(1) set packetSize_ 1024 $cbr_(1) set interval_ 0.001 $cbr_(1) set random_ 0.56272 $cbr_(1) set maxpkts_ 1000000 $cbr_(1) attach-agent $udp_(1) $ns_ connect $udp_(1) $null_(1) $ns_ at 1.0 "$cbr_(1) start"## Tell nodes when the simulation ends#for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at $val(simtime) "$node_($i) reset";}$ns_ at $val(simtime) "stop"$ns_ at $val(simtime).01 "puts "NS EXITING..." ; $ns_ halt"proc stop {} { global ns_ tracefd $ns_ flush-trace close $tracefd}puts "Starting Simulation..."$ns_ runFairsim:# ======================================================================# Define options# ======================================================================set val(chan) Channel/WirelessChannel ;# channel type
  5. 5. set val(prop) Propagation/TwoRayGround ;# radio-propagationmodelset val(netif) Phy/WirelessPhy ;# network interfacetypeset val(mac) Mac/802_11 ;# MAC typeset val(ifq) Queue/DropTail/PriQueue ;# interface queuetypeset val(ll) LL ;# link layer typeset val(ant) Antenna/OmniAntenna ;# antenna modelset val(ifqlen) 50 ;# max packet in ifqset val(nn) 4 ;# number ofmobilenodesset val(rp) AODV ;# routing protocolset val(sc) "./scenario" ;# scenario fileset val(x) 3000.0 ;set val(y) 400.0 ;set val(simtime) 10.0 ; #sim timeset val(drate) 2.0e6 ; #default datarateset val(dist) 150.0 ; #default datarate# ======================================================================if { $argc < 2} { puts "Wrong no. of cmdline args." puts "Usage: ns sim.tcl -dist <dist>" exit 0}proc getopt {argc argv} { global val lappend optlist dist 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]] }}# Main Program# ======================================================================getopt $argc $argv## Initialize Global Variables#set ns_ [new Simulator]set tracefd [open fairsim.tr w]$ns_ trace-all $tracefd
  6. 6. set namtrace [open fairsim.nam w] ;# for nam tracing$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)$ns_ use-newtrace# set up topography objectset topo [new Topography]$topo load_flatgrid $val(x) $val(y)## Create God#set god_ [ create-god $val(nn) ]$val(mac) set bandwidth_ 22.0e6#$val(prop) set pathlossExp_ 3.0## Create the specified number of mobilenodes [$val(nn)] and "attach"them# to the channel.# configure node $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 OFF for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node] $node_($i) random-motion 0 ;# disable random motion }## Provide initial (X,Y, Z=0) co-ordinates for mobilenodes##Node 0 is the source, Node 1 is the dst
  7. 7. $node_(1) set X_ 200.0 $node_(1) set Y_ 200.0 $node_(1) set Z_ 0.0 $node_(0) set X_ 250.0 $node_(0) set Y_ 200.0 $node_(0) set Z_ 0.0 set x [expr 250.0 + $val(dist)] $node_(3) set X_ $x $node_(3) set Y_ 200.0 $node_(3) set Z_ 0.0 $node_(2) set X_ [expr $x + 100.0] $node_(2) set Y_ 200.0 $node_(2) set Z_ 0.0# Define node initial position in namfor {set i 0} {$i < $val(nn)} {incr i} { # 20 defines the node size in nam, must adjust it according to yourscenario # The function must be called after mobility model is defined $ns_ initial_node_pos $node_($i) 20}#Attach a data-sink to destination set null_(1) [new Agent/Null] $ns_ attach-agent $node_(1) $null_(1) set null_(3) [new Agent/Null] $ns_ attach-agent $node_(3) $null_(3)#traffic...make sources talk to dst set udp_(0) [new Agent/UDP] $ns_ attach-agent $node_(0) $udp_(0) set cbr_(0) [new Application/Traffic/CBR] $cbr_(0) set packetSize_ 512 $cbr_(0) set interval_ 0.0008 $cbr_(0) set random_ 0.96749 $cbr_(0) set maxpkts_ 1000000 $cbr_(0) attach-agent $udp_(0) set udp_(2) [new Agent/UDP] $ns_ attach-agent $node_(2) $udp_(2) set cbr_(2) [new Application/Traffic/CBR] $cbr_(2) set packetSize_ 512
  8. 8. $cbr_(2) set interval_ 0.0008 $cbr_(2) set random_ 0.96749 $cbr_(2) set maxpkts_ 1000000 $cbr_(2) attach-agent $udp_(2) $ns_ connect $udp_(0) $null_(1) $ns_ connect $udp_(2) $null_(3) $ns_ at 0.0 "$cbr_(0) start" $ns_ at 0.0 "$cbr_(2) start"## Tell nodes when the simulation ends#for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at $val(simtime) "$node_($i) reset";}$ns_ at $val(simtime) "stop"$ns_ at $val(simtime).01 "puts "NS EXITING..." ; $ns_ halt"proc stop {} { global ns_ tracefd $ns_ flush-trace close $tracefd}puts "Starting Simulation..."$ns_ runAodv: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) 3 ;set val(rp) AODV ;set val(x) 500 ;set val(y) 400 ;set val(stop) 150 ;set ns [new Simulator]set tracefd [open simple.tr w]set windowVsTime2 [open win.tr w]set namtrace [open simwrls.nam w]
  9. 9. $ns trace-all $tracefd$ns namtrace-all-wireless $namtrace $val(x) $val(y)set topo [new Topography]$topo load_flatgrid $val(x) $val(y)create-god $val(nn)$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 OFF -movementTrace ONfor {set i 0} {$i <$val(nn) } {incr i} { set node_($i) [$ns node]}$node_(0) set X_ 5.0$node_(0) set Y_ 5.0$node_(0) set Z_ 0.0$node_(1) set X_ 490.0$node_(1) set Y_ 285.0$node_(1) set Z_ 0.0$node_(2) set X_ 150.0$node_(2) set Y_ 240.0$node_(2) set Z_ 0.0$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"set tcp [new Agent/TCP/Newreno]$tcp set class_ 2set sink [new Agent/TCPSink]$ns attach-agent $node_(0) $tcp$ns attach-agent $node_(1) $sink$ns connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns at 10.0 "$ftp start"
  10. 10. proc plotWindow {tcpSource file} {global nsset time 0.01set now [$ns now]set cwnd [$tcpSource set cwnd_]puts $file "$now $cwnd"$ns at [expr $now+$time] "plotWindow $tcpSource $file" }$ns at 10.1 "plotWindow $tcp $windowVsTime2"for {set i 0} {$i<$val(nn) } {incr i} {$ns initial_node_pos $node_($i) 30}for {set i 0} {$i<$val(nn) } {incr i} { $ns at $val(stop) "$node_($i) reset";}$ns at $val(stop) "$ns nam-end-wireless $val(stop)"$ns at $val(stop) "stop"$ns at 150.01 "puts"end simulation";$ns halt"proc stop { } { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace}$ns runSim:# ======================================================================# Define options# ======================================================================set val(chan) Channel/WirelessChannel ;# channel typeset val(prop) Propagation/Shadowing ;# radio-propagationmodelset val(netif) Phy/WirelessPhy ;# network interfacetypeset val(mac) Mac/802_11 ;# MAC typeset val(ifq) Queue/DropTail/PriQueue ;# interface queuetypeset val(ll) LL ;# link layer typeset val(ant) Antenna/OmniAntenna ;# antenna modelset val(ifqlen) 50 ;# max packet in ifqset val(nn) 2 ;# number ofmobilenodesset val(rp) AODV ;# routing protocolset val(sc) "./scenario" ;# scenario fileset val(x) 3000.0 ;set val(y) 400.0 ;set val(simtime) 10.0 ; #sim timeset val(drate) 2.0e6 ; #default datarateset val(dist) 100 ;
  11. 11. # ======================================================================# Main Program# ======================================================================if { $argc != 2} { puts "Wrong no. of cmdline args." puts "Usage: ns sim.tcl -dist <x>" exit 0}proc getopt {argc argv} { global val lappend optlist dist 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]] }}getopt $argc $argv## Initialize Global Variables#set ns_ [new Simulator]set tracefd [open sim.tr w]$ns_ trace-all $tracefd$ns_ use-newtrace# set up topography objectset topo [new Topography]$topo load_flatgrid $val(x) $val(y)## Create God#set god_ [ create-god $val(nn) ]$val(netif) set Pt_ 0.100$val(netif) set RXThresh_ 7.94328e-13$val(netif) set CSThresh_ 6.30957e-14
  12. 12. $val(mac) set dataRate_ $val(drate)#Shadowing Model Parameters$val(prop) set std_db_ 4.0$val(prop) set pathlossExp_ 3.5## Create the specified number of mobilenodes [$val(nn)] and "attach"them# to the channel.# configure node $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 OFF for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node] $node_($i) random-motion 0 ;# disable random motion }## Provide initial (X,Y, Z=0) co-ordinates for mobilenodes##Node 0 is the source, Node 1 is the dst $node_(0) set X_ 100.0 $node_(0) set Y_ 200.0 $node_(0) set Z_ 0.0 $node_(1) set X_ [expr 100.0 + $val(dist)]
  13. 13. $node_(1) set Y_ 200.0 $node_(1) set Z_ 0.0# Define node initial position in namfor {set i 0} {$i < $val(nn)} {incr i} { # 20 defines the node size in nam, must adjust it according to yourscenario # The function must be called after mobility model is defined $ns_ initial_node_pos $node_($i) 20}#Attach a data-sink to destination set null_(0) [new Agent/Null] $ns_ attach-agent $node_(1) $null_(0)#traffic...make src talk to dst set udp_(0) [new Agent/UDP] $ns_ attach-agent $node_(0) $udp_(0) set cbr_(0) [new Application/Traffic/CBR] $cbr_(0) set packetSize_ 512 $udp_(0) set packetSize_ 512 $cbr_(0) set interval_ 0.0008 $cbr_(0) set random_ 0.96749 $cbr_(0) set maxpkts_ 1000000 $cbr_(0) attach-agent $udp_(0) $ns_ connect $udp_(0) $null_(0) $ns_ at 0.0 "$cbr_(0) start"## Tell nodes when the simulation ends#for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at $val(simtime) "$node_($i) reset";}$ns_ at $val(simtime) "stop"$ns_ at $val(simtime).01 "puts "NS EXITING..." ; $ns_ halt"proc stop {} { global ns_ tracefd
  14. 14. $ns_ flush-trace close $tracefd}puts "Starting Simulation..."$ns_ runRcs_sim:# ======================================================================# Define options# ======================================================================set val(chan) Channel/WirelessChannel ;# channel typeset val(prop) Propagation/TwoRayGround ;# radio-propagation modelset val(netif) Phy/WirelessPhy ;# network interfacetypeset val(mac) Mac/802_11 ;# MAC typeset val(ifq) Queue/DropTail/PriQueue ;# interface queuetypeset val(ll) LL ;# link layer typeset val(ant) Antenna/OmniAntenna ;# antenna modelset val(ifqlen) 50 ;# max packet in ifqset val(nn) 100 ;# default number ofmobilenodesset val(rp) AODV ;# routing protocolset val(x) 1500.0 ;set val(y) 1500.0 ;set val(simtime) 10.0 ; #sim timeset val(rlen) 10 ;# ======================================================================# Main Program# ======================================================================if { $argc != 8 } { puts "Wrong no. of cmdline args." puts "Usage: ns multi-hop.tcl -rate <rate (Mbps)> -rxthresh<rcv_threshold> -cpthresh <capture-threshold> -csthresh <carrier-sensethreshold (W)>" exit 0}proc getopt {argc argv} { global val lappend optlist rate rxthresh cpthresh csthresh 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]
  15. 15. set val($name) [lindex $argv [expr $i+1]] }}getopt $argc $argv## Initialize Global Variables#set ns_ [new Simulator]set tracefd [open rcssim.tr w]$ns_ trace-all $tracefdset namtrace [open rcssim.nam w] ;# for nam tracing$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)$ns_ use-newtrace# set up topography objectset topo [new Topography]$topo load_flatgrid $val(x) $val(y)## Create God#set god_ [ create-god $val(nn) ]#set Propagation parameters$val(prop) set pathlossExp_ 2.5$val(prop) set std_db_ 2.0#set PHY parameters#2.4 GHz frequency$val(netif) set freq_ 2.4e+09#100 mW from Cisco 350$val(netif) set Pt_ 0.100$val(netif) set RXThresh_ $val(rxthresh)$val(netif) set CPThresh_ $val(cpthresh)$val(netif) set CSThresh_ $val(csthresh)#$val(netif) set bandwidth_ $val(rate)#set MAC parameters#$val(mac) set bandwidth_ $val(rate)$val(mac) set basicRate_ $val(rate)$val(mac) set dataRate_ $val(rate)
  16. 16. #set RTSThreshold to some v. large value...effectively shutting downRTS/CTS$val(mac) set RTSThreshold_ 10192## Create the specified number of mobilenodes [$val(nn)] and "attach"them# to the channel.# configure node $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 OFF for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node] $node_($i) random-motion 0 ;# disable random motion }## Provide initial (X,Y, Z=0) co-ordinates for mobilenodes# set gridspace [expr $val(x) / $val(rlen)] for {set i 0} {$i < $val(rlen) } {incr i} { for {set j 0} {$j < $val(rlen) } {incr j} { set a [expr $j + [expr $i * $val(rlen)]] $node_($a) set X_ [expr 0.0 + [ expr $i * $gridspace]] $node_($a) set Y_ [expr 0.0 + [ expr $j * $gridspace]] $node_($a) set Z_ 0.0 } }
  17. 17. # Define node initial position in namfor {set i 0} {$i < $val(nn)} {incr i} { # 20 defines the node size in nam, must adjust it according to yourscenario # The function must be called after mobility model is defined $ns_ initial_node_pos $node_($i) 20 set udp_($i) [new Agent/UDP] $udp_($i) set packetSize_ 1024 $ns_ attach-agent $node_($i) $udp_($i) set cbr_($i) [new Application/Traffic/CBR] $cbr_($i) set packetSize_ 1024 $cbr_($i) set interval_ 0.001 $cbr_($i) set random_ 0.1346 $cbr_($i) set maxpkts_ 1000000 $cbr_($i) attach-agent $udp_($i) set null_($i) [new Agent/Null] $ns_ attach-agent $node_($i) $null_($i)}## Tell nodes when the simulation ends#expr srand(19987)for {set i 0} {$i < 25 } {incr i} { set tmp [expr [expr rand()] * $val(nn)] set src [expr int($tmp)] set tmp [expr [expr rand()] * $val(nn)] set dst [expr int($tmp)] puts "$src to $dstn" $ns_ connect $udp_($src) $null_($dst) $ns_ at [expr 0.1 * $i] "$cbr_($src) start" #$ns_ at [expr 0.01 * $i] "puts " cbr_($i) start" "}for {set i 0} {$i < $val(nn)} {incr i} { $ns_ at $val(simtime) "$node_($i) reset";}$ns_ at $val(simtime) "stop"$ns_ at $val(simtime).01 "puts "NS EXITING..." ; $ns_ halt"
  18. 18. proc stop {} { global ns_ tracefd $ns_ flush-trace close $tracefd}puts "Starting Simulation..."$ns_ runSingle hop:# ======================================================================# Define options# ======================================================================set val(chan) Channel/WirelessChannel ;# channel typeset val(prop) Propagation/TwoRayGround ;# radio-propagationmodelset val(netif) Phy/WirelessPhy ;# network interfacetypeset val(mac) Mac/802_11 ;# MAC typeset val(ifq) Queue/DropTail/PriQueue ;# interface queuetypeset val(ll) LL ;# link layer typeset val(ant) Antenna/OmniAntenna ;# antenna modelset val(ifqlen) 50 ;# max packet in ifqset val(nn) 2 ;# number ofmobilenodesset val(rp) AODV ;# routing protocolset val(sc) "./scenario" ;# scenario fileset val(x) 3000.0 ;set val(y) 400.0 ;set val(simtime) 10.0 ; #sim timeset val(drate) 2.0e6 ; #default datarate# ======================================================================# Main Program# ======================================================================if { $argc != 2} { puts "Wrong no. of cmdline args." puts "Usage: ns single-hop.tcl -drate <dataRate>" exit 0}proc getopt {argc argv} { global val lappend optlist drate 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]] }
  19. 19. }getopt $argc $argv## Initialize Global Variables#set ns_ [new Simulator]set tracefd [open single-hop.tr w]$ns_ trace-all $tracefd#set namtrace [open single-hop.nam w] ;# for nam tracing#$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)$ns_ use-newtrace# set up topography objectset topo [new Topography]$topo load_flatgrid $val(x) $val(y)## Create God#set god_ [ create-god $val(nn) ]$val(mac) set dataRate_ $val(drate)$val(mac) set bandwidth_ 22.0e6$val(prop) set pathlossExp_ 4.0## Create the specified number of mobilenodes [$val(nn)] and "attach"them# to the channel.# configure node $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
  20. 20. -macTrace ON -movementTrace OFF for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node] $node_($i) random-motion 0 ;# disable random motion }## Provide initial (X,Y, Z=0) co-ordinates for mobilenodes##Node 0 is the source, Node 1 is the dst $node_(0) set X_ 100.0 $node_(0) set Y_ 200.0 $node_(0) set Z_ 0.0 $node_(1) set X_ 300.0 $node_(1) set Y_ 200.0 $node_(1) set Z_ 0.0# Define node initial position in namfor {set i 0} {$i < $val(nn)} {incr i} { # 20 defines the node size in nam, must adjust it according to yourscenario # The function must be called after mobility model is defined $ns_ initial_node_pos $node_($i) 20}#Attach a data-sink to destination set null_(0) [new Agent/Null] $ns_ attach-agent $node_(1) $null_(0)#traffic...make src talk to dst set udp_(0) [new Agent/UDP]
  21. 21. $ns_ attach-agent $node_(0) $udp_(0) set cbr_(0) [new Application/Traffic/CBR] $cbr_(0) set packetSize_ 2048 $udp_(0) set packetSize_ 2048 $cbr_(0) set interval_ 0.0008 $cbr_(0) set random_ 0.96749 $cbr_(0) set maxpkts_ 1000000 $cbr_(0) attach-agent $udp_(0) $ns_ connect $udp_(0) $null_(0) $ns_ at 0.0 "$cbr_(0) start"## Tell nodes when the simulation ends#for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at $val(simtime) "$node_($i) reset";}$ns_ at $val(simtime) "stop"$ns_ at $val(simtime).01 "puts "NS EXITING..." ; $ns_ halt"proc stop {} { global ns_ tracefd $ns_ flush-trace close $tracefd}puts "Starting Simulation..."$ns_ run

×