0

有没有办法做一些程序,不要这样做:

set tcp [new Agent/TCP/Newreno]
set sink [new Agent/TCPSink]
$ns attach-agent $n(0) $tcp
$ns attach-agent $n(1) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.0 "$ftp start" 
$ns at 130.0 "$ftp stop" 
##################################################################
set tcp [new Agent/TCP/Newreno]
set sink [new Agent/TCPSink]
$ns attach-agent $n(6) $tcp
$ns attach-agent $n(15) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start" 
$ns at 110.0 "$ftp stop" 
##################################################################
set tcp [new Agent/TCP/Newreno]
set sink [new Agent/TCPSink]
$ns attach-agent $n(8) $tcp
$ns attach-agent $n(0) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 30.0 "$ftp start" 
$ns at 100.0 "$ftp stop" 

一遍又一遍?我做这样的事情:

    proc wymiana {ns n_varname w1 w2 t1 t2} {
    upvar 1 $n_varname n

    set tcp [new Agent/TCP/Newreno]
    set sink [new Agent/TCPSink]
    $ns attach-agent $n($w1) $tcp
    $ns attach-agent $n($w2) $sink
    $ns connect $tcp $sink
    set ftp [new Application/FTP]
    $ftp attach-agent $tcp
    $ns at t1 "$ftp start" 
    $ns at t2 "$ftp stop" 
}

wymiana $ns  n  1 2 1.0 100.0

但它不起作用......在NAM中没有传输。我不知道为什么。请帮忙。

4

1 回答 1

3

你认为应该有更好方法的直觉是正确的。您需要做一些调整:

proc wymiana {ns n_varname w1 w2 t1 t2} {
    upvar 1 $n_varname node

    set tcp [new Agent/TCP/Newreno]
    set sink [new Agent/TCPSink]
    ### Varname is different just to make it clearer
    $ns attach-agent $node($w1) $tcp
    $ns attach-agent $node($w2) $sink
    $ns connect $tcp $sink
    set ftp [new Application/FTP]
    $ftp attach-agent $tcp
    ### Changes on two lines below
    $ns at $t1 "$ftp start" 
    $ns at $t2 "$ftp stop" 
}

# Create the setup from your question
wymiana $ns  n  1  2   1.0 130.0
wymiana $ns  n  6 15  10.0 110.0
wymiana $ns  n  8  0  30.0 100.0

但是,考虑模拟和节点映射是否是真正的全局变量以及为了清晰起见您的过程应该采用哪种语法也是合理的:

proc SetupFTP args {
    global ns n
    array set a $args

    set tcp [new Agent/TCP/Newreno]
    set sink [new Agent/TCPSink]
    $ns attach-agent $n($a(-from)) $tcp
    $ns attach-agent $n($a(-to)) $sink
    $ns connect $tcp $sink
    set ftp [new Application/FTP]
    $ftp attach-agent $tcp
    $ns at $a(-start) "$ftp start" 
    $ns at $a(-stop) "$ftp stop" 
}

SetupFTP -from 1 -to  2 -start  1.0 -stop 130.0
SetupFTP -from 6 -to 15 -start 10.0 -stop 110.0
SetupFTP -from 8 -to  0 -start 30.0 -stop 100.0

这样做完全是骗人的,你可以看到实现代码非常相似,但是当你回到代码时,这种方法会看起来更清晰。(您也可以通过array set a {the-default mappings}在过程中做第一件事来设置默认值,并且可以添加更多错误检查。或者不。这取决于您。我不知道什么是合理的默认值;我想代理的类型可能是适合那种事情。)

于 2013-10-22T19:45:24.610 回答