一种基于ACE的简单应用程序模型
来源:互联网 发布:2016年地区数据库 编辑:程序博客网 时间:2024/05/22 15:08
ACE定时器队列
2009-07-01 20:44:55| 分类:ACE | 标签: |字号大中小 订阅
哎,无语了,看到了examples目录下的高级例子我就有种想哭的感觉,我的天那,这还要学多久啊,仅凭我一己之力的话。我特别讨厌这种感觉,无法忍受自己的无知,没办法,谁叫我天真愚昧地活了二十多年呢,醒悟的开始便是痛苦的开端!Reactor是ACE一个非常核心的东西,我从各个层面角度去接近它,不断积累自己的直观感受,好累呀~~
下面这个例子就是从examples中摘抄过来的,它演示了定时器队列的使用,这个例子没有用到反应器,其实反应器内部有一个定时器队列,所以其才能使用schedule_timer添加定时器,这个例子开启了10个定时器,每个定时器使用一个事件处理器。如果处理器处理的业务非常费时的话,那将影响别的定时器的响应。ACE定时器队列似乎是不断地取最早到期的定时器,然后使用while循环等待它到期,然后执行它的处理器,之后立即返回,继续下一次循环,这样方式简单又好用,我在里面没有看到select的影子啊,难道真的用这么简单的方式实现定时器??因为一个定时器队列中所有的处理器都在一个线程中处理,如果处理器处理的业务非常费时的话,那将影响别的定时器的响应。如果能将定时器的事件处理器放到单独线程中处理就好了。定时器们需要保存,需要不断地取出被删除,这就有多种数据结构存储定时器,它们分别偏重某一方面的性能。Reactor默认使用的ACE_Timer_Heap,它使用的数据结构是堆,类似的还有ACE_Timer_Hash,ACE_Timer_Wheel,ACE_Timer_List等,schedule_timer所做的工作就是将定时器保存起来,移除定时器的函数是cancel_timer,数据结构不一样,这两个操作的时间复杂度也不一样。策略模式大概ACE使用最多的设计模式吧,什么东西都封装一层,可以很方便地更换内部业务处理的类以适用不同的场合,这个例子就是一个典型的示例,"Timer::instance ()->set (timer_queue);",timer_queue可以更换成别的类型的队列。
#include "ace/Log_Msg.h"
#include "ace/Event_Handler.h"
#include "ace/Singleton.h"
#include "ace/Synch.h"
#include "ace/Timer_Queue.h"
#include "ace/Timer_Queue.h"
#include "ace/Timer_Heap.h"
#include "ace/Timer_Wheel.h"
#include "ace/Timer_Hash.h"
#include "ace/Timer_List.h"
#include "ace/OS_NS_unistd.h"
class Timer_Dispatcher
{
public:
void wait_for_event (void);
long schedule (ACE_Event_Handler *cb,
void *arg,
const ACE_Time_Value &abs_time,
const ACE_Time_Value &interval);
int cancel (ACE_Event_Handler *cb,
int dont_call_handle_close = 1);
int reset_interval (long timer_id,
const ACE_Time_Value &interval);
void set (ACE_Timer_Queue *timer_queue);
private:
ACE_Timer_Queue *timer_queue_;
ACE_Event timer_;
};
typedef ACE_Singleton<Timer_Dispatcher, ACE_Null_Mutex> Timer;
void Timer_Dispatcher::wait_for_event (void)
{
ACE_TRACE (ACE_TEXT ("Timer_Dispatcher::wait_for_event"));
while (1)
{
//这个会更新所有的定时器,先移除每一个定时器,然后再将定时器重新加入队列中
//在反应器中使用定时器你不需要自己调用这个函数,反应器内部其实调用这个函数
this->timer_queue_->expire ();
}
}
long Timer_Dispatcher::schedule (ACE_Event_Handler *cb,
void *arg,
const ACE_Time_Value &abs_time,
const ACE_Time_Value &interval)
{
ACE_TRACE (ACE_TEXT ("Timer_Dispatcher::schedule_timer"));
return this->timer_queue_->schedule(cb, arg, abs_time, interval);
}
int Timer_Dispatcher::cancel (ACE_Event_Handler *cb,
int dont_call_handle_close)
{
ACE_TRACE (ACE_TEXT ("Timer_Dispatcher::cancel"));
return timer_queue_->cancel (cb, dont_call_handle_close);
}
void Timer_Dispatcher::set (ACE_Timer_Queue *timer_queue)
{
ACE_TRACE (ACE_TEXT ("Timer_Dispatcher::set"));
timer_queue_ = timer_queue;
}
int Timer_Dispatcher::reset_interval (long timer_id,
const ACE_Time_Value &interval)
{
ACE_TRACE (ACE_TEXT ("Timer_Dispatcher::reset_interval"));
return timer_queue_->reset_interval(timer_id, interval);
}
class CB : public ACE_Event_Handler
{
public:
CB ();
// Set the timer id that is being handled by this instance.
void setID (long timerID);
// Get the timer id.
long getID (void);
// Handle the timeout.
virtual int handle_timeout(const ACE_Time_Value &tv,
const void *arg = 0);
virtual int handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask close_mask);
private:
long timerID_;
int count_;
};
CB::CB () : count_(0)
{
ACE_TRACE (ACE_TEXT ("CB::CB"));
}
int CB::handle_timeout (const ACE_Time_Value &,
const void *arg)
{
ACE_TRACE (ACE_TEXT ("CB::handle_timeout"));
ACE_DEBUG((LM_DEBUG, "(%P : %t) timer %d timeout\n", timerID_));
//此时延时的话,定时器就全乱套了
//这就是单线程的不利之处
//ACE_OS::sleep(40);
if (count_ == 5)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Reseting interval for timer %d\n"),
timerID_));
// New interval is 10 ms.
ACE_Time_Value interval (0L, 1000L);
int status = Timer::instance ()->reset_interval
(timerID_, interval);
}
if (count_++ == 10)
{
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Canceling %d\n"),
timerID_));
ACE_ASSERT ((Timer::instance ()->cancel (this)) != 0);
}
return 0;
}
void CB::setID (long timerID)
{
ACE_TRACE (ACE_TEXT ("CB::setID"));
timerID_ = timerID;
}
long CB::getID (void)
{
ACE_TRACE (ACE_TEXT ("CB::getID"));
return timerID_;
}
int CB::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
{
ACE_TRACE (ACE_TEXT ("CB::handle_close"));
return 0;
}
int ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_Timer_Queue *timer_queue = new ACE_Timer_Heap;
// setup the timer queue
Timer::instance ()->set (timer_queue);
CB cb[10];
long args[10];
for (long i = 0; i < 10 ; i++)
{
ACE_Time_Value const timeout (i);
long timerID =
Timer::instance ()->schedule
(&cb[i],
&args[i],
timer_queue->gettimeofday () + (ACE_Time_Value)5,
timeout);
// Set the timerID state variable of the handler.
cb[i].setID (timerID);
// Implicitly send the handler it's timer id.
args[i] = timerID;
}
// "run" the timer.
Timer::instance ()->wait_for_event ();
return 0;
}
- 一种基于ACE的简单应用程序模型
- 高等C++ 之 一种基于ACE的调度器设计
- 一种基于RBAC的扩展模型探讨
- 一种基于神经网络的对话模型
- 一种简单的多进程编程模型
- 简单实现一种经典的数据传输模型
- ACE的反应器模型
- ACE的Select模型
- 一种基于 SOA 的应用程序的动态实现框架方案
- 简单ACE的 Makefile
- 一种基于云计算方式的任务处理模型
- 一种基于邻接图模型的改进LDA算法
- 一种基于主客体模型的权限管理框架
- 一种基于主客体模型的权限管理框架
- 基于ACE Reacotr FTP server 简单实现
- 基于Accept的IOCP简单模型
- 一种简单的图像显著性计算模型
- 一种简单的图像显著性计算模型
- mysql 不 能启动的原因
- ZOJ 3258 Wistone and Owenwater 01背包+完全背包
- Jython学习笔记(一) Eclipse开发环境搭建
- 网页链接Oracle实现数据的增删改查
- chrony, ntp, 同步
- 一种基于ACE的简单应用程序模型
- ffmpeg installation
- wireless setup
- Mac OS X系统应该如何维护和保养
- Android学习笔记之AndroidManifest.xml文件解析
- 通过struts2的配置文件访问jsp
- Android自测题13
- Android应用程序资源访问
- RecSys的Yelp推荐比赛数据简单分析