In order to use the scheduling functionality of the simulation system, we must create a Simulator object:
new Simulator |
|
|
set ns [new Simulator] set curr_time [$ns now] // Returns the current simulation time |
|
I.e., the time when the OTcl command is run !!!
|
set ns [new Simulator] $ns at Time Action |
Notes:
|
set ns [new Simulator] $ns at 1.0 {puts "Point 1: Now = [$ns now]"} $ns at 8.0 {puts "Point 2: Now = [$ns now]"} $ns at 4.0 {puts "Point 3: Now = [$ns now]"} $ns run // Run simulation ! |
Point 1: Now = 1 Point 3: Now = 4 Point 2: Now = 8 |
Note:
|
set ns [new Simulator] $ns at 1.0 {puts "Point 1: ..."} // Schedule event at time 1.0 $ns at 8.0 {puts "Point 2: ..."} // Schedule event at time 8.0 $ns at 4.0 {puts "Point 3: ..."} // Schedule event at time 4.0 $ns run // Run simulation ! |
When the simulation is run, the scheduled events are "fired" (run) in chronological order (given by their event time !!!
set ns [new Simulator] ... (set up simulation network) $ns run // run simulation |
|
set ns [new Simulator] # ----------------------------------------- # The 'finish' procedure wraps things up # ----------------------------------------- proc finish {} { ... ... (print performance information gathered) ... exit 0 } ... (set up simulation network) # ------------------------------------------- # Schedule the wrapup action at time 100 # ------------------------------------------- $ns at 100.0 "finish" $ns run // run simulation |
proc person1 {x} { global ns puts "Person 1:" puts " Hey, $x, time is [$ns now], it's your turn to say something" $ns at [expr [$ns now] + 0.4] "$x person1" } proc person2 {x} { global ns puts "Person 2:" puts " Hey, $x, time is [$ns now], it's your turn to say something" $ns at [expr [$ns now] + 0.6] "$x person2" } set ns [new Simulator] $ns at 0 "person1 person2" $ns at 4.5 "exit 0" $ns run |
Person 1: Hey, person2, time is 0, it's your turn to say something Person 2: Hey, person1, time is 0.4, it's your turn to say something Person 1: Hey, person2, time is 1, it's your turn to say something Person 2: Hey, person1, time is 1.4, it's your turn to say something Person 1: Hey, person2, time is 2, it's your turn to say something Person 2: Hey, person1, time is 2.4, it's your turn to say something Person 1: Hey, person2, time is 3, it's your turn to say something Person 2: Hey, person1, time is 3.4, it's your turn to say something Person 1: Hey, person2, time is 4, it's your turn to say something Person 2: Hey, person1, time is 4.4, it's your turn to say something |