基于ACE的定时器模板类
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 ¤t_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 ¤t_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的定时器模板类的更多相关文章
- 基于ace后台管理系统模板--CMS(Thinkphp框架)的筹划
临近春节,准备自己做一个关于宠物的cms网站,特写下此博客提醒自己,尽量争取在过年前做好.废号少说,先梳理下接下来准备使用的工具.. 由于最近在学习thinkphp,所以打算用这个框架来作为主体,可能 ...
- 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
并发编程概述 前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...
- c++ 跨平台线程同步对象那些事儿——基于 ace
前言 ACE (Adaptive Communication Environment) 是早年间很火的一个 c++ 开源通讯框架,当时 c++ 的库比较少,以至于谈 c++ 网络通讯就绕不开 ACE, ...
- 开涛spring3(7.2) - 对JDBC的支持 之 7.2 JDBC模板类
7.2 JDBC模板类 7.2.1 概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...
- C++模板类与Qt信号槽混用
一.正文 目前正在做一个视频处理相关的项目.项目的技术栈是这样的,UI层采用Qt来实现基本的数据展示和交互,底层音视频采用的是一套基于FFmpeg的视频处理框架.这是一套类似Microsoft Med ...
- (转)JDBC模板类。
Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTempl ...
- spring3:对JDBC的支持 之 JDBC模板类
7.2 JDBC模板类 7.2.1 概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...
- QCache 缓存(模板类,类似于map,逻辑意义上的缓存,方便管理,和CPU缓存无关。自动获得被插入对象的所有权,超过一定数量就会抛弃某些值)
在软件开发中,我们经常需要在内存中存储一些临时数据用于后续相关计算.我们一般把这些数据存储到某个数组里,或者STL中的某个合适的容器中.其实,在Qt中直接为我们提供了一个QCache类专用于这种需求. ...
- C++之Stack模板类
假设有这样一种情况:某人将一车文件交给小王.倘若小王的抽屉是空的,那么小王从车上取出最上面的文件将其放入抽屉:倘若抽屉是满的,小王从抽屉中取出最上面的文件,放入垃圾篓:倘若抽屉即不空也未满,那么小王抛 ...
随机推荐
- HDOJ-ACM1061(JAVA) Rightmost Digit
题意:求n的n次方的个位数(1<=N<=1,000,000,000) 第一个最愚蠢的办法就是暴力破解,没什么意义,当然,还是实现来玩玩. 以下是JAVA暴力破解: import java. ...
- Dllimport函数時无法在Dll中找到的入口点
今天開發客戶提供的一個dll時出現無法找到入口點問題,由於客戶也不能明確說明dll,所以一時不知道如何下手,經查詢後找到可通過vs自帶的dumpbin.exe查看. Dumpbin.exe位于 VS的 ...
- 问题-[WIN8.132位系统]安装Win8.1 遇到无法升级.NET Framework 3.5.1
问题现象:安装Win8后都遇到了无法升级.NET Framework 3.5.1的问题,在线升级会遇到错误0x800F0906.这使得91手机助手等很多软件无法运行,更郁闷的是,网上几乎所有的解决办法 ...
- 教程-delphi的开源json库:superobject,用法简介
困惑一天的问题 一个语句搞定了... 回头细说. superobject中的{$DEFINE UNICODE} 就是它,这是json官方推荐的Delphi处理json的包,地址: http://www ...
- mongodb在java驱动包下的操作(转)
推荐几章很有用的文章 java操作参考文档 http://www.cnblogs.com/hoojo/archive/2011/06/02/2068665.html http://blog.csdn. ...
- JAVA自动生成正则表达式工具类
经过很久的努力,终于完成了JAVA自动生成正则表达式工具类.还记得之前需要正则,老是从网上找吗?找了想修改也不会修改.现在不用再为此烦恼了,使用此生成类轻松搞定所有正则表达式.赶快在同事面前炫一下吧. ...
- oncopy和onpaste
在Javascript中,有对应的事件能够监听复制和粘贴,那就是oncopy和onpaste. oncopy: demo: <body oncopy="alert('不能复制');re ...
- systemtap 技巧系列 +GDB
http://blog.csdn.net/wangzuxi/article/category/2647871
- CSharp Algorithm - How to traverse binary tree by breadth (Part II)
/* Author: Jiangong SUN */ Here I will introduce the breadth first traversal of binary tree. The pri ...
- [原理][源代码解析]spring中@Transactional,Propagation.SUPPORTS,以及 Hibernate Session,以及jdbc Connection关系---转载
问题: 一. 1. Spring 如何处理propagation=Propagation.SUPPORTS? 2. Spring 何时生成HibernateSession ? 3. propagati ...