|
The NS2 system is:
|
|
set Variable [ new Simulator ]; |
|
|
set ns [new Simulator] ; # Create a simulator object set curr_time [$ns now] ; # Returns the current simulation time |
|
|
|
|
|
set ns [new Simulator] $ns at Time Action |
set ns [new Simulator]; # Create a simulator object puts "Line 1"; # Schedule 3 "events" $ns at 1.0 {puts "Event 1: Now = [$ns now]"}; # puts = put string $ns at 8.0 {puts "Event 2: Now = [$ns now]"}; $ns at 4.0 {puts "Event 3: Now = [$ns now]"}; puts "Line 2"; puts "Notice line 1 & 2 preceeds all events !!!" $ns run ; # Run simulation puts "Notice that event 3 preceeds event 2 !!!" |
Line 1 Line 2 Notice line 1 & 2 preceeds all events !!! Event 1: Now = 1 Event 3: Now = 4 Event 2: Now = 8 Notice that event 3 preceeds event 2 !!! |
Notice that:
|
cd /home/cs455000/demo/NS2/intro ns intro1.ns |
set ns [new Simulator]; # Create a netw simulator object # ----------------------------------------- # Define 2 functions # ----------------------------------------- proc event {} { global ns; puts "[$ns now]: exec: event()"; } proc finish {} { global ns; puts "[$ns now]: exec: finish()"; } # ------------------------------------------- # Schedule function execution as events # ------------------------------------------- $ns at 1.0 "event" ; $ns at 5.0 "event" ; $ns at 20.0 "event" ; $ns at 10.0 "finish" ; $ns run ; |
cd /home/cs455000/demo/NS2/intro ns intro2.ns |
set ns [new Simulator] ... (set up simulation network) $ns run // run simulation |
|
|
|
(Once you run the simulation, it's too late to schedule the exit command)
set ns [new Simulator] # ----------------------------------------- # The 'finish' procedure wraps things up # ----------------------------------------- proc finish {} { ... ... (print performance information gathered) ... exit 0 } ... # ------------------------------------------- # Schedule the wrapup action at time 100 # ------------------------------------------- $ns at 100.0 "finish" $ns run // run simulation |
set ns [new Simulator]; # Create a netw simulator object # --------------------------------------------- # This function schedule itself 3.0 sec later # --------------------------------------------- proc event {} { global ns; puts "[$ns now]: exec: event()"; $ns at [expr [$ns now] + 3.0] "event" ; # Self-scheduling !!! } # ------------------------------------------- # Schedule function execution as events # ------------------------------------------- $ns at 1.0 "event" ; $ns at 30.0 "exit 0" ; $ns run ; |
Result:
1: exec: event() 4: exec: event() 7: exec: event() 10: exec: event() 13: exec: event() 16: exec: event() 19: exec: event() 22: exec: event() 25: exec: event() 28: exec: event() |
cd /home/cs455000/demo/NS2/intro ns intro4.ns |