0
在編寫ns2仿真時,我預計Graph proc中的變量nummsg對於文件的每個輸出都會發生變化。但它仍然是18。我不明白爲什麼incr函數不起作用。任何幫助表示讚賞。Tcl全局變量在ns2中似乎沒有變化
#Create scheduler
#Create an event scheduler wit multicast turned on
set ns [new Simulator -multicast on]
$ns multicast
set nummsg 18
#Turn on Tracing
set tf [open output.tr w]
$ns trace-all $tf
# Turn on nam Tracing
set fd [open mcast.nam w]
$ns namtrace-all $fd
# Create nodes
set OLDCOMM {
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]
set n7 [$ns node]
}
set i 0
for {set i} {$i < 20} {incr i} {
set n$i "[$ns node]"
}
# Create links
$ns duplex-link $n0 $n1 1.5Mb 10ms DropTail
$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link $n2 $n4 1.5Mb 10ms DropTail
$ns duplex-link $n1 $n7 1.5Mb 10ms DropTail
$ns duplex-link $n1 $n5 1.5Mb 10ms DropTail
$ns duplex-link $n4 $n6 1.5Mb 10ms DropTail
#tactic
# for time
# add or delete nodes based on a rand
for {set i} {$i < 100} {incr i} {
puts "OK $i "
}
# Routing protocol: say distance vector
#Protocols: CtrMcast, DM, ST, BST
set mproto DM
set mrthandle [$ns mrtproto $mproto {}]
# Allocate group addresses
set group1 [Node allocaddr]
set group2 [Node allocaddr]
# UDP Transport agent for the traffic source
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
$udp0 set dst_addr_ $group1
$udp0 set dst_port_ 0
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp0
# Transport agent for the traffic source
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
$udp1 set dst_addr_ $group2
$udp1 set dst_port_ 0
set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp1
#$ns at 0.2 "Graph"
set g [open graph.tr w]
set g1 [open graph1.tr w]
proc Graph {} {
global nummsg
global ns g g1
set time 0.5
set now [$ns now]
puts $nummsg
puts $g "$now $nummsg"
#puts $g1 "[expr rand()*8] [expr rand()*6]"
$ns at [expr $now+$time] "Graph"
}
# Create receiver
set rcvr1 [new Agent/Null]
$ns attach-agent $n5 $rcvr1
$ns at 1.0 "$n5 join-group $rcvr1 $group1"
incr nummsg
set rcvr2 [new Agent/Null]
$ns attach-agent $n6 $rcvr2
$ns at 1.5 "$n6 join-group $rcvr2 $group1"
incr nummsg
set rcvr3 [new Agent/Null]
$ns attach-agent $n7 $rcvr3
$ns at 2.0 "$n7 join-group $rcvr3 $group1"
set nummsg 22
#$ns duplex-link $n4 $n6 1.5Mb 10ms DropTail
set rcvr4 [new Agent/Null]
$ns attach-agent $n5 $rcvr1
$ns at 2.5 "$n5 join-group $rcvr4 $group2"
incr nummsg
set rcvr5 [new Agent/Null]
$ns attach-agent $n6 $rcvr2
$ns at 3.0 "$n6 join-group $rcvr5 $group2"
incr nummsg
set rcvr6 [new Agent/Null]
$ns attach-agent $n7 $rcvr3
$ns at 3.5 "$n7 join-group $rcvr6 $group2"
$ns at 4.0 "$n5 leave-group $rcvr1 $group1"
$ns at 4.5 "$n6 leave-group $rcvr2 $group1"
$ns at 5.0 "$n7 leave-group $rcvr3 $group1"
set nummsg [expr {$nummsg - 1}]
set nummsg [expr {$nummsg - 1}]
set nummsg [expr {$nummsg - 1}]
$ns at 5.5 "$n5 leave-group $rcvr4 $group2"
$ns at 6.0 "$n6 leave-group $rcvr5 $group2"
$ns at 6.5 "$n7 leave-group $rcvr6 $group2"
set nummsg [expr {$nummsg - 1}]
set nummsg [expr {$nummsg - 1}]
set nummsg [expr {$nummsg - 1}]
# Schedule events
$ns at 0.0 "Graph"
$ns at 0.5 "$cbr1 start"
$ns at 9.5 "$cbr1 stop"
$ns at 0.5 "$cbr2 start"
$ns at 9.5 "$cbr2 stop"
#post-processing
$ns at 10.0 "finish"
proc finish {} {
global ns tf
$ns flush-trace
close $tf
#exec nam mcast.nam &
exit 0
}
# For nam
#Colors for packets from two mcast groups
$ns color 10 red
$ns color 11 green
$ns color 30 purple
$ns color 31 green
# Manual layout: order of the link is significant!
#$ns duplex-link-op $n0 $n1 orient right
#$ns duplex-link-op $n0 $n2 orient right-up
#$ns duplex-link-op $n0 $n3 orient right-down
# Show queue on simplex link n0->n1
#$ns duplex-link-op $n2 $n3 queuePos 0.5
# Group 0 source
$udp0 set fid_ 10
$n0 color red
$n0 label "Source 1"
# Group 1 source
$udp1 set fid_ 11
$n1 color green
$n1 label "Source 2"
$n5 label "Receiver 1"
$n5 color blue
$n6 label "Receiver 2"
$n6 color blue
$n7 label "Receiver 3"
$n7 color blue
#$n2 add-mark m0 red
#$n2 delete-mark m0"
# Animation rate
$ns set-animation-rate 3.0ms
$ns run
我不知道ns2能夠回答你的問題,雖然我沒有看到任何錯誤的圖形過程。但是你是否知道'incr'命令需要一個步驟參數?而不是'set nummsg [expr {$ nummsg - 1}]'你可以做'incr nummsg -1'(或者如果你喜歡......可以加上incr nummsg -3) – 2013-05-05 18:48:05
好吧,我確實把一個nummsg 22放入第104行測試Graph內部打印的值是否會發生變化。但它仍然打印18. – 2013-05-05 18:51:11
從哪裏調用Graph圖? – 2013-05-05 23:06:50