Advertisement
coding_2018

class 3-5-18

May 2nd, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 3.32 KB | None | 0 0
  1. #Create a simulator object set ns [new Simulator]
  2. #Open the output files
  3. set f0 [open out0.tr w]
  4. set f1 [open out1.tr w]
  5. set f2 [open out2.tr w]
  6. #Create 5 nodes
  7. set n0 [$ns node]
  8. set n1 [$ns node]
  9. set n2 [$ns node]
  10. set n3 [$ns node]
  11. set n4 [$ns node]
  12. #Connect the nodes
  13. $ns duplex-link $n0 $n3 1Mb 100ms DropTail
  14. $ns duplex-link $n1 $n3 1Mb 100ms DropTail
  15. $ns duplex-link $n2 $n3 1Mb 100ms DropTail
  16. $ns duplex-link $n3 $n4 1Mb 100ms DropTail
  17. #Define a 'finish' procedure
  18. proc finish {} {
  19. global f0 f1 f2
  20. #Close the output files
  21. close $f0
  22. close $f1
  23. close $f2
  24. exit 0
  25. }
  26. #Define a procedure that attaches a UDP agent to a previously created node
  27. #'node' and attaches an Expoo traffic generator to the agent with the
  28. #characteristic values 'size' for packet size 'burst' for burst time,
  29. #'idle' for idle time and 'rate' for burst peak rate. The procedure connects
  30. #the source with the previously defined traffic sink 'sink' and returns the
  31. #source object.
  32. proc attach-expoo-traffic { node sink size burst idle rate } {
  33. #Get an instance of the simulator
  34. set ns [Simulator instance]
  35. #Create a UDP agent and attach it to the node
  36. set source [new Agent/UDP]
  37. $ns attach-agent $node $source
  38. #Create an Expootraffic agent and set its configuration parameters
  39. set traffic [new Application/Traffic/Exponential]
  40. $traffic set packetSize_ $size
  41. $traffic set burst_time_ $burst
  42. $traffic set idle_time_ $idle
  43. $traffic set rate_ $rate
  44. # Attach traffic source to the traffic generator
  45. $traffic attach-agent $source
  46. #Connect the source and the sink
  47. $ns connect $source $sink
  48. return $traffic
  49. }
  50. #Define a procedure which periodically records the bandwidth received by the
  51. #three traffic sinks sink0/1/2 and writes it to the three files f0/1/2.
  52. proc record {} {
  53. global sink0 sink1 sink2 f0 f1 f2
  54. #Get an instance of the simulator
  55. set ns [Simulator instance]
  56. #Set the time after which the procedure should be called again
  57. set time 0.5
  58. #How many bytes have been received by the traffic sinks?
  59. set bw0 [$sink0 set bytes_]
  60. set bw1 [$sink1 set bytes_]
  61. set bw2 [$sink2 set bytes_]
  62. #Get the current time
  63. set now [$ns now]
  64. #Calculate the bandwidth (in MBit/s) and write it to the files
  65. puts $f0 "$now [expr $bw0/$time*8/1000000]"
  66. puts $f1 "$now [expr $bw1/$time*8/1000000]"
  67. puts $f2 "$now [expr $bw2/$time*8/1000000]"
  68. #Reset the bytes_ values on the traffic sinks
  69. $sink0 set bytes_ 0
  70. $sink1 set bytes_ 0
  71. $sink2 set bytes_ 0
  72. #Re-schedule the procedure
  73. $ns at [expr $now+$time] "record"
  74. }
  75. #Create three traffic sinks and attach them to the node n4
  76. set sink0 [new Agent/LossMonitor]
  77. set sink1 [new Agent/LossMonitor]
  78. set sink2 [new Agent/LossMonitor]
  79. $ns attach-agent $n4 $sink0
  80. $ns attach-agent $n4 $sink1
  81. $ns attach-agent $n4 $sink2
  82. #Create three traffic sources
  83. set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]
  84. set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]
  85. set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]
  86. #Start logging the received bandwidth
  87. $ns at 0.0 "record"
  88. #Start the traffic sources
  89. $ns at 10.0 "$source0 start"
  90. $ns at 10.0 "$source1 start"
  91. $ns at 10.0 "$source2 start"
  92. #Stop the traffic sources
  93. $ns at 50.0 "$source0 stop"
  94. $ns at 50.0 "$source1 stop"
  95. $ns at 50.0 "$source2 stop"
  96. #Call the finish procedure after 60 seconds simulation time
  97. $ns at 60.0 "finish"
  98. #Run the simulation
  99. $ns run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement