基于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模板类
假设有这样一种情况:某人将一车文件交给小王.倘若小王的抽屉是空的,那么小王从车上取出最上面的文件将其放入抽屉:倘若抽屉是满的,小王从抽屉中取出最上面的文件,放入垃圾篓:倘若抽屉即不空也未满,那么小王抛 ...
随机推荐
- ZOJ-3721 Final Exam Arrangement 贪心
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3721 容易的贪心题,排个序.. //STATUS:C++_AC_ ...
- light oj 1354 - IP Checking
1354 - IP Checking PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB An I ...
- eas bos 编辑界面 editUIt 属性值为空
在编辑界面,我们可以正常的保存某个值到数据库,同时列表界面也可以正常显示. 但是当我们再次打开这个编辑界面的时候,该属性值缺无法显示. 通常情况下,通过下面两个方法可以解决 方法一: 在编辑界面重写一 ...
- 8-14-Exercise(博弈:HDU 1846 & HDU 1527 )
B.HDU 1846 Brave Game 算是最简单的入门博弈题吧...... 呃......我用的......算是不是方法的方法吧——找规律~ 可以发现:X-M为奇数时,先手会输:而为偶数的 ...
- JSON基本操作
import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.J ...
- 关于python文件转为exe文件
一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装python而在windows系统上运行这个可执行程序. py2e ...
- SQLite 入门教程(二)创建、修改、删除表 (转)
转于 SQLite 入门教程(二)创建.修改.删除表 一.数据库定义语言 DDL 在关系型数据库中,数据库中的表 Table.视图 View.索引 Index.关系 Relationship 和触发器 ...
- java Graphics2D 画图
在Java中,当需要画一些特殊的形状时,比如说椭圆.矩形等,可以使用 Graphics2D 来绘图. 一些API: g.drawLine(3,3,50,50);//画一条线段 g.drawRect(8 ...
- 使用startCoroutine制定倒计时
使用startCoroutine制定倒计时 using UnityEngine; using System.Collections; public class TimerCoroutine : Mon ...
- Linux下crontab命令详解
crontab -e编辑定时任务 * * * shell.sh 从左到右依次是:分钟.小时.天.周.月