[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ns] Using now command in TCL



Hello

I am trying to modify Marc Greis's recv procedure for the ping agent in
ping.tcl in order to print the virtual time when the agent received the
ping reply.

I tried to use [$ns now] or [$ns_ now] but it didn't work. Could
somebody tell me how to access the virtual time from within the agent
class?

The ping.tcl file follows. The arrows mark the change.

Thanking you in advance

Nick

--------------------------------

#Create a simulator object
set ns [new Simulator]

#Open a trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure
proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
}

#Create three nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

#Connect the nodes with two links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail

#Define a 'recv' function for the class 'Agent/Ping'
Agent/Ping instproc recv {from rtt} {
 $self instvar node_

#original output code here
<----------------------
# puts "node [$node_ id] received ping answer from \
#              $from with round-trip-time $rtt ms."

#changed output code here
<----------------------
 puts "At [$ns now] sec. node [$node_ id] received ping answer from \
              $from with round-trip-time $rtt ms."
}

#Create two ping agents and attach them to the nodes n0 and n2
set p0 [new Agent/Ping]
$ns attach-agent $n0 $p0

set p1 [new Agent/Ping]
$ns attach-agent $n2 $p1

#Connect the two agents
$ns connect $p0 $p1

#Schedule events
$ns at 0.2 "$p0 send"
$ns at 0.4 "$p1 send"
$ns at 0.6 "$p0 send"
$ns at 0.6 "$p1 send"
$ns at 1.0 "finish"

#Run the simulation
$ns run