Ns2 节点不进行并发 http 请求

Ns2 nodes do not make concurrent http requests

我写了一个 ns2 脚本来 运行 来自 80 个节点中的 40 个节点的并发 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"

问题出在这段代码中:

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 
}

列表是一种很好的方式来保存简单的事物集合。