Advertisement
iPPle

script-tcl-prof-tp3

Sep 22nd, 2022
2,571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 1.86 KB | None | 0 0
  1. set ns [new Simulator]
  2.  
  3.  
  4. # open trace files
  5. set f [open out.tr w]
  6. $ns trace-all $f
  7. set nf [open out.nam w]
  8. $ns namtrace-all $nf
  9.  
  10. # nb nodes
  11. set nb 6
  12.  
  13. #create topology
  14.  
  15. for {set i 0} {$i < 6} {incr i} {
  16.     set n($i) [$ns node]
  17. }
  18.  
  19. $ns duplex-link $n(0) $n(1) 2Mb 5ms DropTail
  20. $ns duplex-link $n(0) $n(2) 2Mb 5ms DropTail
  21. $ns duplex-link $n(1) $n(2) 2Mb 5ms DropTail
  22. $ns duplex-link $n(1) $n(3) 2Mb 5ms DropTail
  23. $ns duplex-link $n(2) $n(4) 2Mb 5ms DropTail
  24. $ns duplex-link $n(3) $n(4) 2Mb 5ms DropTail
  25. $ns duplex-link $n(3) $n(5) 2Mb 5ms DropTail
  26. $ns duplex-link $n(4) $n(5) 2Mb 5ms DropTail
  27.  
  28.  
  29. # create UDP agents and attach them
  30.  
  31. for {set i 0} {$i < 6} {incr i} {
  32.     set udp($i) [new Agent/UDP]
  33.     $ns attach-agent $n($i) $udp($i)
  34. }
  35.  
  36. # dif is a flag
  37. for {set i 0} {$i < 6} {incr i} {
  38.     $udp($i) set dif 0
  39. }
  40.  
  41. # core functions
  42.  
  43. Agent/UDP instproc init_brcst { } {
  44.     global ns udp
  45.     $self instvar node_ dif
  46.  
  47.     puts "[$node_ node-addr] INIT BRDCST"
  48.  
  49.     foreach i [$node_ neighbors] {
  50.         set j [$i node-addr]
  51.         puts "[$node_ node-addr] send brcst to $j"
  52.         $ns connect $udp($j) $udp([$node_ node-addr])
  53.         $self send 100 "brdcst([$node_ node-addr])"
  54.     }
  55.  
  56. }
  57.  
  58. Agent/UDP instproc process_data {size data} {
  59.     global ns udp
  60.     $self instvar node_ dif
  61.  
  62.     puts "[$node_ node-addr] received {$data}"
  63.  
  64.     if {[regexp {brdcst *\(([0-9]+)\)} $data entirematch number] && $dif == 0} {
  65.         set dif 1
  66.         foreach i [$node_ neighbors] {
  67.             set j [$i node-addr]
  68.             if { $j != $number } {
  69.                 puts "[$node_ node-addr] send brcst to $j"
  70.                 $ns connect $udp($j) $udp([$node_ node-addr])
  71.                 $self send 100 "brdcst([$node_ node-addr])"
  72.             }
  73.         }
  74.     }
  75. }
  76.  
  77. # simulation
  78. $ns at 0.1 "$udp(0) init_brcst"
  79.  
  80. $ns at 1.0 "finish"
  81.  
  82. proc finish {} {
  83.     global ns f nf
  84.     $ns flush-trace
  85.     close $f
  86.     close $nf
  87.  
  88.     #puts "running nam..."
  89.     #exec nam out.nam &
  90.     exit 0
  91. }
  92.  
  93. $ns run
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement