Hello All,
Can anyone help me to find out why I cannot get any MAC connection between
nodes when I execute this program? And I
always find minus dropped packets.
Thank you for your help.
Sincerely,
Feng An
please place the three files in the same folder, and use: ns *.tcl -nn XX to
execute that
main file:
source getopt.tclib
global opt
lappend optlist path base tr nam nn bw delay ll ifq mac chan verb stop
#pkt_Size rate
set x [getopt $argc $argv $optlist]
# ======================================================================
# Define options
# ======================================================================
set opt(chan) Channel/WirelessChannel ;# channel type
set opt(prop) Propagation/TwoRayGround ;# radio-propagation
model
set opt(netif) Phy/WirelessPhy ;# network interface type
set opt(mac) Mac/802_11 ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type
set opt(ll) LL ;# link layer type
set opt(ant) Antenna/OmniAntenna ;# antenna model
set opt(ifqlen) 50 ;# max packet in ifq
#set opt(nn) 4 ;# number of mobilenodes
set opt(adhocRouting) DSDV ;# routing protocol
set opt(x) 600 ;# x coordinate of topology
set opt(y) 600 ;# y coordinate of topology
set opt(seed) 0.0 ;# seed for random number
gen.
set opt(stop) 20 ;# time to stop simulation
set num_bs_nodes 1
Mac/Simple set bandwidth_ 2Mb
# ======================================================================
# Main Program
# ======================================================================
#
# Initialize Global Variables
#
set ns_ [new Simulator]
$ns_ use-newtrace
set tracefd [open simple3.tr w]
$ns_ trace-all $tracefd
set namtrace [open simple3.tr.nam w]
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)
# set up topography object
set topo [new Topography]
$topo load_flatgrid $opt(x) $opt(y)
#set chan [new $opt(chan)]
#
# Create God
#
#create-god 3
set god_ [create-god [expr $opt(nn) + $num_bs_nodes]]
#
# Create the specified number of mobilenodes [$val(nn)] and "attach" them
# to the channel.
# Here two nodes are created : node(0) and node(1)
# configure node
$ns_ node-config -adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-agentTrace ON \
-wiredRouting OFF \
-routerTrace ON \
-macTrace ON \
-movementTrace OFF
for {set i 0} {$i < $opt(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) set id_ $i
$node_($i) random-motion 0 ;# disable random motion
}
#
# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
#
#set random value
set x [new RandomVariable/Uniform]
$x set min_ 0
$x set max_ 600
set y [new RandomVariable/Uniform]
$y set min_ 0
$y set max_ 600
for {set i 0} {$i < $opt(nn)} {incr i} {
set x_rand [$x value]
set y_rand [$y value]
$node_($i) set X_ x_rand
$node_($i) set Y_ y_rand
$node_($i) set Z_ 0.0
}
# configure for base-station node
$ns_ node-config -adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-wiredRouting OFF \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace OFF
#create base-station node
set BS(0) [$ns_ node ]
$BS(0) set id_ $opt(nn)
$BS(0) random-motion 0 ;# disable random motion
#provide some co-ord (fixed) to base station node
$BS(0) set X_ 300.0
$BS(0) set Y_ 300.0
$BS(0) set Z_ 0.0
################################################################################
#######################senser nodes to base
station#############################
for {set i 0} {$i < $opt(nn)} {incr i} {
#Create a UDP agent and attach it to node n0
set udp($i) [new Agent/UDP]
# $udp($i) set filename
sd_udp#?????????????????????????????????????????????
# $udp($i) set fid_ 1
$ns_ attach-agent $node_($i) $udp($i)
#Create a CBR traffic source and attach it to udp0
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) attach-agent $udp($i)
$cbr($i) set type_ CBR
$cbr($i) set packetSize_ 500
$cbr($i) set rate_ 1mb
}
set null0 [new Agent/Null]
#$null0 set filename rd_udp#?????????????????????????????????????????????
$ns_ attach-agent $BS(0) $null0
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns_ connect $udp($i) $null0
}
#############################
for {set i 0} {$i < $opt(nn)} {incr i} {
set tcp($i) [new Agent/TCP]
$tcp($i) set class_ 2
$ns_ attach-agent $node_($i) $tcp($i)
}
set sink0 [new Agent/TCPSink]
$ns_ attach-agent $BS(0) $sink0
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns_ connect $tcp($i) $sink0
}
for {set i 0} {$i < $opt(nn)} {incr i} {
set ftp($i) [new Application/FTP]
$ftp($i) attach-agent $tcp($i)
$ns_ at 0.0 "$ftp($i) start"
}
################################
set u [new RandomVariable/Uniform]
$u set min_ 0
$u set max_ 1
for {set i 0} {$i < $opt(nn)} {incr i} {
set rand [$u value]
$ns_ at $rand "$cbr($i) start"
}
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns_ at $opt(stop) "$cbr($i) stop"
}
################################################################################
####################base station to sensor
nodes################################
set udp0 [new Agent/UDP]
#$udp0 set filename sd_udp#?????????????????????????????????????????????
#$udp0 set fid_ 2
$ns_ attach-agent $BS(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set type_ CBR
$cbr0 set packetSize_ 50
$cbr0 set rate_ 100kb
for {set i 0} {$i < $opt(nn)} {incr i} {
set null($i) [new Agent/Null]
# $null($i) set filename rd_udp#????????????????????????????????????????
$ns_ attach-agent $node_($i) $null($i)
}
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns_ connect $udp0 $null($i)
}
###########################################
set tcp0 [new Agent/TCP]
$tcp0 set class_ 2
$ns_ attach-agent $BS(0) $tcp0
for {set i 0} {$i < $opt(nn)} {incr i} {
set sink($i) [new Agent/TCPSink]
$ns_ attach-agent $node_($i) $sink($i)
}
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns_ connect $tcp0 $sink($i)
}
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns_ at 0.0 "$ftp0 start"
############################################
set u [new RandomVariable/Uniform]
$u set min_ 0
$u set max_ 1
set rand [$u value]
$ns_ at $rand "$cbr0 start"
$ns_ at $opt(stop) "$cbr0 stop"
################################################################################
################################################################################
#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $opt(nn) } {incr i} {
$ns_ at $opt(stop).0 "$node_($i) reset";
}
$ns_ at $opt(stop).0 "$BS(0) reset";
$ns_ at $opt(stop).01 "stop"
$ns_ at $opt(stop).02 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
global ns_ tracefd
$ns_ flush-trace
close $tracefd
}
puts "Starting Simulation..."
$ns_ run
additional file1:
getopt.tclib
# To process user's arguments to the global array "opt"
proc getopt {argc argv option_list} {
global opt
set infix ""
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
if {[string range $arg 0 0] != "-"} {
# an argument name must start with a hypen(-)
puts "Unknow argument: $arg (ignored)"
continue
}
set name [string range $arg 1 end]
if { [lsearch $option_list $name] < 0 } {
# an argument name must match with the predefined list
puts "Unknow option: $arg (ignored)"
continue
}
incr i
set value [lindex $argv $i]
set opt($name) $value
append infix "-" $name ":" $value
}
return $infix
}
additional file 2:
getopt_test.tcl
source getopt.tclib
global opt ;# getopt.lib need an array in global scope
#puts "$opt(path)"
#puts "$opt(nn)"
lappend optlist path base tr nam nn bw delay ll ifq mac chan verb stop
#pkt_Size rate
set x [getopt $argc $argv $optlist]
puts "$x"
#puts "$opt(path)"
puts "$opt(nn)"