Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 4.80 KB | None | 0 0
  1. set ns [new Simulator]
  2. set outputfile [open out.tr w]
  3. $ns trace-all $outputfile
  4.  
  5. set nf [open out.nam w]
  6. $ns namtrace-all $nf
  7.  
  8. #fichiers d'output
  9.  
  10. proc finish {} {
  11.     global ns nf outputfile
  12.     $ns flush-trace
  13.     close $nf
  14.     close $outputfile
  15.     puts "nam se lance.."
  16.     exec nam -a out.nam &
  17.    
  18.     exit 0
  19. }
  20.  
  21. #parsing des fichiers
  22. proc plotWindow {tcpSource outfile} {
  23.     global ns
  24.     set time 0.1
  25.     set now [$ns now]
  26.     set cwnd [$tcpSource set cwnd_]
  27.     set ssth [$tcpSource set ssthresh_]
  28.     set window [$tcpSource set window_]
  29.     set maxseq [$tcpSource set maxseq_]
  30.  
  31.     set debitUtile [expr $maxseq/$now]
  32.  
  33.     ###Print TIME CWND   for  gnuplot to plot progressing on CWND
  34.     puts  $outfile  "$now $cwnd $ssth $window $maxseq $debitUtile"
  35.  
  36.     $ns at [expr $now+$time] "plotWindow $tcpSource $outfile"
  37. }
  38.  
  39. #decrementer une variable
  40. proc decr { n reste } {
  41.     set reste [expr $reste - $n]
  42.     return $reste
  43. }
  44.  
  45. #volume aleatoire
  46. proc randomVolume { value } {
  47.     set min_volume 1500
  48.     set res [expr {int($value*rand()) - $min_volume}]
  49.     return $res
  50. }
  51.  
  52. #depart aleatoire
  53. proc randomDeparture { min max } {
  54.     set res [expr {int($max*rand()) - $min}]
  55.  
  56.     return $res
  57. }
  58.  
  59.  
  60. if { 0 } {
  61.     proc randomDeparture05 { } {
  62.         set maxFactor [expr [expr $max + 1] - $min]
  63.         set res [expr int([expr rand() * 100])]
  64.         set res [expr [expr $res % $maxFactor] + $min]
  65.     }
  66. }
  67.  
  68.  
  69. #parse le fichier traff
  70. set fp_traf [open "traff.traf" r]
  71. set file_traf [read $fp_traf]
  72. close $fp_traf
  73.  
  74. #creation des noeuds
  75. set n_ -1
  76. set data_traf [split $file_traf "\n"]
  77. foreach line $data_traf {
  78.     if { $n_ < [lindex $line 0] } {
  79.         set n_ [lindex $line 0]
  80.         set n($n_) [$ns node]
  81.     }
  82. }
  83.  
  84. #parse le fichier topo
  85. set fp_topo [open "topo.top" r]
  86. set file_topo [read $fp_topo]
  87. close $fp_topo
  88.  
  89.  
  90. set data [split $file_topo "\n"]
  91. foreach line $data {
  92.     #creation des liens
  93.  
  94.     $ns duplex-link $n([lindex $line 0]) $n([lindex $line 1]) [lindex $line 2]Gb/s [lindex $line 3]ms DropTail
  95.     $ns queue-limit $n([lindex $line 0]) $n([lindex $line 1]) 30
  96.     #position queue
  97.     $ns duplex-link-op $n([lindex $line 0]) $n([lindex $line 1]) queuePos 0.1
  98.     #puts "$n([lindex $line 0]) [lindex $line 1]"
  99.  
  100. }
  101.  
  102.  
  103. set flux 0
  104.  
  105. #traff en Mb donc tout passer octets d'abord
  106.  
  107. #pour chaque volume de donnees
  108. #rrajouter borne sur le temps
  109. set sent 0
  110. #set time 0.1
  111. #set now [$ns now]
  112.  
  113. #vérifier qu'on envoie bien au minimum 1500 octets
  114.  
  115. foreach line $data_traf {
  116.     set noeud1 [lindex $line 0]
  117.     set noeud2 [lindex $line 1]
  118.  
  119.  
  120.     #[ createTCP $ns $flux $noeud1 $noeud2 ]
  121.  
  122.     set tcp($flux) [new Agent/TCP/Reno]
  123.     $ns attach-agent $n($noeud1) $tcp($flux)
  124.     $tcp($flux) set packetSize_ 1500
  125.  
  126.  
  127.     #tcp sink
  128.     set sink($flux) [new Agent/TCPSink]
  129.     $ns attach-agent $n($noeud2) $sink($flux)
  130.  
  131.  
  132.     #Connection between the TCP source and the TCP sink
  133.     $ns connect $tcp($flux) $sink($flux)
  134.  
  135.     set volume [lindex $line 2]
  136.     set volume [expr $volume * 125000]
  137.  
  138.  
  139.     set sent $volume
  140.     set vol_sent [ randomVolume $volume ]
  141.     set reste [expr $sent - $vol_sent]
  142.  
  143.     set dep [format "%.4f" [expr {5.0*rand()}]]
  144.     #set dep [ randomDeparture 0 5 ]
  145.     $ns at $dep "$tcp($flux) send $vol_sent"
  146.     puts "$reste $vol_sent $dep $flux"
  147.  
  148.     #incr flux
  149.     #envoie des donnees
  150.     while { $reste > 0 && $vol_sent >= 0 } {
  151.  
  152.         incr flux
  153.  
  154.  
  155.         #depart aleatoire
  156.         set dep [ randomDeparture 0 250 ]
  157.         #volume envoyé aléatoire
  158.         set vol_sent [ randomVolume $reste ]
  159.  
  160.         #cas ou vol_sent etait egal a -1
  161.         if { $vol_sent < 0 } {
  162.             set vol_sent $reste
  163.         }
  164.  
  165.         #cas ou volume = 0
  166.         if { $vol_sent == 0 } {
  167.             set vol_sent $reste
  168.             set tcp($flux) [new Agent/TCP/Reno]
  169.             $ns attach-agent $n($noeud1) $tcp($flux)
  170.             $tcp($flux) set packetSize_ 1500
  171.  
  172.  
  173.  
  174.             #tcp sink
  175.             set sink($flux) [new Agent/TCPSink]
  176.             $ns attach-agent $n($noeud2) $sink($flux)
  177.  
  178.             #Connection between the TCP source and the TCP sink
  179.             $ns connect $tcp($flux) $sink($flux)
  180.             $ns at $dep "$tcp($flux) send $vol_sent"
  181.             puts "$reste $vol_sent $dep $flux"
  182.  
  183.             incr flux
  184.  
  185.             break
  186.  
  187.         } else {
  188.            
  189.             if { $vol_sent <= $reste && $vol_sent >= 1500 } {
  190.                 set tcp($flux) [new Agent/TCP/Reno]
  191.                 $ns attach-agent $n($noeud1) $tcp($flux)
  192.                 $tcp($flux) set packetSize_ 1500
  193.  
  194.  
  195.  
  196.                 #tcp sink
  197.                 set sink($flux) [new Agent/TCPSink]
  198.                 $ns attach-agent $n($noeud2) $sink($flux)
  199.  
  200.                 #Connection between the TCP source and the TCP sink
  201.                 $ns connect $tcp($flux) $sink($flux)
  202.                 $ns at $dep "$tcp($flux) send vol_sent"
  203.                 puts "$reste $vol_sent $dep $flux"
  204.  
  205.                 #on soustrait le volume envoye au total
  206.                 set reste [decr $vol_sent $reste]
  207.  
  208.                 incr flux
  209.  
  210.             }
  211.  
  212.         }
  213.  
  214.     }
  215.  
  216. }
  217.  
  218.  
  219. #set test1 [randomDeparture]
  220. #puts [format "%.4f" $test1]
  221.  
  222. $ns at 300.0 "finish"
  223.  
  224. $ns run
  225.  
  226. #https://gitlab.unistra.fr/felsoci/ns2/blob/master/produce.py
  227. #https://wiki.tcl-lang.org/page/How+do+I+read+and+write+files+in+Tcl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement