The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ... The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

nmlab.korea.ac.kr
from nmlab.korea.ac.kr More from this publisher
12.07.2015 Views

Chapter 11TimersTimers may be implemented in C++ or OTcl. In C++, timers are based on an abstract base class defined in ~ns/timer-handler.h.They are most often used in agents, but the framework is general enough to be used by other objects. The discussion below isoriented towards the use of timers in agents.The procedures and functions described in this chapter can be found in ~ns/tcl/ex/timer.tcl, and ~ns/timer-handler.{cc, h}.In OTcl, a simple timer class is defined in ~ns/tcl/ex/timer.tcl. Subclasses can be derived to provide a simple mechanism forscheduling events at the OTcl level.11.1 C++ abstract base class TimerHandlerThe abstract base class TimerHandler contains the following public member functions:void sched(double delay) schedule a timer to expire delay seconds in the futurevoid resched(double delay) reschedule a timer (similar to sched(), but timer may be pending)void cancel() cancel a pending timerint status() returns timer status (either TIMER_IDLE, TIMER_PENDING, orTIMER_HANDLING)The abstract base class TimerHandler contains the following protected members:virtual void expire(Event* e) =0 this method must be filled in by the timer clientvirtual void handle(Event* e) consumes an event; invokes expire() and sets status_ of the timer appropriatelyint status_ keeps track of the current timer statusEvent event_ event to be consumed upon timer expirationThe pure virtual function expire() must be defined by the timer classes deriving from this abstract base class.Finally, two private inline functions are defined:inline void _sched(double delay) {111

(void)Scheduler::instance().schedule(this, &event_, delay);}inline void _cancel() {(void)Scheduler::instance().cancel(&event_);}From this code we can see that timers make use of methods of the Scheduler class.11.1.1 Definition of a new timerTo define a new timer, subclass this function and define handle() if needed (handle() is not always required):class MyTimer : public TimerHandler {public:MyTimer(MyAgentClass *a) : TimerHandler() { a_ = a; }virtual double expire(Event *e);protected:MyAgentClass *a_;};Then define expire:doubleMyTimer::expire(Event *e){// do the work// return TIMER_HANDLED; // => do not reschedule timer// return delay; // => reschedule timer after delay}Note that expire() can return either the flag TIMER_HANDLED or a delay value, depending on the requirements for thistimer.Often MyTimer will be a friend of MyAgentClass, or expire() will only call a public function of MyAgentClass.Timers are not directly accessible from the OTcl level, although users are free to establish method bindings if they so desire.11.1.2 Example: Tcp retransmission timerTCP is an example of an agent which requires timers. There are three timers defined in the basic Tahoe TCP agent defined intcp.cc:rtx_timer_; /* Retransmission timer */delsnd_timer_; /* Delays sending of packets by a small random amount of time, *//* to avoid phase effects */burstsnd_timer_; /* Helps TCP to stagger the transmission of a large window *//* into several smaller bursts */112

(void)Scheduler::i<strong>ns</strong>tance().schedule(this, &event_, delay);}inline void _cancel() {(void)Scheduler::i<strong>ns</strong>tance().cancel(&event_);}From this code we can see th<strong>at</strong> timers make use of methods of the Scheduler class.11.1.1 Definition of a new timerTo define a new timer, subclass this function <strong>and</strong> define h<strong>and</strong>le() if needed (h<strong>and</strong>le() is not always required):class MyTimer : public TimerH<strong>and</strong>ler {public:MyTimer(MyAgentClass *a) : TimerH<strong>and</strong>ler() { a_ = a; }virtual double expire(Event *e);protected:MyAgentClass *a_;};<strong>The</strong>n define expire:doubleMyTimer::expire(Event *e){// do the work// return TIMER_HANDLED; // => do not reschedule timer// return delay; // => reschedule timer after delay}Note th<strong>at</strong> expire() can return either the flag TIMER_HANDLED or a delay value, depending on the requirements for thistimer.Often MyTimer will be a friend of MyAgentClass, or expire() will only call a public function of MyAgentClass.Timers are not directly accessible from the OTcl level, although users are free to establish method bindings if they so desire.11.1.2 Example: Tcp retra<strong>ns</strong>mission timerTCP is an example of an agent which requires timers. <strong>The</strong>re are three timers defined in the basic Tahoe TCP agent defined intcp.cc:rtx_timer_; /* Retra<strong>ns</strong>mission timer */delsnd_timer_; /* Delays sending of packets by a small r<strong>and</strong>om amount of time, *//* to avoid phase effects */burstsnd_timer_; /* Helps TCP to stagger the tra<strong>ns</strong>mission of a large window *//* into several smaller bursts */112

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!