Question

Is there any way to do some procedure and don't do like this:

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" 

over and over? I do something like this:

    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

But it doesn't work... In NAM there is no transmission. I don't know why. Please help.

Was it helpful?

Solution

Your instinct that there ought to be a better way is correct. What you need are a few tweaks:

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

However, it's also reasonable to consider whether the simulation and node mapping are true globals and also what sort of syntax your procedure ought to take for clarity:

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

It's a total cheaty-hack doing it like this, and you can see that the implementation code is very similar, but this way of doing it is going to look much clearer when you come back to the code. (You can also set defaults by doing array set a {the-default mappings} first thing in the procedure, and you can add more error checking. Or not. It's up to you. I've no idea what sensible default values might be; I suppose the types of agents might be good for that sort of thing.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top