If statement Ns2节点不进行并发http请求

If statement Ns2节点不进行并发http请求,if-statement,for-loop,tcl,ns2,If Statement,For Loop,Tcl,Ns2,我编写了一个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] # t

我编写了一个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"
set ns[新模拟器]
设置clswitch[$ns node]#访问层交换机
设置配电开关[$ns node]#配电开关
设置cch[$ns node]#缓存
将websrv[$ns node]#设置为web服务器
设置跟踪文件[打开Asim.tr w]
设置namTraceFile[打开Asim.nam w]
$ns namtrace all$namTraceFile
$ns跟踪所有$traceFile
过程完成{}{
全局ns traceFile namTraceFile
$ns刷新跟踪所有
将“模拟完成”
刷新$traceFile
刷新$namTraceFile
关闭$traceFile
关闭$namTraceFile
出口0
}
对于{set i 0}{$i<80}{incr i}{
如果{$i%2==0}{
设置cl($i)[$ns节点]
$ns双工链路$cl($i)$clswitch 100Mb 10ms DropTail
设置tcpAgent[新代理/TCP]
$ns附加代理$cl($i)$tcpAgent
设置tcpSink[新代理/tcpSink]
$ns附加代理$clswitch$tcpSink
$ns connect$tcpAgent$tcpSink
设置客户端[新Http/客户端$ns$cl($i)]
进程启动连接{}{
全局ns客户端缓存服务器
$client connect$cache
$cache connect$server
$client start session$cache$server
}
0.5“开始连接时为$ns”
}
}
$ns双工链路$clswitch$DistributSwitch 100Mb 10ms DropTail
设置tcpAgent[新代理/TCP]
$ns附加代理$clswitch$tcpAgent
设置tcpSink[新代理/tcpSink]
$ns附加代理$DistributSwitch$tcpSink
$ns connect$tcpAgent$tcpSink
$ns双工链路$DistrictSwitch$cch 100Mb 10ms DropTail
设置tcpAgent[新代理/TCP]
$ns附加代理$DistrictSwitch$tcpAgent
设置tcpSink[新代理/tcpSink]
$ns附加代理$cch$tcpSink
$ns connect$tcpAgent$tcpSink
$ns双工链路$cch$websrv 100Mb 10ms垂尾
设置tcpAgent[新代理/TCP]
$ns附加代理$cch$tcpAgent
设置tcpSink[新代理/tcpSink]
$ns附加代理$websrv$tcpSink
$ns connect$tcpAgent$tcpSink
设置服务器[新Http/server$ns$websrv]
设置缓存[new Http/cache$ns$cch]
设置pgp[新页面池/数学]
设置tmp[新的随机变量/常数]
$tmp set val_u4096
$pgp ranvar大小$tmp
设置tmp[新随机变量/指数]
$tmp组平均值6
$pgp ranvar年龄$tmp
$server set页面生成器$pgp
设置tmp[新随机变量/指数]
$tmp组平均值0.5
$client set interval generator$tmp
$client set页面生成器$pgp
6.0“完成时为$ns”

此代码中存在问题:

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"
    }
}
每次通过循环,都会被覆盖;只有最后一个这样的客户端才能启动。我们需要做的是通过参数将客户机传递给过程

##### 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
    }
}
列表是保存简单事物集合的好方法

##### 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 
}