McastMonitor.tcl
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:4k
源码类别:

通讯编程

开发平台:

Visual C++

  1. Class McastMonitor
  2. McastMonitor instproc init {} {
  3.     $self instvar period_ ns_
  4.     set ns_ [Simulator instance]
  5.     set period_ 0.03
  6. }
  7. McastMonitor instproc trace-topo {} {
  8.     $self instvar ns_ period_
  9.     $self trace-links [$ns_ all-links-list]
  10. }
  11. McastMonitor instproc trace-links links {
  12.     $self instvar pktmon_
  13.     foreach l $links {
  14. set pktmon_($l) [new PktInTranMonitor]
  15. $pktmon_($l) attach-link $l
  16. $l add-pktmon $pktmon_($l)
  17.     }
  18. }
  19. McastMonitor instproc filter {header field value} {
  20.     $self instvar pktmon_
  21.     foreach index [array name pktmon_] {
  22. $pktmon_($index) filter $header $field $value
  23.     }
  24. }
  25. McastMonitor instproc pktintran {} {
  26.     $self instvar ns_ pktmon_
  27.     set total 0
  28.     foreach index [array name pktmon_] {
  29. if {[$index up?] == "up"} {
  30.     incr total [$pktmon_($index) pktintran]
  31. }
  32.     }
  33.     return $total
  34. }
  35. McastMonitor instproc print-trace {} {
  36.     $self instvar ns_ period_ file_
  37.     if [info exists file_] {
  38. puts $file_ "[$ns_ now] [$self pktintran]"
  39.     } else {
  40. puts "[$ns_ now] [$self pktintran]"
  41.     }
  42.     $ns_ at [expr [$ns_ now] + $period_] "$self print-trace"
  43. }
  44. McastMonitor instproc attach file {
  45.     $self instvar file_
  46.     set file_ $file
  47. }
  48. ###############
  49. ###############################Pkt In Transit Monitor###################
  50. ### constructed by
  51. ### front filter and front counter: keep track of #pkts passed into link
  52. ### rear filter and rear counter: keep track of #pkts passed out of link
  53. ### front count - rear count = #pkt in transit
  54. ###
  55. Class PktInTranMonitor
  56. PktInTranMonitor instproc init {} {
  57.     $self instvar period_ ns_ front_counter_ rear_counter_ front_filter_ rear_filter_ 
  58.     set ns_ [Simulator instance]
  59.     set period_ 0.03
  60.     set front_counter_ [new PktCounter]
  61.     $front_counter_ set pktInTranMonitor_ $self
  62.     set front_filter_ [new Filter/MultiField]
  63.     $front_filter_ filter-target $front_counter_
  64.     set rear_counter_ [new PktCounter]
  65.     $rear_counter_ set pktInTranMonitor_ $self
  66.     set rear_filter_ [new Filter/MultiField]
  67.     $rear_filter_ filter-target $rear_counter_
  68. }
  69. PktInTranMonitor instproc reset {} {
  70.     $self instvar front_counter_ rear_counter_  ns_ next_
  71.     $front_counter_ reset
  72.     $rear_counter_ reset
  73.     if {[info exist next_] && $next_ != 0} {
  74. $next_ reset
  75.     }
  76. }
  77. PktInTranMonitor instproc filter {header field value} {
  78.     $self instvar front_filter_ rear_filter_
  79.     $front_filter_ filter-field [PktHdr_offset PacketHeader/$header $field] $value
  80.     $rear_filter_ filter-field [PktHdr_offset PacketHeader/$header $field] $value
  81. }
  82. PktInTranMonitor instproc attach-link link {
  83.     $self instvar front_filter_ rear_filter_ front_counter_ rear_counter_
  84.     
  85.     set tmp [$link head]
  86.     while {[$tmp target] != [$link link]} {
  87.         set tmp [$tmp target]
  88.     }
  89.     $tmp target $front_filter_
  90.     $front_filter_ target [$link link]
  91.     $front_counter_ target [$link link]
  92.     $rear_filter_ target [[$link link] target]
  93.     $rear_counter_ target [[$link link] target]
  94.     [$link link] target $rear_filter_
  95. }
  96. PktInTranMonitor instproc attach file {
  97.     $self instvar file_
  98.     set file_ $file
  99. }
  100. PktInTranMonitor instproc pktintran {} {
  101.     $self instvar front_counter_ rear_counter_ 
  102.     return [expr [$front_counter_ value] - [$rear_counter_ value]]
  103. }
  104. PktInTranMonitor instproc output {} {
  105.     $self instvar front_counter_ rear_counter_ ns_ file_ 
  106.     puts $file_ "[$ns_ now] [expr [$front_counter_ value] - [$rear_counter_ value]]"
  107. }
  108.     
  109. PktInTranMonitor instproc periodical-output {} {
  110.     $self instvar period_ ns_
  111.     $self output
  112.     $ns_ at [expr [$ns_ now] + $period_] "$self periodical-output"
  113. }
  114. ################
  115. Simulator instproc all-links-list {} {
  116.     $self instvar link_
  117.     set links ""
  118.     foreach n [array names link_] {
  119. lappend links $link_($n)
  120.     }
  121.     set links
  122. }
  123. Link instproc add-pktmon pktmon {
  124.     $self instvar pktmon_
  125.     if [info exists pktmon_] {
  126. $pktmon set next_ $pktmon_
  127.     } else {
  128. $pktmon set next_ 0
  129.     }
  130.     set pktmon_ $pktmon
  131. }