SUBANGKAR

Wired

Jan 3rd, 2019
643
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #create an ns ofject
  2.  
  3. set ns    [new Simulator]
  4.  
  5. #create and open trace file
  6. set tr wired.tr
  7. set nm wired.nam
  8.  
  9.  
  10. set tracefile [open $tr w]
  11. $ns trace-all $tracefile
  12.  
  13. set namfile [open $nm w]
  14. $ns namtrace-all $namfile
  15.  
  16.  
  17. #taking inputs from shell
  18.  
  19. set num_node [lindex $argv 0]
  20. set num_flows [lindex $argv 1]
  21. set packet_rate [lindex $argv 2]
  22. #packet_rate = packet per second
  23.  
  24. set cbr_size 1000
  25. set cbr_rate 11.0Mb
  26. set cbr_interval [expr 1.0/$packet_rate]
  27.  
  28. set grid_x_dim 500
  29. set grid_y_dim 500
  30.  
  31.  
  32. #Creating nodes
  33. puts "starting node creation..."
  34.  
  35. for {set i 0} {$i < $num_node} {incr i} {
  36.     set node_($i) [$ns node]
  37.  
  38.     set row  [expr ($i/10)]
  39.     set colm [expr ($i%10)]
  40.     set row [expr ($row * 50)]
  41.     set colm [expr ($colm * 50)]
  42.  
  43.     set x_pos [expr int($row)] ; #random settings
  44.     set y_pos [expr int($colm)] ; #random settings
  45.    
  46.  
  47.     $node_($i) set X_ $x_pos;
  48.     $node_($i) set Y_ $y_pos;
  49.     $node_($i) set Z_ 0.0
  50.  
  51.     # puts -nonewline $topo_file "$i x: [$node_($i) set X_] y: [$node_($i) set Y_] \n"
  52. }
  53.  
  54. #creating edges in a grid-like way
  55.  
  56. puts "Creating Edges between the nodes..."
  57.  
  58.  
  59. for {set i 0} {$i < $num_node} {incr i} {
  60.  
  61.     set right [expr $i + 1]
  62.     set down [expr $i + 10]
  63.  
  64.     set val [expr $i % 10]
  65.  
  66.  
  67.     if {$val != 9} {
  68.         $ns duplex-link $node_($i) $node_($right) 5Mb 2ms DropTail
  69.     }
  70.  
  71.     if {$down < [expr $num_node-1]} {
  72.         $ns duplex-link $node_($i) $node_($down) 5Mb 2ms DropTail
  73.     }
  74. }
  75.  
  76.  
  77. #Create flows and associate them with nodes
  78.  
  79. for {set i 0} {$i < $num_flows} {incr i} {
  80.    
  81.     set tcp_($i) [new Agent/TCP]
  82.     $tcp_($i) set class_ $i
  83.     $tcp_($i) set fid_ $i
  84.  
  85.     set tcpsink_($i) [new Agent/TCPSink]
  86.    
  87.     if { [expr $i%2] == 0} {
  88.         $ns color $i Blue
  89.     } else {
  90.         $ns color $i Red
  91.     }
  92.    
  93. }
  94.  
  95.  
  96. for {set i 0} {$i < $num_flows} {incr i} {
  97.  
  98.     set t [expr (rand() * $num_node)]
  99.     set tt [expr int($t)]
  100.  
  101.     set t1 [expr (rand() * $num_node)]
  102.     set tt1 [expr int($t1)]
  103.  
  104.     set src_no [expr $tt % $num_node]
  105.     set sink_no [expr $tt1 % $num_node]
  106.    
  107.     while {$src_no == $sink_no} {
  108.         set t [expr (rand() * $num_node)]
  109.         set tt [expr int($t)]
  110.  
  111.         set t1 [expr (rand() * $num_node)]
  112.         set tt1 [expr int($t1)]
  113.  
  114.         set src_no [expr $tt % $num_node]
  115.         set sink_no [expr $tt1 % $num_node]
  116.  
  117.     }
  118.  
  119.     # puts "src_no = $src_no && sink_no = $sink_no"
  120.  
  121.     $ns attach-agent $node_($src_no) $tcp_($i)
  122.     $ns attach-agent $node_($sink_no) $tcpsink_($i)
  123.  
  124.     set ftp [new Application/FTP]
  125.     $ftp attach-agent $tcp_($i)
  126.  
  127.     $ns at 2.0 "$ftp start"
  128.     $ns at 10.0 "finish"
  129. }
  130.  
  131.  
  132. for {set i 0} {$i < $num_flows } {incr i} {
  133.      $ns connect $tcp_($i) $tcpsink_($i)
  134. }
  135.  
  136.  
  137. for {set i 0} {$i < $num_flows } {incr i} {
  138.     set cbr_($i) [new Application/Traffic/CBR]
  139.     $cbr_($i) set packetSize_ $cbr_size
  140.     $cbr_($i) set rate_ $cbr_rate
  141.    
  142.     $cbr_($i) set interval_ $cbr_interval
  143.     $cbr_($i) attach-agent $tcp_($i)
  144. }
  145.  
  146.  
  147. for {set i 0} {$i < $num_flows } {incr i} {
  148.      $ns at 1 "$cbr_($i) start"
  149. }
  150.  
  151.  
  152. #finish procedure
  153.  
  154. proc finish {} {
  155.     puts "finishing"
  156.    
  157.     global ns tr nm  namfile tracefile
  158.     $ns flush-trace
  159.     close $namfile
  160.     close $tracefile
  161.     # exec nam $nm &
  162.     exit 0
  163. }
  164.  
  165.  
  166. $ns run
RAW Paste Data