Driving principle of discrete event in NS2. (schedler, handler, event, timer)

Source: Internet
Author: User

NS2. it is a discrete event-driven simulation mechanism. This document is everywhere, but it has never talked about ideas. This article attempts to find out what the Discrete Event driver is from the perspective of several basic NS2.
The first is the relationship between schedler, handler and event class.
In NS2. events are basic scheduling units, such as sending a packet and receiving a packet. Each event has its own processing tool, which is a handler Class Object Handler _. Handler contains only one function, which describes the processing method of event, that is, handle (event * E ).
Given an event, schedle calls the schedule (handler * H, event * E, double delay) function, which sets the UID _ of the event and the handler used to process the event: e-> handler _ = H, and insert the event into the event queue maintained by scheduler. Generally, the handler that processes the event is the entity that generates the event, so we can often see that the schedule function uses the this pointer when calling the * H function.
When running the simulation in NS2. The schedue: Run () function keeps running to process events in the queue. The events in the queue are dequeue () one by one, and then run schedue :: the dispatch (event * P, double time) function pops up an event from the queue and calls its corresponding handler's handle () function to process it.
In this way, an event is completed from being generated to queuing to being handled.
Next, let's take a look at the role of timerhandler.
Timer (timer) is a key means of NS2. it is used to set a future event. When it is reached, the event will be processed by dispatch. Timer is a derived class of the timerhandler base class. The interaction function between timerhandler and scheduler is sched (double delay). It calls Scheduler: Schedule (handler * H, event * E, double delay) insert an event after the delay time into the queue, and set handler to timerhandler itself (use this ).
After the delay time is reached, schedue will issue the event from the dequeue In the event queue and call its processing function, that is, timerhandler: handle (). In addition to timer State setting, handle () is implemented by the virtual function expire (event * E. It is not difficult to understand the dynamic characteristics of C ++. expire () will be rewritten in various custom timer (that is, the derived class of timerhandler) to implement different processing methods for each timer.
Let's look at a specific example.
Taking the dltimer In the NIST WiMAX module as an example, this timer is used to trigger the generation of a downstream frame:
Class dltimer: Public timerhandler {
Public:
Dltimer (mac802_16 * m): timerhandler () {M _ = m ;}
Void expire (event * E );
PRIVATE:
Mac802_16 * M _;
};
In the Mac entity using the timer, the timer is enabled in the mac802_16bs: Init () function:
Double stime = getframeduration () + random: Uniform (0, getframeduration ());
Dl_timer _-> sched (stime );
This timer is scheduled to trigger an event after stime. So what will scheduler do after that? As shown above, the schedule () function will call the handler of the event to process the event. What is the handler of this event? The code for viewing sched () can be traced to an inline function:
Inline void _ sched (double delay ){
(Void) schedle: instance (). Schedule (this, & event _, delay );
}
This function sets the handler of Event _ to this, that is, the object that calls schedule (), and this object is exactly the dl_timer _! So we know that the timer will call the handle () function of dl_timer _ to do things, and dltimer directly inherits the handle () function of timerhandler, while timerhandler :: the handle () function depends on the virtual function expire (event * E. When dltimer inherits timerhandler, it just overwrites the expire function:
Void dltimer: expire (event * E)
{
M _-> start_dlsubframe ();
}
Now things become clear. After dl_timer _ arrives, schedmer triggers an event that triggers the start_dlsubframe () action of the mac802_16 class. This function is "starting the downlink subframe ". In this way, the whole process of scheduling is completed with a timer.

 

 

From: http://kongjian.baidu.com/wienerlee/blog/item/c4c65726f85569118b82a144.html/cmtid/dcda1ceaa4f063dcd539c9da

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.