test-suite-algo-routing.tcl
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:8k
源码类别:

通讯编程

开发平台:

Visual C++

  1. # This test suite is for validating the algorithmic routing support
  2. # in ns.
  3. #
  4. # To run all tests:  test-all-algo-routing
  5. #
  6. # To run individual tests:
  7. # ns test-suite-algo-routing.tcl Algo1
  8. # ns test-suite-algo-routing.tcl Algo2
  9. # ...
  10. #
  11. # To view a list of available tests to run with this script:
  12. # ns test-suite-mixmode.tcl
  13. #
  14. #remove-all-packet-headers       ; # removes all except common
  15. #add-packet-header Flags IP RTP TCP  ; # hdrs reqd for validation test
  16.  
  17. # FOR UPDATING GLOBAL DEFAULTS:
  18. Class TestSuite
  19. TestSuite instproc init {} {
  20. $self instvar ns_ net_ defNet_ test_ topo_ node_ testName_
  21. set ns_ [new Simulator]
  22. #$ns_ use-scheduler List
  23.         $ns_ multicast
  24. $ns_ trace-all [open temp.rands w]
  25. $ns_ namtrace-all [open temp.rands.nam w]
  26. $ns_ color 1 red
  27. $ns_ color 0 blue
  28. if {$net_ == ""} {
  29. set net_ $defNet_
  30. }
  31. if ![Topology/$defNet_ info subclass Topology/$net_] {
  32. global argv0
  33. puts "$argv0: cannot run test $test_ over topology $net_"
  34. exit 1
  35. }
  36. set topo_ [new Topology/$net_ $ns_]
  37. foreach i [$topo_ array names node_] {
  38. # This would be cool, but lets try to be compatible
  39. # with test-suite.tcl as far as possible.
  40. #
  41. # $self instvar $i
  42. # set $i [$topo_ node? $i]
  43. #
  44. set node_($i) [$topo_ node? $i]
  45. }
  46. if {$net_ == $defNet_} {
  47. set testName_ "$test_"
  48. } else {
  49. set testName_ "$test_:$net_"
  50. }
  51. }
  52. TestSuite instproc finish args {
  53. $self instvar ns_
  54. $ns_ flush-trace
  55. exit 0
  56. }
  57. proc usage {} {
  58. global argv0
  59. puts stderr "usage: ns $argv0 <tests> [<topologies>]"
  60. puts stderr "Valid tests are:t[get-subclasses TestSuite Test/]"
  61. puts stderr "Valid Topologies are:t[get-subclasses SkelTopology Topology/]"
  62. exit 1
  63. }
  64. proc isProc? {cls prc} {
  65. if [catch "Object info subclass $cls/$prc" r] {
  66. global argv0
  67. puts stderr "$argv0: no such $cls: $prc"
  68. usage
  69. }
  70. }
  71. proc get-subclasses {cls pfx} {
  72. set ret ""
  73. set l [string length $pfx]
  74. set c $cls
  75. while {[llength $c] > 0} {
  76. set t [lindex $c 0]
  77. set c [lrange $c 1 end]
  78. if [string match ${pfx}* $t] {
  79. lappend ret [string range $t $l end]
  80. }
  81. eval lappend c [$t info subclass]
  82. }
  83. set ret
  84. }
  85. TestSuite proc runTest {} {
  86. global argc argv
  87. switch $argc {
  88. 1 {
  89. set test $argv
  90. isProc? Test $test
  91. set topo ""
  92. }
  93. 2 {
  94. set test [lindex $argv 0]
  95. isProc? Test $test
  96. set a [lindex $argv 1]
  97. if {$a == "QUIET"} {
  98. set topo ""
  99. } else {
  100. set topo $a
  101. isProc? Topology $topo
  102. }
  103. }
  104. default {
  105. usage
  106. }
  107. }
  108. set t [new Test/$test $topo]
  109. $t run
  110. }
  111. # Skeleton topology base class
  112. Class SkelTopology
  113. SkelTopology instproc init {} {
  114.     $self next
  115. }
  116. SkelTopology instproc node? n {
  117.     $self instvar node_
  118.     if [info exists node_($n)] {
  119. set ret $node_($n)
  120.     } else {
  121. set ret ""
  122.     }
  123.     set ret
  124. }
  125. SkelTopology instproc add-fallback-links {ns nodelist bw delay qtype args} {
  126.    $self instvar node_
  127.     set n1 [lindex $nodelist 0]
  128.     foreach n2 [lrange $nodelist 1 end] {
  129. if ![info exists node_($n2)] {
  130.     set node_($n2) [$ns node]
  131. }
  132. $ns duplex-link $node_($n1) $node_($n2) $bw $delay $qtype
  133. foreach opt $args {
  134.     set cmd [lindex $opt 0]
  135.     set val [lindex $opt 1]
  136.     if {[llength $opt] > 2} {
  137. set x1 [lindex $opt 2]
  138. set x2 [lindex $opt 3]
  139.     } else {
  140. set x1 $n1
  141. set x2 $n2
  142.     }
  143.     $ns $cmd $node_($x1) $node_($x2) $val
  144.     $ns $cmd $node_($x2) $node_($x1) $val
  145. }
  146. set n1 $n2
  147.     }
  148. }
  149. Class NodeTopology/6nodes -superclass SkelTopology
  150. NodeTopology/6nodes instproc init ns {
  151.     $self next
  152.     $self instvar node_
  153.     set node_(n0) [$ns node]
  154.     set node_(n1) [$ns node]
  155.     set node_(n2) [$ns node]
  156.     set node_(n3) [$ns node]
  157.     set node_(n4) [$ns node]
  158.     set node_(n5) [$ns node]
  159. }
  160. Class Topology/net6 -superclass NodeTopology/6nodes
  161. #
  162. # Create a simple six node topology:
  163. #
  164. #                  n0
  165. #                 /                      
  166. #               n1    n2
  167. #              /    /  
  168. #             n3   n4   n5
  169. #
  170. # All links are of 1.5Mbps bandwidth with 10ms latency
  171. #
  172. Topology/net6 instproc init ns {
  173.     $self next $ns
  174.     $self instvar node_
  175.     Simulator set NumberInterfaces_ 1
  176.     $ns duplex-link $node_(n0) $node_(n1) 1.5Mb 10ms DropTail 
  177.     $ns duplex-link $node_(n0) $node_(n2) 1.5Mb 10ms DropTail 
  178.     $ns duplex-link $node_(n1) $node_(n3) 1.5Mb 10ms DropTail 
  179.     $ns duplex-link $node_(n1) $node_(n4) 1.5Mb 10ms DropTail 
  180.     $ns duplex-link $node_(n2) $node_(n4) 1.5Mb 10ms DropTail 
  181.     $ns duplex-link $node_(n2) $node_(n5) 1.5Mb 10ms DropTail 
  182.     if {[$class info instprocs config] != ""} {
  183. $self config $ns
  184.     }
  185. }
  186. Class NodeTopology/3nodes -superclass SkelTopology
  187. NodeTopology/3nodes instproc init ns {
  188.     $self next
  189.     $self instvar node_
  190.     set node_(n0) [$ns node]
  191.     set node_(n1) [$ns node]
  192.     set node_(n2) [$ns node]
  193. }
  194. Class Topology/net3 -superclass NodeTopology/3nodes
  195. #
  196. # Create a simple six node topology:
  197. #
  198. #                  n0
  199. #                 /                      
  200. #               n1 -- n2
  201. #
  202. # All links are of 1.5Mbps bandwidth with 10ms latency
  203. #
  204. Topology/net3 instproc init ns {
  205.     $self next $ns
  206.     $self instvar node_
  207.     $ns duplex-link $node_(n0) $node_(n1) 1.5Mb 10ms DropTail 
  208.     $ns duplex-link $node_(n0) $node_(n2) 1.5Mb 10ms DropTail 
  209.     $ns duplex-link $node_(n1) $node_(n2) 1.5Mb 10ms DropTail 
  210.     if {[$class info instprocs config] != ""} {
  211. $self config $ns
  212.     }
  213. }
  214. # Definition of test-suite tests
  215. # Testing algorithmic routing in a simple topology
  216. Class Test/Algo1 -superclass TestSuite
  217. Test/Algo1 instproc init net {
  218. $self instvar defNet_ test_ net_
  219. set defNet_ net3
  220. set test_ Algo1
  221. set net_ $net
  222. $self next
  223. }
  224. Test/Algo1 instproc run {} {
  225. $self instvar ns_ node_ testName_
  226.         $ns_ rtproto Algorithmic
  227. set grp0 [Node allocaddr]
  228.         set mproto CtrMcast
  229.         set mrthandle [$ns_ mrtproto $mproto {}]
  230.         if {$mrthandle != ""} {
  231.     $mrthandle set_c_rp $node_(n2)
  232. }
  233.         if {$mrthandle != ""} {
  234.     $ns_ at 0.3 "$mrthandle switch-treetype $grp0"
  235.         }
  236. set udp0 [new Agent/UDP]
  237. $ns_ attach-agent $node_(n1) $udp0
  238. $udp0 set dst_addr_ $grp0
  239. $udp0 set dst_port_ 0
  240. $udp0 set class_ 1
  241. set cbr0 [new Application/Traffic/CBR]
  242. $cbr0 attach-agent $udp0
  243. set rcvr0 [new Agent/LossMonitor]
  244. $ns_ attach-agent $node_(n0) $rcvr0
  245. set rcvr1 [new Agent/LossMonitor]
  246. $ns_ attach-agent $node_(n1) $rcvr1
  247. set rcvr2 [new Agent/LossMonitor]
  248. $ns_ attach-agent $node_(n2) $rcvr2
  249. $ns_ at 0.1 "$cbr0 start"
  250. $ns_ at 0.1 "$node_(n1) join-group $rcvr1 $grp0"
  251. $ns_ at 0.1 "$node_(n0) join-group $rcvr0 $grp0"
  252. $ns_ at 0.1 "$node_(n2) join-group $rcvr2 $grp0"
  253. $ns_ at 0.5 "$self finish [list $rcvr0 $rcvr1 $rcvr2]"
  254. $ns_ run
  255. }
  256. # Testing algorithmic routing with multicast in a simple topology
  257. Class Test/Algo2 -superclass TestSuite
  258. Test/Algo2 instproc init net {
  259. $self instvar defNet_ test_ net_
  260. set defNet_ net6
  261. set test_ Algo2
  262. set net_ $net
  263. $self next
  264. }
  265. Test/Algo2 instproc run {} {
  266. $self instvar ns_ node_ testName_
  267. set grp0 [Node allocaddr]
  268.         $ns_ rtproto Algorithmic
  269.         set mproto CtrMcast
  270.         set mrthandle [$ns_ mrtproto $mproto {}]
  271.         if {$mrthandle != ""} {
  272.     $mrthandle set_c_rp $node_(n2)
  273. }
  274.         if {$mrthandle != ""} {
  275.     $ns_ at 0.3 "$mrthandle switch-treetype $grp0"
  276.         }
  277. set udp0 [new Agent/UDP]
  278. $ns_ attach-agent $node_(n4) $udp0
  279. $udp0 set dst_addr_ $grp0
  280. $udp0 set dst_port_ 0
  281. $udp0 set class_ 1
  282. set cbr0 [new Application/Traffic/CBR]
  283. $cbr0 attach-agent $udp0
  284. set rcvr0 [new Agent/LossMonitor]
  285. $ns_ attach-agent $node_(n0) $rcvr0
  286. set rcvr1 [new Agent/LossMonitor]
  287. $ns_ attach-agent $node_(n1) $rcvr1
  288. set rcvr2 [new Agent/LossMonitor]
  289. $ns_ attach-agent $node_(n2) $rcvr2
  290. set rcvr3 [new Agent/LossMonitor]
  291. $ns_ attach-agent $node_(n3) $rcvr3
  292. set rcvr4 [new Agent/LossMonitor]
  293. $ns_ attach-agent $node_(n4) $rcvr4
  294. set rcvr5 [new Agent/LossMonitor]
  295. $ns_ attach-agent $node_(n5) $rcvr5
  296. $ns_ at 0.1 "$cbr0 start"
  297. $ns_ at 0.1 "$node_(n1) join-group $rcvr1 $grp0"
  298. $ns_ at 0.1 "$node_(n0) join-group $rcvr0 $grp0"
  299. $ns_ at 0.1 "$node_(n3) join-group $rcvr3 $grp0"
  300. $ns_ at 0.1 "$node_(n2) join-group $rcvr2 $grp0"
  301. $ns_ at 0.1 "$node_(n4) join-group $rcvr4 $grp0"
  302. $ns_ at 0.1 "$node_(n5) join-group $rcvr5 $grp0"
  303. $ns_ at 0.5 "$self finish [list $rcvr0 $rcvr1 $rcvr2 $rcvr3 $rcvr4 $rcvr5]"
  304. $ns_ run
  305. }
  306. TestSuite runTest