NS simulation: scheduling events (examples inside)

Source: Internet
Author: User

NS simulation: scheduling events

 

  • Simulation time

     

    • A similation system (such as NS) must have a built-inSimulation Clock-It represents the "Clock" in the real world.

       

    • You canExamine(Read) the value of the Simulation Clock by usingNowCommand inSimulatorClass

       

    • Example::

       

        set  ns  [new Simulator]  $ns  now   // Returns the current simulation time   

       

     

     

  • Events

     

    • AnEventIs an action made by a simulation entity

       

    • An event (action) in a simulation is representedOTCL command!!!

       

    • Each event happens at a certainTimeAnd therefore, each event has an associatedEvent TimeWhich is the time when the event will happen

       

    • In simulation,EventsAreScheduled(To happen at some ure time)

       

    • To schedule an event, useAtCommand inSimulatorClass

       

    • Example::

       

        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 !

       

    • You will see the following output:

       

        Point 1: Now = 1  Point 3: Now = 4        Point 2: Now = 8

      Notice thatPoint 3Happens beforePoint 2

       

    • This is what is happening:

       

        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 !

      So when the simulation is run, the events are"Fired"InChronological order

     

     

  • Running the NS simulation

     

    • TheRunCommand inSimulationClass is used to run the network simuation.

       

    • Example:

       

          set  ns  [new Simulator]    ... (set up simulation network)    $ns run  // run simulation       

       

     

     

  • Stopping an NS simulation

     

    • There is no"Stop"Command available.

       

    • To stop a running simulation, you mustScheduleATerminationCommandBeforeYou start running the simulation.

       

    • TheTerminationCommand is usuallyTCL procedure(To wrap up things)

       

    • Example:

       

          set  ns  [new Simulator]    #Define the ‘finish‘ procedure    proc finish {} {       exit 0    }    ... (set up simulation network)        #Set simulation end time    $ns at 100.0 "finish"    $ns run           // run simulation       

       

     

  • A simple simulation ....

     

    • To give you a taste of simulation, here is a NS script that simulates 2 person "talking" to each other:

       

        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

       

       

    • Example program:(Demo abve code)

       

      • Prog file: click here

       

    • Output when you run this simulation:

       

      Person 1: Hey, person2, time is 0, it‘s your turn to say somethingPerson 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 somethingPerson 2: Hey, person1, time is 1.4, it‘s your turn to say somethingPerson 1: Hey, person2, time is 2, it‘s your turn to say somethingPerson 2: Hey, person1, time is 2.4, it‘s your turn to say somethingPerson 1: Hey, person2, time is 3, it‘s your turn to say somethingPerson 2: Hey, person1, time is 3.4, it‘s your turn to say somethingPerson 1: Hey, person2, time is 4, it‘s your turn to say somethingPerson 2: Hey, person1, time is 4.4, it‘s your turn to say something     

       

     

     

  • Example NS simulation script

     

    • We will construct an NS simulation script that simulate the following network:

       

       

    • The following figure is a break down of the NS components that make up the above network:

       

       

    • Here is the NS (OTCL) script that creates the above simulation:

       

        #Make a NS simulator     set ns [new Simulator]  # Define a ‘finish‘ procedure  proc finish {} {     exit 0  }  # Create the nodes:  set n0 [$ns node]  set n1 [$ns node]  set n2 [$ns node]  set n3 [$ns node]  set n4 [$ns node]  set n5 [$ns node]  # Create the links:  $ns duplex-link $n0 $n2   2Mb  10ms DropTail  $ns duplex-link $n1 $n2   2Mb  10ms DropTail  $ns duplex-link $n2 $n3 0.3Mb 200ms DropTail  $ns duplex-link $n3 $n4 0.5Mb  40ms DropTail  $ns duplex-link $n3 $n5 0.5Mb  30ms DropTail  # Add a TCP sending module to node n0  set tcp1 [new Agent/TCP/Reno]  $ns attach-agent $n0 $tcp1  # Add a TCP receiving module to node n4  set sink1 [new Agent/TCPSink]  $ns attach-agent $n4 $sink1  # Direct traffic from "tcp1" to "sink1"  $ns connect $tcp1 $sink1  # Setup a FTP traffic generator on "tcp1"  set ftp1 [new Application/FTP]  $ftp1 attach-agent $tcp1  $ftp1 set type_ FTP               (no necessary)  # Schedule start/stop times  $ns at 0.1   "$ftp1 start"  $ns at 100.0 "$ftp1 stop"  # Set simulation end time  $ns at 125.0 "finish"    (Will invoke "exit 0")     # Run simulation !!!!  $ns run

       

       

    • Example program:(Demo abve code)

       

      • Prog file: click here

     

     

     

  • Problems with the above Simulation

     

    • It simulates alright...

       

    • ButWithout producing any dataThat we can examine !!!

       

    • What we still need to learn is howAdd commandsIn the simulation programOutput State VariablesThat we are interested in !!!

       

Http://www.mathcs.emory.edu /~ Cheung/courses/558-old/syllabus/90-ns/2-ns-prog/events.html

NS simulation: scheduling events (examples inside)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.