Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Dynamic of Unique ID      Ns2Ultimate.com             by     Teerawat Issariyakul        January 2011
RecapNS2 is a discrete-event simulator, where actions are associated with eventsrather than time. An event in a discrete-e...
Recap• Attributes of Event     handler    handle(){        handle(){     handler                  <actions>        <action...
Recap• Attributes of Event     handler    handle(){        handle(){     handler                  <actions>        <action...
Recap: Function schedule(h,e,delay)Put an event *e on the timeline at “delay”second infuture, and associated the event wit...
Recap: Function schedule(h,e,delay)//~ns/common/scheduler.ccvoid Scheduler::schedule(Handler* h, Event* e, double delay){ ...
So, NS2 has two type of unique  IDs: Global Unique ID and            Event
T T    wo ypes of UIDGlobal UID  The variable uid_ of the Scheduler  One and only one for a simulationEvent UID or Local U...
Global UID and Event UIDSimulation: In function Schedule(…)                                   Event                       ...
Global UID and Event UIDSimulation: In function Schedule(…)                                        //~ns/common/scheduler....
Event UIDSimulation: In function dispatch(…)         Event     x     uid_              //~ns/common/scheduler.cc          ...
84              Dynamic of Event UID       4 Implementation of Discrete-Event Simulation in NS2schedule(...) and dispatch(...
T key questions wo1. Why is the “event UID” changes its value  like this?2. What if the dynamic is different?             ...
Dynamic of Event UIDIt is a UID protection mechanism.But before going further, let’s askanother basic question:           ...
EVENTS        NS2 has several type of events        Let’s focus on packet reception events                                ...
Packet Reception Event                 Packet                  uid_Time where the  packet is      time_   received        ...
Packet Event ReuseOne packet could be passed amongseveral Nodes          Event/          Packet         2 uid_           t...
Packet Event ReuseNS2 does not create a new event every time it forwards apacket.It     Changes the event UID, and     pas...
An example of packet forwarding                                                  time                0                    ...
An example of packet forwarding                                               time          1.2               5.12        ...
84     4 Implementation of Discrete-Event Simulation in NS2     An example of packetThe sign toggling mechanism     schedu...
84     4 Implementation of Discrete-Event Simulation in NS2 An example of packetThe sign toggling mechanism schedule(...) ...
An example of packet forwarding                 We’ve already decided at “t=5” that                 the packet will be rec...
The Sign of the Event UID Positive (+):     The event is scheduled but not yet dispatched     Do not reschedule here. Othe...
Protection MechanismFrom within the function schedule(…),NS2 checks for the error  if (e->uid_ > 0) {     printf("Schedule...
For more         information          about NS2                   Please see                   this book                 f...
Upcoming SlideShare
Loading in …5
×

Dynamic UID

29,948 views

Published on

Published in: Technology, Sports
  • Be the first to comment

Dynamic UID

  1. 1. Dynamic of Unique ID Ns2Ultimate.com by Teerawat Issariyakul January 2011
  2. 2. RecapNS2 is a discrete-event simulator, where actions are associated with eventsrather than time. An event in a discrete-event simulator consists of executiontime, a set of actions, and a reference to the next event (Fig. 4.1). These eventsconnect to each other and form a chain of events on the simulation timelineSchedule = Put an event on the simulation(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-drivensimulator, time between a pair of events does not need to be constant. Whentime linethe simulation starts, events in the chain are executed from left to right (i.e.,chronologically).1 In the next section, we will discuss the simulation concept ofNS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes EventDispatch = Execute the eventand Handler, class Scheduler, and class Simulator, respectively. Finally, wesummarize this chapter in Section 4.6. insert Event5 create event event time = 3.7 Action5 Event1 Event2 Event3 Event4 time = 0.9 time = 2.2 time = 5 time = 6.8 Action1 Action2 Action3 Action4 Time (second) 1 2 3 4 5 6 7Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-tains execution time and a reference to the next event. In this figure, Event1 createsand inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second). www.ns2ultimate.com
  3. 3. Recap• Attributes of Event handler handle(){ handle(){ handler <actions> <actions> } } handler_ next_ handler_ next_ event uid_ time_ uid_ time_ event How do we assign a unique ID (i.e., uid_) to the event? www.ns2ultimate.com
  4. 4. Recap• Attributes of Event handler handle(){ handle(){ handler <actions> <actions> } } handler_ next_ handler_ next_ event uid_ time_ uid_ time_ event Using the Scheduler www.ns2ultimate.com
  5. 5. Recap: Function schedule(h,e,delay)Put an event *e on the timeline at “delay”second infuture, and associated the event with a handler “*h”. Scheduler s& = Scheduler::instance(); s.schedule(h,e,delay); Call here h e associated Handler Event Delay x current time www.ns2ultimate.com
  6. 6. Recap: Function schedule(h,e,delay)//~ns/common/scheduler.ccvoid Scheduler::schedule(Handler* h, Event* e, double delay){ < CHECKING FOR ERRORS > e->uid_ = uid_++; e->handler_ = h; 1. Increment the local variable uid_ double t = clock_ + delay; 2. Assigned the incremented value e->time_ = t; as the unique ID of the event insert(e);} Scheduler Event assign to uid_ uid_ www.ns2ultimate.com
  7. 7. So, NS2 has two type of unique IDs: Global Unique ID and Event
  8. 8. T T wo ypes of UIDGlobal UID The variable uid_ of the Scheduler One and only one for a simulationEvent UID or Local UID The variable uid_ of an event Every event has its own uid_ www.ns2ultimate.com
  9. 9. Global UID and Event UIDSimulation: In function Schedule(…) Event Event UID: assign to uid_ - Unique to each Scheduler event uid_ assign to Event uid_ - Many events in assign to a simulation ➠ Event Many local UID Global UID: uid_ in a simulation Unique to the simulation www.ns2ultimate.com
  10. 10. Global UID and Event UIDSimulation: In function Schedule(…) //~ns/common/scheduler.cc Event uid_ void Scheduler::schedule assign to (Handler* h, Event* e, Scheduler double delay){ uid_ assign to Event Event UID is always a uid_ positive new number ... assign to e->uid_ = uid_++; e->handler_ = h; Event double t = clock_ + uid_ delay; e->time_ = t; insert(e); } www.ns2ultimate.com
  11. 11. Event UIDSimulation: In function dispatch(…) Event x uid_ //~ns/common/scheduler.cc void Scheduler::dispatch(Event* p, double t){ Reversing clock_ = t; the sign p->uid_ = -p->uid_; p->handler_->handle(p); Event } -x uid_ www.ns2ultimate.com
  12. 12. 84 Dynamic of Event UID 4 Implementation of Discrete-Event Simulation in NS2schedule(...) and dispatch(...) functions. The sign toggling mechanismof unique ID ensures that events will be scheduled and dispatched properly. Ifa scheduled UIDis nota certain event changes its value like A event of dispatched, or is dispatched twice, its unique ID willbe positive, and an attempt to schedule this undispatched event will cause an this (a positiveerror (Lines 5 and 6 in Program 4.7). new value) –2 (reversingFig. 4.2. Dynamics of Event unique ID (uid) : Take a positive value from the sign)Scheduler::uid when being scheduled, and invert the sign when being dispatched.Increment upon schedule and inversion of sign upon dispatch. www.ns2ultimate.com
  13. 13. T key questions wo1. Why is the “event UID” changes its value like this?2. What if the dynamic is different? www.ns2ultimate.com
  14. 14. Dynamic of Event UIDIt is a UID protection mechanism.But before going further, let’s askanother basic question: WHAT IS AN EVENT? www.ns2ultimate.com
  15. 15. EVENTS NS2 has several type of events Let’s focus on packet reception events Event ... AtEvent Packet ... Packet reception eventsSource: http://www-rp.lip6.fr/ns-doc/ns226-doc/html/hierarchy.htm www.ns2ultimate.com
  16. 16. Packet Reception Event Packet uid_Time where the packet is time_ received handler_ a pointer to node 2 Node 1 Node 2 www.ns2ultimate.com
  17. 17. Packet Event ReuseOne packet could be passed amongseveral Nodes Event/ Packet 2 uid_ time_ handler_Node 1 Node 2 Node 3 www.ns2ultimate.com
  18. 18. Packet Event ReuseNS2 does not create a new event every time it forwards apacket.It Changes the event UID, and passes the same packet to the next object Event/ Packet 6 uid_ time_ handler_ Node 1 Node 2 Node 3 www.ns2ultimate.com
  19. 19. An example of packet forwarding time 0 5.12 Instance 1: Event/ Event/ A packet with UID Packet Packet “2” is scheduled to 2 uid_ 6 uid_ arrived at Node 2 at 1.2 time_ 5.1 time_ “0” s. handler_ handler_ Node 1 Node 2 Node 3 www.ns2ultimate.com
  20. 20. An example of packet forwarding time 1.2 5.12 Instance 2: Event/ Packet A packet with UID “6” is scheduled to 6 uid_ arrived at Node 3 at 5.1 time_ “5.1” s. handler_ Node 1 Node 2 Node 3 www.ns2ultimate.com
  21. 21. 84 4 Implementation of Discrete-Event Simulation in NS2 An example of packetThe sign toggling mechanism schedule(...) and dispatch(...) functions. forwarding of unique ID ensures that events will be scheduled and dispatched properly. If a scheduled event is not dispatched, or is dispatched twice, its unique ID will be positive, and an attempt to schedule this undispatched event will cause an error (Lines 5 and 6 in Program 4.7). –2 t=0: Fig. 4.2. Dynamics of Event unique ID (uid) : Take a positive value from NS2 schedules being scheduled, and invert the sign when being dispatched. Scheduler::uid when t=1.2:packet reception at and inversion of sign upon dispatch. Increment upon schedule packet is received node 2 at 1.2 s at node 2 4.3.6 Scheduling-Dispatching Mechanism www.ns2ultimate.com
  22. 22. 84 4 Implementation of Discrete-Event Simulation in NS2 An example of packetThe sign toggling mechanism schedule(...) and dispatch(...) functions. forwarding of unique ID ensures that events will be scheduled and dispatched properly. If a scheduled event is not dispatched, or already decided at “t=5” that We’ve is dispatched twice, its unique ID will be positive, and an attempt to schedule this undispatched event will cause an the packet will be received at “t=5.1”. error (Lines 5 and 6 in Program 4.7). So nothing can happen here. –2 t=5: NS2 schedules t=5.1: Fig. 4.2. Dynamics of Event unique ID (uid) : Take a positive value frompacket reception being scheduled, and invert the signpacket is received Scheduler::uid when atand inversion of sign upon dispatch. being dispatched. Increment upon schedule when node 3 at 5.1 s at node 3 4.3.6 Scheduling-Dispatching Mechanism www.ns2ultimate.com
  23. 23. An example of packet forwarding We’ve already decided at “t=5” that the packet will be received at “t=5.1”. So nothing can happen here. HOW DO WE KNOW THAT NOTHING WILL HAPPEN AT THESE PERIODS? WE USE“THE SIGN” OF EVENT UID www.ns2ultimate.com
  24. 24. The Sign of the Event UID Positive (+): The event is scheduled but not yet dispatched Do not reschedule here. Otherwise, you will see the following error message on the screen Scheduler: Event UID not valid! Negative (-): The event has been dispatched. It is now ready to be scheduled again. www.ns2ultimate.com
  25. 25. Protection MechanismFrom within the function schedule(…),NS2 checks for the error if (e->uid_ > 0) { printf("Scheduler: Event UID not valid!nn"); abort(); }which will show an error message if theevent UID is positive. www.ns2ultimate.com
  26. 26. For more information about NS2 Please see this book from SpringerT. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Second Edition,Springer 2011, or visit www.ns2ultimate.com

×