基于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模板类
假设有这样一种情况:某人将一车文件交给小王.倘若小王的抽屉是空的,那么小王从车上取出最上面的文件将其放入抽屉:倘若抽屉是满的,小王从抽屉中取出最上面的文件,放入垃圾篓:倘若抽屉即不空也未满,那么小王抛 ...
随机推荐
- hdu5593--ZYB's Tree(树形dp)
问题描述 ZYB有一颗N个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采 ...
- 问题-[Delphi]PixelFormat 图像颜色的数据格式
PixelFormat: (指定图像中每个像素的颜色数据的格式) Delphi 微软 ...
- sqlplus 连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0
sqlplus 连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0 问题描述: 使用sqlplus客户端登录数据库,报 ...
- 编程实例--for循环,找出0~100之间与8有关的正整数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- IOS 中runtime 不可变数组__NSArray0 和__NSArrayI
IOS 中runtime 不可变数组__NSArray0 和__NSArrayI 大家可能都遇到过项目中不可变数组避免数组越界的处理:runtime,然而有时候并不能解决所有的问题,因为类簇不一样 # ...
- Xenomai 的模式切换浅析
在Xenomai的用户空间下,有两种模式:primary mode (主模式) 和 secondary mode(次模式). 在主模式下调用Linux系统调用后程序就会进入次模式,反之,在次模式下调用 ...
- 利用dom4j读取xml文件
对于xml文件的读取,其实有很多方法,例如:SAX实现方法,DOM4J实现方法 ,DOM 实现方法,JDOM实现方法 等等. 下面,我就说下dom4j的读取方法: 1.首先,肯定要引入第三方jar包. ...
- Android代码中动态设置图片的大小(自动缩放),位置
项目中需要用到在代码中动态调整图片的位置和设置图片大小,能自动缩放图片,用ImageView控件,具体做法如下: 1.布局文件 <RelativeLayout xmlns:android=&qu ...
- C++实现建立和一二进制树的三个递归遍历
说明:这篇文章是学习交流,转载请注明出处,欢迎转载! 二叉树是一种常见的数据结构.二叉树的遍历也是家常便饭的事了,这里仅仅写出一个完整的能够执行的C++代码来随便建立一个例如以下图所看到的的二叉树,建 ...
- mysqldump 备份原理8
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; http://www.cnblogs.com/lyhabc/p/38 ...