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

通讯编程

开发平台:

Visual C++

  1. #Copyright (c) 1997 Regents of the University of California.
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. # 1. Redistributions of source code must retain the above copyright
  8. #    notice, this list of conditions and the following disclaimer.
  9. # 2. Redistributions in binary form must reproduce the above copyright
  10. #    notice, this list of conditions and the following disclaimer in the
  11. #    documentation and/or other materials provided with the distribution.
  12. # 3. All advertising materials mentioning features or use of this software
  13. #    must display the following acknowledgement:
  14. #      This product includes software developed by the Computer Systems
  15. #      Engineering Group at Lawrence Berkeley Laboratory.
  16. # 4. Neither the name of the University nor of the Laboratory may be used
  17. #    to endorse or promote products derived from this software without
  18. #    specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. # SUCH DAMAGE.
  31. #
  32. # Each agent keeps track of what messages it has seen
  33. # and only forwards those which it hasn't seen before.
  34. # Each message is of the form "ID:DATA" where ID is some arbitrary
  35. # message identifier and DATA is the payload.  In order to reduce
  36. # memory usage, the agent stores only the message ID.
  37. # Note that I have not put in any mechanism to expire old message IDs
  38. # from the list of seen messages.  There also isn't any mechanism
  39. # for assigning message IDs.
  40. Mac/Simple set bandwidth_ 1Mb
  41. set MESSAGE_PORT 42
  42. set BROADCAST_ADDR -1
  43. # variables which control the number of nodes and how they're grouped
  44. # (see topology creation code below)
  45. set group_size 4
  46. set num_groups 6
  47. set num_nodes [expr $group_size * $num_groups]
  48. set val(chan)           Channel/WirelessChannel    ;#Channel Type
  49. set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
  50. set val(netif)          Phy/WirelessPhy            ;# network interface type
  51. #set val(mac)            Mac/802_11                 ;# MAC type
  52. #set val(mac)            Mac                 ;# MAC type
  53. set val(mac) Mac/Simple
  54. set val(ifq)            Queue/DropTail/PriQueue    ;# interface queue type
  55. set val(ll)             LL                         ;# link layer type
  56. set val(ant)            Antenna/OmniAntenna        ;# antenna model
  57. set val(ifqlen)         50                         ;# max packet in ifq
  58. # DumbAgent, AODV, and DSDV work.  DSR is broken
  59. set val(rp) DumbAgent
  60. #set val(rp)             DSDV
  61. #set val(rp)             DSR
  62. #set val(rp)  AODV
  63. # size of the topography
  64. set val(x)              [expr 120*$group_size + 500]
  65. set val(y)              [expr 240*$num_groups + 200]
  66. set ns [new Simulator]
  67. set f [open wireless-flooding-$val(rp).tr w]
  68. $ns trace-all $f
  69. set nf [open wireless-flooding-$val(rp).nam w]
  70. $ns namtrace-all-wireless $nf $val(x) $val(y)
  71. $ns use-newtrace
  72. # set up topography object
  73. set topo       [new Topography]
  74. $topo load_flatgrid $val(x) $val(y)
  75. #
  76. # Create God
  77. #
  78. create-god $num_nodes
  79. set chan_1_ [new $val(chan)]
  80. $ns node-config -adhocRouting $val(rp) 
  81.                 -llType $val(ll) 
  82.                 -macType $val(mac) 
  83.                 -ifqType $val(ifq) 
  84.                 -ifqLen $val(ifqlen) 
  85.                 -antType $val(ant) 
  86.                 -propType $val(prop) 
  87.                 -phyType $val(netif) 
  88.                 -topoInstance $topo 
  89.                 -agentTrace ON 
  90.                 -routerTrace OFF 
  91.                 -macTrace ON 
  92.                 -movementTrace OFF 
  93.                 -channel $chan_1_ 
  94. # subclass Agent/MessagePassing to make it do flooding
  95. Class Agent/MessagePassing/Flooding -superclass Agent/MessagePassing
  96. Agent/MessagePassing/Flooding instproc recv {source sport size data} {
  97.     $self instvar messages_seen node_
  98.     global ns BROADCAST_ADDR 
  99.     # extract message ID from message
  100.     set message_id [lindex [split $data ":"] 0]
  101.     puts "nNode [$node_ node-addr] got message $message_idn"
  102.     if {[lsearch $messages_seen $message_id] == -1} {
  103. lappend messages_seen $message_id
  104.         $ns trace-annotate "[$node_ node-addr] received {$data} from $source"
  105.         $ns trace-annotate "[$node_ node-addr] sending message $message_id"
  106. $self sendto $size $data $BROADCAST_ADDR $sport
  107.     } else {
  108.         $ns trace-annotate "[$node_ node-addr] received redundant message $message_id from $source"
  109.     }
  110. }
  111. Agent/MessagePassing/Flooding instproc send_message {size message_id data port} {
  112.     $self instvar messages_seen node_
  113.     global ns MESSAGE_PORT BROADCAST_ADDR
  114.     lappend messages_seen $message_id
  115.     $ns trace-annotate "[$node_ node-addr] sending message $message_id"
  116.     $self sendto $size "$message_id:$data" $BROADCAST_ADDR $port
  117. }
  118. # create a bunch of nodes
  119. for {set i 0} {$i < $num_nodes} {incr i} {
  120.     set n($i) [$ns node]
  121.     $n($i) set Y_ [expr 230*floor($i/$group_size) + 160*(($i%$group_size)>=($group_size/2))]
  122.     $n($i) set X_ [expr (90*$group_size)*($i/$group_size%2) + 200*($i%($group_size/2))]
  123.     $n($i) set Z_ 0.0
  124.     $ns initial_node_pos $n($i) 20
  125. }
  126. # attach a new Agent/MessagePassing/Flooding to each node on port $MESSAGE_PORT
  127. for {set i 0} {$i < $num_nodes} {incr i} {
  128.     set a($i) [new Agent/MessagePassing/Flooding]
  129.     $n($i) attach  $a($i) $MESSAGE_PORT
  130.     $a($i) set messages_seen {}
  131. }
  132. # now set up some events
  133. $ns at 0.2 "$a(1) send_message 200 1 {first message}  $MESSAGE_PORT"
  134. $ns at 0.4 "$a([expr $num_nodes/2]) send_message 600 2 {some big message} $MESSAGE_PORT"
  135. $ns at 0.7 "$a([expr $num_nodes-2]) send_message 200 3 {another one} $MESSAGE_PORT"
  136. $ns at 1.0 "finish"
  137. proc finish {} {
  138.         global ns f nf val
  139.         $ns flush-trace
  140.         close $f
  141.         close $nf
  142. #        puts "running nam..."
  143.         exec nam wireless-flooding-$val(rp).nam &
  144.         exit 0
  145. }
  146. $ns run