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 ...
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
- Page 61 and 62: Module NameRtModule/BaseRtModule/Mc
- Page 63 and 64: $node neighborsThis returns the lis
- Page 65 and 66: Linkhead_enqT_queue_ deqT_ link_ tt
- Page 67 and 68: 6.2 ConnectorsConnectors, unlink cl
- Page 69 and 70: $ns_ link-lossmodel This function
- Page 71 and 72: Chapter 7Queue Management and Packe
- Page 73 and 74: }}}void Queue::resume(){Packet* p =
- Page 75 and 76: 7.3 Different types of Queue object
- Page 77 and 78: maxidle_ is the maximum amount of t
- Page 79 and 80: dst_ The destination address of pac
- Page 81 and 82: 7.5.2 ConfigurationRunning a JoBS s
- Page 83 and 84: mean_pkt_size_ Used to set the expe
- Page 85 and 86: Demarker objects$q trace-file This
- Page 87 and 88: The recv() method overrides the bas
- Page 89 and 90: 9.2 ImplementationThe procedures an
- Page 91 and 92: Average sending rateTSW window leng
- Page 93 and 94: The following command adds an entry
- Page 95 and 96: $qCE configQ 0 1 10 20 0.10Note tha
- Page 97 and 98: Chapter 10AgentsAgents represent en
- Page 99 and 100: CtrMcast/EncapCtrMcast/DecapMessage
- Page 101 and 102: }bind("windowOption_", &wnd_option_
- Page 103 and 104: hdr_flags* nf = (hdr_flags*)npkt->a
- Page 105 and 106: public:ECHO_Timer(ECHO_Agent *a) :
- Page 107 and 108: 10.6.4 Using the agent through OTcl
- Page 109 and 110: State Variables are:dupacks_ Number
- Page 111: $agent attach-tbf Attaches a token
- Page 115 and 116: * outstanding, cancel it.*/void Tcp
- Page 117 and 118: Chapter 12Packet Headers and Format
- Page 119 and 120: method is now obsolete; its usage i
- Page 121 and 122: Packethdrsize_next_bits()points to
- Page 123 and 124: the system’s memory allocator. In
- Page 125 and 126: }incr hdrlen_ $incrreturn $baseFrom
- Page 127 and 128: Chapter 13Error ModelThis chapter d
- Page 129 and 130: SimpleLink::errormodule argsSimulat
- Page 131 and 132: This is a simple example of how to
- Page 133 and 134: Higher LayersNode 1Node 2. . .Node
- Page 135 and 136: };void recv(Packet* p, Handler*);vi
- Page 137 and 138: and the frame type. It then passes
- Page 139 and 140: $ll_ set delay_ $delay$ll_ set band
- Page 141 and 142: 14.10 Commands at a glanceThe follo
- Page 143 and 144: 15.2.1 Default Hierarchical Setting
- Page 145 and 146: Chapter 16Mobile Networking in nsTh
- Page 147 and 148: entry_addrdemuxIP addressdefaulttar
- Page 149 and 150: 16.1.2 Creating Node movementsThe m
- Page 151 and 152: set netif $netif_($t)set mac $mac_(
- Page 153 and 154: the hardware address of a packet’
- Page 155 and 156: destined to itself to the port dmux
- Page 157 and 158: switch(ch->ptype()) {case PT_MAC:br
- Page 159 and 160: 16.1.7 Revised format for wireless
- Page 161 and 162: Packet info at "Application level"
Chapter 11TimersTimers may be implemented in C++ or OTcl. In C++, timers are based on an abstract base class defined in ~<strong>ns</strong>/timer-h<strong>and</strong>ler.h.<strong>The</strong>y are most often used in agents, but the framework is general enough to be used by other objects. <strong>The</strong> discussion below isoriented towards the use of timers in agents.<strong>The</strong> procedures <strong>and</strong> functio<strong>ns</strong> described in this chapter can be found in ~<strong>ns</strong>/tcl/ex/timer.tcl, <strong>and</strong> ~<strong>ns</strong>/timer-h<strong>and</strong>ler.{cc, h}.In OTcl, a simple timer class is defined in ~<strong>ns</strong>/tcl/ex/timer.tcl. Subclasses can be derived to provide a simple mechanism forscheduling events <strong>at</strong> the OTcl level.11.1 C++ abstract base class TimerH<strong>and</strong>ler<strong>The</strong> abstract base class TimerH<strong>and</strong>ler contai<strong>ns</strong> the following public member functio<strong>ns</strong>: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 st<strong>at</strong>us() retur<strong>ns</strong> timer st<strong>at</strong>us (either TIMER_IDLE, TIMER_PENDING, orTIMER_HANDLING)<strong>The</strong> abstract base class TimerH<strong>and</strong>ler contai<strong>ns</strong> the following protected members:virtual void expire(Event* e) =0 this method must be filled in by the timer clientvirtual void h<strong>and</strong>le(Event* e) co<strong>ns</strong>umes an event; invokes expire() <strong>and</strong> sets st<strong>at</strong>us_ of the timer appropri<strong>at</strong>elyint st<strong>at</strong>us_ keeps track of the current timer st<strong>at</strong>usEvent event_ event to be co<strong>ns</strong>umed upon timer expir<strong>at</strong>ion<strong>The</strong> pure virtual function expire() must be defined by the timer classes deriving from this abstract base class.Finally, two priv<strong>at</strong>e inline functio<strong>ns</strong> are defined:inline void _sched(double delay) {111