1 ACETimerClockGenerator.h 2 ClockGeneratorIF.h 3 在類中定義一個結構體,在結構體中定義一個函數。 4 在結構體中定義一個函數,這樣做有什麼好呢? 5 6 TimerHandler.h 7 用了模板的方法去構造定時器類。有助於底層調用上層。在構造的時候就初始化一個類中最大的定時器個數,及模板類(也就是parent)。 8 TimerHandler(T *parent, int numTimers) : timers(numTimers, -1) 9 { //初始化向量,賦值給私有的成員變量。 10 this->parent = parent; 11 this->numTimers = numTimers; 12 } 13 14 15 用到了STL向量: 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 ¤t_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 104 void showTimers(bool showAll) 105 { 106 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 for (; !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(), 137 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 %02ld:%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 ACE_Event_Handler 172 { 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 ¤t_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 type 204 return -2; 205 if (timerType < 1) 206 return -1; 207 if (timers[timerType] != -1) // Timer already running 208 return -3; 209 timers[timerType] = 210 ACE_Reactor::instance()->schedule_timer(this, 211 (const void *)timerType, 212 delay); 213 return timers[timerType]; 214 } 215 216 int stopTimer(int timerType) 217 { 218 if (timerType > numTimers-1 || // No such timer type 219 timerType < 1 || 220 timers[timerType] == -1) // Timer not already running 221 return -1; 222 ACE_Reactor::instance()->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 */