基于ACE的定时器模板类

  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 &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
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 &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 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 */

基于ACE的定时器模板类,布布扣,bubuko.com

时间: 2024-07-30 17:47:56

基于ACE的定时器模板类的相关文章

【C++】智能指针类和OpenCV的Ptr模板类

智能指针类 引用计数 智能指针(smart pointer)的一种通用实现技术是使用引用计数(reference count).智能指针类将一个计数器与类指向的对象相关联,引用计数跟踪该类有多少个对象的指针指向同一对象.引用计数为0时,删除对象. 其基本使用规则是: 每次创建类的新对象时,初始化指针并将引用计数置为1.当对象作为另一对象的副本而创建时,复制构造函数复制指针并增加与之相应的引用计数的值.对一个对象进行赋值时,赋值操作符减少左操作数所指对象的引用计数的值(如果引用计数减至0,则删除对

(转)JDBC模板类。

Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTemplate类通过模板设计模式帮助我们消除了冗长的代码,只做需要做的事情(即可变部分),并且帮我们做哪些固定部分,如连接的创建及关闭. JdbcTemplate类对可变部分采用回调接口方式实现,如ConnectionCallback通过回调接口返回给用户一个连接,从而可以使用该连 接做任何事情.State

浅谈JavaEE中的JDBC模板类的封装实现以及合理的建立项目包结构(一)

从今天开始我们一起来聊下有关,javaEE开发中的一些知识,JavaEE的开发用于企业级的开发,但是现在企业中一般也不会使用JDBC开发,大部分都是使用自己公司开发的一套的框架,但是这些框架的架构一般也是会模仿着有名JavaEE开源三大开发框架SSH(Struts2+Spring+Hibernate)或者现在也很流行的SSM开发框架(Spring+SpringMVC+MyBatis) 来进行深度定制以便于适合自己企业的实际开发需求.有的人曾说既然去公司又是重新学习一套框架,还有必要学习开源的三大

Spring(二):AOP(面向切面编程),Spring的JDBC模板类

1 AOP概述 1.2 什么是AOP 在软件业,AOP为Aspect Oriented Programmig的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型.利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率. AOP解决了OOP遇到一些问题,采取横向抽取机制,取代了传统

模板类与类模板、函数模板与模板函数等的区别

在C++中有好几个这样的术语,但是我们很多时候用的并不正确,几乎是互相替换混淆使用.下面我想彻底辨清几个术语,这样就可以避免很多概念上的混淆和使用上的错误.这几个词是: 函数指针——指针函数 数组指针——指针数组 类模板——模板类 函数模板——模板函数 最终在使用中,我们就可以让它们实至名归,名正言顺. 1.函数指针——指针函数   函数指针的重点是指针.表示的是一个指针,它指向的是一个函数,例子: int   (*pf)(); 指针函数的重点是函数.表示的是一个函数,它的返回值是指针.例子:

开涛spring3(7.2) - 对JDBC的支持 之 7.2 JDBC模板类

7.2  JDBC模板类 7.2.1  概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTemplate类通过模板设计模式帮助我们消除了冗长的代码,只做需要做的事情(即可变部分),并且帮我们做哪些固定部分,如连接的创建及关闭. JdbcTemplate类对可变部分采用回调接口方式实现,如ConnectionCallback通过回调接口返回给用户一个连

C++模板类与Qt信号槽混用

一.正文 目前正在做一个视频处理相关的项目.项目的技术栈是这样的,UI层采用Qt来实现基本的数据展示和交互,底层音视频采用的是一套基于FFmpeg的视频处理框架.这是一套类似Microsoft Media Foundation的处理框架,采用管道流进行架构,解复用.解码.复用.编码及用户自定义操作都采用Filter组件来实现,灵活度和可扩展性都比较好.(基本上常用音视频处理框架都采用了这一架构,如Microsoft Media Foundation, DirectShow Filter, gst

模板类的友元重载

模板类的友元重载和普通类的友元重载有不同之处,可以参考这篇CSDN博客http://blog.csdn.net/ozwarld/article/details/7770808 #include <iostream> using namespace std; template <class T> class Test; // 模板类前置声明 template<class T> ostream& operator << (ostream& out

C++ 模板函数与模板类

一.模板函数 函数模板提供了一类函数的抽象,即代表了一类函数.当函数模板被实例化后,它会生成具体的模板函数.例如下面便是一个函数模板: 当实际调用它时,就会生成具体的模板函数:    模板函数在调用过程中会进行数据类型的自动匹配(在不产生歧义的情况下),但如果需要指定类型的话,可以显示声明,如: 这样,函数模板中的T就会被double所代替. 自动匹配有以下的规则: 1) 函数实参是否满足模板的实参(此时的判断没有数据类型的转换): 2) 若不满足1), 函数实参进行数据转换在进行匹配: 3)