ACETimerClockGenerator.h
ClockGeneratorIF.h
在类中定义一个结构体,在结构体中定义一个函数。
在结构体中定义一个函数,这样做有什么好呢? TimerHandler.h
用了模板的方法去构造定时器类。有助于底层调用上层。在构造的时候就初始化一个类中最大的定时器个数,及模板类(也就是parent)。
TimerHandler(T *parent, int numTimers) : timers(numTimers, -)
{ //初始化向量,赋值给私有的成员变量。
this->parent = parent;
this->numTimers = numTimers;
} 用到了STL向量:
std::vector<int> timers; ACE中
. startTimer(int timerType, const ACE_Time_Value &delay)
ACE_Reactor::instance()->schedule_timer()
. stopTimer(int timerType)
ACE_Reactor::instance()->cancel_timer(timers[timerType]);
. showTimers(bool showAll)
ACE_Timer_Queue *reactor_timerQ = ACE_Reactor::instance()->timer_queue();
ACE_Timer_Queue_Iterator &iter = reactor_timerQ->iter(); #ifndef _TimerHandler_h
#define _TimerHandler_h #include <vector>
#include "ace/Timer_Queue.h"
#include "ace/Date_Time.h"
#include "ace/Event_Handler.h"
#include "ace/Reactor.h" #include "TraceUtils.h" template <class T>
class TimerHandler : public ACE_Event_Handler
{
std::vector<int> timers;
T *parent;
int numTimers;
public:
TimerHandler(T *parent, int numTimers) : timers(numTimers, -)
{
this->parent = parent;
this->numTimers = numTimers;
} ~TimerHandler()
{
for (unsigned int i = ; i < timers.size(); i++)
{
if (timers[i] != -)
ACE_Reactor::instance()->cancel_timer(timers[i]);
}
} int handle_timeout (const ACE_Time_Value &current_time,
const void *arg)
{
long int timerType = (long int)arg;
timers[timerType] = -;
parent->handleTimeout(timerType);
return ;
} int startTimer(int timerType, const ACE_Time_Value &delay)
{
if (timerType > numTimers-) // No such timer type
return -;
if (timerType < )
return -;
if (timers[timerType] != -) // Timer already running
return -;
timers[timerType] =
ACE_Reactor::instance()->schedule_timer(this,
(const void *)timerType,
delay);
return timers[timerType];
} int stopTimer(int timerType)
{
if (timerType > numTimers- || // No such timer type
timerType < ||
timers[timerType] == -) // Timer not already running
return -;
ACE_Reactor::instance()->cancel_timer(timers[timerType]);
timers[timerType] = -;
return ;
} bool timerStarted (int timerType)
{
return (timers[timerType] != -);
} void showTimers(bool showAll)
{
ACE_Timer_Queue *reactor_timerQ = ACE_Reactor::instance()->timer_queue();
ACE_Timer_Queue_Iterator &iter = reactor_timerQ->iter(); if (reactor_timerQ->is_empty())
{
TRACE_DEBUG("No Timers in Queue\n");
} int total_timers=, hndlr_timers=; TRACE_DEBUG("Timers in queue:\n");
for (; !iter.isdone (); iter.next ())
{
ACE_Timer_Node *tn = iter.item (); total_timers++;
if (tn->get_type() == this)
hndlr_timers++; if (showAll || (tn->get_type() == this))
{
char str[];
ACE_Date_Time dt; dt.update(tn->get_timer_value());
sprintf(str,
"%02ld/%02ld/%04ld %02ld:%02ld:%02ld.%03ld",
dt.day(),
dt.month(),
dt.year(),
dt.hour(),
dt.minute(),
dt.second(),
dt.microsec() / );
TRACE_DEBUG("Timer Id #%d: Hndlr=0x%x, Timer/Act: %ld, Interval: %d, Expiry= %s\n",
tn->get_timer_id (),
tn->get_type(),
(long int)tn->get_act(),
tn->get_interval().sec(),
str);
}
} char str[];
ACE_Date_Time dt;
dt.update(reactor_timerQ->earliest_time()); sprintf(str,
"%02ld/%02ld/%04ld %02ld:%02ld:%02ld.%03ld",
dt.day(),
dt.month(),
dt.year(),
dt.hour(),
dt.minute(),
dt.second(),
dt.microsec() / ); TRACE_INFO("Total Timers= %d, Timers for Handler[ 0x%x ]= %d, Skew=%d, Earliest= %s\n",
total_timers, this, hndlr_timers, reactor_timerQ->timer_skew().sec(), str); return;
}
}; template <class T>
class subTimerHandler : public ACE_Event_Handler
{
std::vector<int> timers;
T *parent;
int numTimers;
public:
subTimerHandler(T *parent, int numTimers) : timers(numTimers, -)
{
this->parent = parent;
this->numTimers = numTimers;
} subTimerHandler()
{
for (unsigned int i = ; i < timers.size(); i++)
{
if (timers[i] != -)
ACE_Reactor::instance()->cancel_timer(timers[i]);
}
} int handle_timeout (const ACE_Time_Value &current_time,
const void *arg)
{
long int timerType = (long int)arg;
timers[timerType] = -;
parent->handleSubTimeout(timerType);
return ;
} int startTimer(int timerType, const ACE_Time_Value &delay)
{
if (timerType > numTimers-) // No such timer type
return -;
if (timerType < )
return -;
if (timers[timerType] != -) // Timer already running
return -;
timers[timerType] =
ACE_Reactor::instance()->schedule_timer(this,
(const void *)timerType,
delay);
return timers[timerType];
} int stopTimer(int timerType)
{
if (timerType > numTimers- || // No such timer type
timerType < ||
timers[timerType] == -) // Timer not already running
return -;
ACE_Reactor::instance()->cancel_timer(timers[timerType]);
timers[timerType] = -;
return ;
} bool timerStarted (int timerType)
{
return (timers[timerType] != -);
} }; #endif /* _TimerHandler_h */

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

  1. 基于ace后台管理系统模板--CMS(Thinkphp框架)的筹划

    临近春节,准备自己做一个关于宠物的cms网站,特写下此博客提醒自己,尽量争取在过年前做好.废号少说,先梳理下接下来准备使用的工具.. 由于最近在学习thinkphp,所以打算用这个框架来作为主体,可能 ...

  2. 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)

    并发编程概述   前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...

  3. c++ 跨平台线程同步对象那些事儿——基于 ace

    前言 ACE (Adaptive Communication Environment) 是早年间很火的一个 c++ 开源通讯框架,当时 c++ 的库比较少,以至于谈 c++ 网络通讯就绕不开 ACE, ...

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

    7.2  JDBC模板类 7.2.1  概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...

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

    一.正文 目前正在做一个视频处理相关的项目.项目的技术栈是这样的,UI层采用Qt来实现基本的数据展示和交互,底层音视频采用的是一套基于FFmpeg的视频处理框架.这是一套类似Microsoft Med ...

  6. (转)JDBC模板类。

    Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTempl ...

  7. spring3:对JDBC的支持 之 JDBC模板类

    7.2  JDBC模板类 7.2.1  概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...

  8. QCache 缓存(模板类,类似于map,逻辑意义上的缓存,方便管理,和CPU缓存无关。自动获得被插入对象的所有权,超过一定数量就会抛弃某些值)

    在软件开发中,我们经常需要在内存中存储一些临时数据用于后续相关计算.我们一般把这些数据存储到某个数组里,或者STL中的某个合适的容器中.其实,在Qt中直接为我们提供了一个QCache类专用于这种需求. ...

  9. C++之Stack模板类

    假设有这样一种情况:某人将一车文件交给小王.倘若小王的抽屉是空的,那么小王从车上取出最上面的文件将其放入抽屉:倘若抽屉是满的,小王从抽屉中取出最上面的文件,放入垃圾篓:倘若抽屉即不空也未满,那么小王抛 ...

随机推荐

  1. C语言之Static

    1.全局静态变量 在全局变量之前加上关键字static,全局变量就被定义成为一个全局静态变量. 1)内存中的位置:静态存储区(静态存储区在整个程序运行期间都存在) 2)初始化:未经初始化的全局静态变量 ...

  2. HDOJ-ACM1015(JAVA) 运用 组合、全排列实现

    转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5573934.html 这个题目的题意:(自己结合百度翻译,简单的翻译了一下) “这个项目是在一个在二楼图书馆一幅 ...

  3. hadoop-2.6.0.tar.gz + spark-1.5.2-bin-hadoop2.6.tgz 的集群搭建(3节点和5节点皆适用)

    本人呕心沥血所写,经过好一段时间反复锤炼和整理修改.感谢所参考的博友们!同时,欢迎前来查阅赏脸的博友们收藏和转载,附上本人的链接.http://www.cnblogs.com/zlslch/p/584 ...

  4. PC-IIS因为端口问题报错的解决方法

    1.我的电脑-管理-服务和应用程序-Internet信息服务  情况:这时发现“默认 SMTP 虚拟服务器”停止  解决方法:右击启动  情况:发现网页还是打不开.2.Internet信息服务-网站- ...

  5. jQuery获取鼠标移动方向2

    (function($) { $.fn.extend({ show: function(div) { var w = this.width(), h = this.height(), xpos = w ...

  6. 东芝超级本从win8到win7

    东芝超级本从win8到win7 2014年2月20日 11:08:46   1. 进入BIOS     调出关机选项,按住shift不松手,然后点选关机,彻底关机后,按住f2不松手,按下电源开机,就进 ...

  7. DevExpress GridControl 显示行号、设置行号宽

    显示行号类 /// <summary> /// GridView 显示行号 设置行号列的宽度 /// </summary> /// <param name="g ...

  8. How to find configuration file MySQL uses?

    http://www.dbasquare.com/2012/04/01/how-to-find-mysql-configuration-file/ A customer called me today ...

  9. Unity3D问题之EnhanceScollView选择角色3D循环滚动效果实现

    需求 呈现3D效果(2D素材)选择角色效果 滚动保证层级.缩放比例.间距正常尾随 循环滚动 这个界面需求一般也会有游戏会採用(貌似有挺多) 怎样实现 实现技术关键点 (3D循环效果,依据数学函数和细致 ...

  10. js select onchange事件

    <select id='a' name='a' onchange="javascript:alert('测试');">