2015-04-27 56 views
0

我編寫了一個ns2腳本來運行來自80個節點中的40個節點的併發HTTP請求。Ns2節點不會發出併發http請求

但是,仿真顯示一個節點而不是40個節點向HTTP服務器發出請求。

有人可以請澄清什麼可能是錯誤的代碼?

該代碼在下面提供

謝謝你的時間,每個人。

set ns [new Simulator] 
set clswitch [$ns node] # the access layer switch 
set distrswitch [$ns node] # the distribution switch 
set cch [$ns node] # the cache 
set websrv [$ns node] # the web server 
set traceFile [open Asim.tr w] 
set namTraceFile [open Asim.nam w] 
$ns namtrace-all $namTraceFile 
$ns trace-all $traceFile 
proc finish {} { 
    global ns traceFile namTraceFile 
    $ns flush-trace-all 
    puts "Simulation completed." 
    flush $traceFile 
    flush $namTraceFile 
    close $traceFile 
    close $namTraceFile 
    exit 0 
} 

for {set i 0} {$i < 80} {incr i} { 
    if {$i % 2 == 0} { 
     set cl ($i) [$ns node] 
     $ns duplex-link $cl($i) $clswitch 100Mb 10ms DropTail 
     set tcpAgent [new Agent/TCP] 
     $ns attach-agent $cl($i) $tcpAgent 
     set tcpSink [new Agent/TCPSink] 
     $ns attach-agent $clswitch $tcpSink 
     $ns connect $tcpAgent $tcpSink 
     set client [new Http/Client $ns $cl($i)] 
     proc start-connection {} { 
      global ns client cache server 
      $client connect $cache 
      $cache connect $server 
      $client start-session $cache $server 
     } 
     $ns at 0.5 "start-connection" 
    } 
} 

$ns duplex-link $clswitch $distrswitch 100Mb 10ms DropTail 
set tcpAgent [new Agent/TCP] 
$ns attach-agent $clswitch $tcpAgent 
set tcpSink [new Agent/TCPSink] 
$ns attach-agent $distrswitch $tcpSink 
$ns connect $tcpAgent $tcpSink 
$ns duplex-link $distrswitch $cch 100Mb 10ms DropTail 
set tcpAgent [new Agent/TCP] 
$ns attach-agent $distrswitch $tcpAgent 
set tcpSink [new Agent/TCPSink] 
$ns attach-agent $cch $tcpSink 
$ns connect $tcpAgent $tcpSink 
$ns duplex-link $cch $websrv 100Mb 10ms DropTail 
set tcpAgent [new Agent/TCP] 
$ns attach-agent $cch $tcpAgent 
set tcpSink [new Agent/TCPSink] 
$ns attach-agent $websrv $tcpSink 
$ns connect $tcpAgent $tcpSink 
set server [new Http/Server $ns $websrv] 
set cache [new Http/Cache $ns $cch] 
set pgp [new PagePool/Math] 
set tmp [new RandomVariable/Constant] 
$tmp set val_ 4096 
$pgp ranvar-size $tmp 
set tmp [new RandomVariable/Exponential] 
$tmp set avg_ 6 
$pgp ranvar-age $tmp 
$server set-page-generator $pgp 
set tmp [new RandomVariable/Exponential] 
$tmp set avg_ 0.5 
$client set-interval-generator $tmp 
$client set-page-generator $pgp 
$ns at 6.0 "finish" 

回答

2

的問題是在此代碼:

for {set i 0} {$i < 80} {incr i} { 
    if {$i % 2 == 0} { 
     set cl ($i) [$ns node] 
     $ns duplex-link $cl($i) $clswitch 100Mb 10ms DropTail 
     set tcpAgent [new Agent/TCP] 
     $ns attach-agent $cl($i) $tcpAgent 
     set tcpSink [new Agent/TCPSink] 
     $ns attach-agent $clswitch $tcpSink 
     $ns connect $tcpAgent $tcpSink 
     set client [new Http/Client $ns $cl($i)] 
     proc start-connection {} { 
      global ns client cache server 
      $client connect $cache 
      $cache connect $server 
      $client start-session $cache $server 
     } 
     $ns at 0.5 "start-connection" 
    } 
} 

首先,你定義一個循環內的過程。這是一個不好的跡象(因爲你沒有做複雜的運行時代碼生成)。但真正的問題是,當你創造了許多客戶,你想他們都儲存在同一個簡單的變量:

 set client [new Http/Client $ns $cl($i)] 

通過每一次循環,這被覆蓋;只有最後一個這樣的客戶纔會開始。我們需要做的是通過爭論將客戶轉交給程序。

##### MOVED, AND client IS NOW AN ARGUMENT, NOT A GLOBAL ##### 
proc start-connection {client} { 
    global ns cache server 
    $client connect $cache 
    $cache connect $server 
    $client start-session $cache $server 
} 

##### Now same as above... except for a couple of lines ##### 
for {set i 0} {$i < 80} {incr i} { 
    if {$i % 2 == 0} { 
     set cl ($i) [$ns node] 
     $ns duplex-link $cl($i) $clswitch 100Mb 10ms DropTail 
     set tcpAgent [new Agent/TCP] 
     $ns attach-agent $cl($i) $tcpAgent 
     set tcpSink [new Agent/TCPSink] 
     $ns attach-agent $clswitch $tcpSink 
     $ns connect $tcpAgent $tcpSink 
     set client [new Http/Client $ns $cl($i)] 
     ##### THE NEXT LINE IS CHANGED FROM YOUR CODE! ##### 
     $ns at 0.5 [list start-connection $client] 
     ##### KEEP TRACK OF ALL THE CLIENTS ##### 
     lappend allClients $client 
    } 
} 

我們還需要稍後再做一點工作,在某些較低代碼中引用「客戶端」。該工作只需要在遍歷客戶列表的循環中完成。

foreach client $allClients { 
    $client set-interval-generator $tmp 
    $client set-page-generator $pgp 
} 

列表是保持事物的簡單集合的偉大方式。