【问题标题】:TCL Is there any way to?TCL有什么办法吗?
【发布时间】:2013-10-22 17:21:47
【问题描述】:

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

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 中没有传输。我不知道为什么。请帮忙。

【问题讨论】:

    标签: linux ftp tcl adhoc proc


    【解决方案1】:

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

    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} 来设置默认值,并且您可以添加更多错误检查。或者不。这取决于您。我不知道什么是合理的默认值;我想代理可能适合这种事情。)

    【讨论】:

    • 据我快速浏览文档可以看出,变量的名称几乎是任意的,普通的Tcl变量; ns2 引擎只关心其中的值。这意味着您可以使用各种技巧来使事情变得更好!
    • 非常感谢您的帮助!
    猜你喜欢
    • 2010-12-15
    • 1970-01-01
    • 2023-02-06
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2016-01-28
    • 1970-01-01
    相关资源
    最近更新 更多