0% found this document useful (0 votes)
74 views21 pages

Carrier Sense

The document defines options for simulating wireless carrier sense using the Network Simulator ns-2. It sets parameters for the wireless channel, radio propagation model, network interface, MAC protocol, queue type, and more. It then initializes nodes in a topology, defines traffic from nodes 0 to 1 and 2 to 3, and runs the simulation.

Uploaded by

Dehri Brahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views21 pages

Carrier Sense

The document defines options for simulating wireless carrier sense using the Network Simulator ns-2. It sets parameters for the wireless channel, radio propagation model, network interface, MAC protocol, queue type, and more. It then initializes nodes in a topology, defines traffic from nodes 0 to 1 and 2 to 3, and runs the simulation.

Uploaded by

Dehri Brahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Carrier sense

# ======================================================================
# Define options
# ======================================================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-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 model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 4 ;# number of
mobilenodes
set val(rp) DSDV ;# routing protocol
set val(x) 1000.0 ;
set val(y) 1000.0 ;
set val(simtime) 10.0 ; #sim time
set val(drate) 2.0e6 ; #default datarate
set 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

#
# 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 object


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


}

#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)
#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_ run

Fairsim:
# ======================================================================
# Define options
# ======================================================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-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 model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 4 ;# number of
mobilenodes
set val(rp) AODV ;# routing protocol
set val(sc) "./scenario" ;# scenario file
set val(x) 3000.0 ;
set val(y) 400.0 ;
set val(simtime) 10.0 ; #sim time
set val(drate) 2.0e6 ; #default datarate
set 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
set namtrace [open fairsim.nam w] ;# for nam tracing
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
$ns_ use-newtrace

# set up topography object


set 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


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


}

#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
$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_ run

Aodv:
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]
$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 ON

for {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_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(1) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"
proc plotWindow {tcpSource file} {
global ns
set time 0.01
set 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 run

Sim:
# ======================================================================
# Define options
# ======================================================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/Shadowing ;# radio-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 model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 2 ;# number of
mobilenodes
set val(rp) AODV ;# routing protocol
set val(sc) "./scenario" ;# scenario file
set val(x) 3000.0 ;
set val(y) 400.0 ;
set val(simtime) 10.0 ; #sim time
set val(drate) 2.0e6 ; #default datarate
set val(dist) 100 ;
# ======================================================================
# 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 object


set 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
$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)]


$node_(1) set Y_ 200.0
$node_(1) set Z_ 0.0

# 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


}

#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
$ns_ flush-trace
close $tracefd
}

puts "Starting Simulation..."


$ns_ run

Rcs_sim:
# ======================================================================
# Define options
# ======================================================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-
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 model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 100 ;# default number of
mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 1500.0 ;
set val(y) 1500.0 ;
set val(simtime) 10.0 ; #sim time
set 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-sense
threshold (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]
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 $tracefd

set namtrace [open rcssim.nam w] ;# for nam tracing


$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
$ns_ use-newtrace

# set up topography object


set 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)
#set RTSThreshold to some v. large value...effectively shutting down
RTS/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
}
}
# 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

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 $dst\n"
$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"
proc stop {} {
global ns_ tracefd
$ns_ flush-trace
close $tracefd
}
puts "Starting Simulation..."
$ns_ run

Single hop:
# ======================================================================
# Define options
# ======================================================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-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 model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 2 ;# number of
mobilenodes
set val(rp) AODV ;# routing protocol
set val(sc) "./scenario" ;# scenario file
set val(x) 3000.0 ;
set val(y) 400.0 ;
set val(simtime) 10.0 ; #sim time
set 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]]
}
}

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 object


set 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 \
-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 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


}

#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_ 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

You might also like