ACE-based timer template class

Source: Internet
Author: User

1 ACETimerClockGenerator. h 2 ClockGeneratorIF. h 3 defines a struct in the class and a function in the struct. 4. Define a function in the struct. How can this be done? 5 6 TimerHandler. h 7 uses the template method to construct the Timer class. It helps the bottom layer to call the upper layer. During construction, the maximum number of timers and template classes (that is, parent) in a class are initialized ). 8 TimerHandler (T * parent, int numTimers): timers (numTimers,-1) 9 {// initialization vector, assigned to a private member variable. 10 this-> parent = parent; 11 this-> numTimers = numTimers; 12} 13 14 15 use STL vector: 16 std: vector <int> timers; 17 18 ACE 19 1. startTimer (int timerType, const ACE_Time_Value & delay) 20 ACE_Reactor: instance ()-> schedule_timer () 21 2. stopTimer (int timerType) 22 ACE_Reactor: instance ()-> cancel_timer (timers [timerType]); 23 3. showTimers (bool showAll) 24 ACE_Timer_Queue * reactor_timerQ = ACE_Reactor :: Instance ()-> timer_queue (); 25 ACE_Timer_Queue_Iterator & iter = reactor_timerQ-> iter (); 26 27 28 29 30 31 # ifndef _ TimerHandler_h 32 # define _ TimerHandler_h 33 34 # include <vector> 35 # include "ace/Timer_Queue.h" 36 # include "ace/Date_Time.h" 37 # include "ace/Event_Handler.h" 38 # include "ace/Reactor. h "39 40 # include" TraceUtils. h "41 42 template <class T> 43 class TimerHandler: public ACE_Event _ Handler 44 {45 std: vector <int> timers; 46 T * parent; 47 int numTimers; 48 public: 49 TimerHandler (T * parent, int numTimers): timers (numTimers, -1) 50 {51 this-> parent = parent; 52 this-> numTimers = numTimers; 53} 54 55 ~ TimerHandler () 56 {57 for (unsigned int I = 0; I <timers. size (); I ++) 58 {59 if (timers [I]! =-1) 60 ACE_Reactor: instance ()-> cancel_timer (timers [I]); 61} 62} 63 64 int handle_timeout (const ACE_Time_Value & current_time, 65 const void * arg) 66 {67 long int timerType = (long int) arg; 68 timers [timerType] =-1; 69 parent-> handleTimeout (timerType ); 70 return 0; 71} 72 73 int startTimer (int timerType, const ACE_Time_Value & delay) 74 {75 if (timerType> numTimers-1) // No such timer type 76 return-2; 77 if (timerType <1) 78 return-1; 79 if (timers [timerType]! =-1) // Timer already running 80 return-3; 81 timers [timerType] = 82 ACE_Reactor: instance ()-> schedule_timer (this, 83 (const void *) timerType, 84 delay); 85 return timers [timerType]; 86} 87 88 int stopTimer (int timerType) 89 {90 if (timerType> numTimers-1 | // No such timer type 91 timerType <1 | 92 timers [timerType] =-1) // Timer not already running 93 return-1; 94 ACE_Reactor: instance ()-> Cancel_timer (timers [timerType]); 95 timers [timerType] =-1; 96 return 0; 97} 98 99 bool timerStarted (int timerType) 100 {101 return (timers [timerType]! =-1); 102} 103 void showTimers (bool showAll) 104 {105 ACE_Timer_Queue * reactor_timerQ = ACE_Reactor: instance ()-> timer_queue (); 107 ACE_Timer_Queue_Iterator & iter = reactor_timerQ-> iter (); 108 109 if (reactor_timerQ-> is_empty () 110 {111 TRACE_DEBUG ("No Timers in Queue \ n "); 112} 113 114 int total_timers = 0, hndlr_timers = 0; 115 116 TRACE_DEBUG ("Timers in queue: \ n"); 117 (;! Iter. isdone (); iter. next () 118 {119 ACE_Timer_Node * tn = iter. item (); 120 121 total_timers ++; 122 if (tn-> get_type () = this) 123 hndlr_timers ++; 124 125 if (showAll | (tn-> get_type () = this) 126 {127 char str [64]; 128 ACE_Date_Time dt; 129 130 dt. update (tn-> get_timer_value (); 131 sprintf (str, 132 "% 02ld/% 02ld/% 04ld % 02ld: % 02ld: % 02ld. % 03ld ", 133 dt. day (), 134 dt. month (), 135 dt. year (), 136 dt. hour (), 13 7. dt. minute (), 138 dt. second (), 139 dt. microsec ()/1000); 140 TRACE_DEBUG ("Timer Id # % d: Hndlr = 0x % x, Timer/Act: % ld, Interval: % d, expiry = % s \ n ", 141 tn-> get_timer_id (), 142 tn-> get_type (), 143 (long int) tn-> get_act (), 144 tn-> get_interval (). sec (), 145 str); 146} 147} 148 149 char str [64]; 150 ACE_Date_Time dt; 151 dt. update (reactor_timerQ-> earliest_time (); 152 153 sprintf (str, 154 "% 02ld/% 02ld/% 04ld % 0 2ld: % 02ld: % 02ld. % 03ld ", 155 dt. day (), 156 dt. month (), 157 dt. year (), 158 dt. hour (), 159 dt. minute (), 160 dt. second (), 161 dt. microsec ()/1000); 162 163 TRACE_INFO ("Total Timers = % d, Timers for Handler [0x % x] = % d, Skew = % d, earliest = % s \ n ", 164 total_timers, this, hndlr_timers, reactor_timerQ-> timer_skew (). sec (), str); 165 166 return; 167} 168}; 169 170 template <class T> 171 class subTimerHandler: public CE_Event_Handler172 {173 std: vector <int> timers; 174 T * parent; 175 int numTimers; 176 public: 177 subTimerHandler (T * parent, int numTimers): timers (numTimers, -1) 178 {179 this-> parent = parent; 180 this-> numTimers = numTimers; 181} 182 183 subTimerHandler () 184 {185 for (unsigned int I = 0; I <timers. size (); I ++) 186 {187 if (timers [I]! =-1) 188 ACE_Reactor: instance ()-> cancel_timer (timers [I]); 189} 190 191 192 int handle_timeout (const ACE_Time_Value & current_time, 193 const void * arg) 194 {195 long int timerType = (long int) arg; 196 timers [timerType] =-1; 197 parent-> handleSubTimeout (timerType ); 198 return 0; 199} 200 201 int startTimer (int timerType, const ACE_Time_Value & delay) 202 {203 if (timerType> numTimers-1) // No such timer Type204 return-2; 205 if (timerType <1) 206 return-1; 207 if (timers [timerType]! =-1) // Timer already running208 return-3; 209 timers [timerType] = 210 ACE_Reactor: instance ()-> schedule_timer (this, 211 (const void *) timerType, 212 delay); 213 return timers [timerType]; 214} 215 int stopTimer (int timerType) 217 {218 if (timerType> numTimers-1 | // No such timer type219 timerType <1 | 220 timers [timerType] =-1) // Timer not already running221 return-1; 222 ACE_Reactor: insta Nce ()-> cancel_timer (timers [timerType]); 223 timers [timerType] =-1; 224 return 0; 225} 226 227 bool timerStarted (int timerType) 228 {229 return (timers [timerType]! =-1); 230} 231 232}; 233 234 # endif/* _ TimerHandler_h */

 

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.