Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set ns [new Simulator]
- # open trace files
- set f [open out.tr w]
- $ns trace-all $f
- set nf [open out.nam w]
- $ns namtrace-all $nf
- # nb nodes
- set nb 6
- #create topology
- for {set i 0} {$i < 6} {incr i} {
- set n($i) [$ns node]
- }
- $ns duplex-link $n(0) $n(1) 2Mb 5ms DropTail
- $ns duplex-link $n(0) $n(2) 2Mb 5ms DropTail
- $ns duplex-link $n(1) $n(2) 2Mb 5ms DropTail
- $ns duplex-link $n(1) $n(3) 2Mb 5ms DropTail
- $ns duplex-link $n(2) $n(4) 2Mb 5ms DropTail
- $ns duplex-link $n(3) $n(4) 2Mb 5ms DropTail
- $ns duplex-link $n(3) $n(5) 2Mb 5ms DropTail
- $ns duplex-link $n(4) $n(5) 2Mb 5ms DropTail
- # create UDP agents and attach them
- for {set i 0} {$i < 6} {incr i} {
- set udp($i) [new Agent/UDP]
- $ns attach-agent $n($i) $udp($i)
- }
- # dif is a flag
- for {set i 0} {$i < 6} {incr i} {
- $udp($i) set dif 0
- }
- # core functions
- Agent/UDP instproc init_brcst { } {
- global ns udp
- $self instvar node_ dif
- puts "[$node_ node-addr] INIT BRDCST"
- foreach i [$node_ neighbors] {
- set j [$i node-addr]
- puts "[$node_ node-addr] send brcst to $j"
- $ns connect $udp($j) $udp([$node_ node-addr])
- $self send 100 "brdcst([$node_ node-addr])"
- }
- }
- Agent/UDP instproc process_data {size data} {
- global ns udp
- $self instvar node_ dif
- puts "[$node_ node-addr] received {$data}"
- if {[regexp {brdcst *\(([0-9]+)\)} $data entirematch number] && $dif == 0} {
- set dif 1
- foreach i [$node_ neighbors] {
- set j [$i node-addr]
- if { $j != $number } {
- puts "[$node_ node-addr] send brcst to $j"
- $ns connect $udp($j) $udp([$node_ node-addr])
- $self send 100 "brdcst([$node_ node-addr])"
- }
- }
- }
- }
- # simulation
- $ns at 0.1 "$udp(0) init_brcst"
- $ns at 1.0 "finish"
- proc finish {} {
- global ns f nf
- $ns flush-trace
- close $f
- close $nf
- #puts "running nam..."
- #exec nam out.nam &
- exit 0
- }
- $ns run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement