ACE 时间泵

// --------------------------------------------------------------
// 
// Copyright (C) 2008 - All Rights Reserved
// 
// File:  TimerPump_T
// Version:  1.0
// Date:   2008-4-10
// 
// Purpose: 
// 
// --------------------------------------------------------------

#ifndef TimerPump_T_H
#define TimerPump_T_H

//

#include <ACE/Task.h>
#include <ACE/Event.h>

//

template <class TimerHandler, class TimerQueue>
class TimerPump_T : public ACE_Task_Base
{
public:
 int start() { return activate(); }
 int stop()
 {
  this->thr_mgr()->cancel_task(this);
  return wait();
 }

 int svc()
 {
  ACE_TRACE(ACE_TEXT("PTimer_Dispatcher::wait_for_event"));

  ACE_Thread_Manager *thread_manager = this->thr_mgr();
  while (true) {
   if (thread_manager->testcancel(ACE_OS::thr_self())) {
    break;
   }

   ACE_Time_Value max_tv = timer_queue_.gettimeofday();
   ACE_Time_Value *this_timeout = this->timer_queue_.calculate_timeout(&max_tv);

   if (*this_timeout == ACE_Time_Value::zero) {
    this->timer_queue_.expire();
   }
   else {
    // Convert to absolute time.
    ACE_Time_Value next_timeout = timer_queue_.gettimeofday();
    next_timeout += *this_timeout;

    if (this->timer_.wait(&next_timeout) == -1) {
     this->timer_queue_.expire();
    }
   }
  }

  return 0;
 }
 long schedule(TimerHandler &timer_handler, void *arg, const ACE_Time_Value &abs_time, const ACE_Time_Value &interval)
 {
  ACE_TRACE(ACE_TEXT("PTimer_Dispatcher::schedule_timer"));

  return this->timer_queue_.schedule(&timer_handler, arg, abs_time, interval);
 }
 int cancel(TimerHandler &timer_handler, int dont_call_handle_close = 1)
 {
  ACE_TRACE(ACE_TEXT("PTimer_Dispatcher::cancel"));

  return timer_queue_.cancel(&timer_handler, dont_call_handle_close);
 }
 int reset_interval(long timer_id, const ACE_Time_Value &interval)
 {
  ACE_TRACE(ACE_TEXT("PTimer_Dispatcher::reset_interval"));

  return timer_queue_.reset_interval(timer_id, interval);
 }

private:
 ACE_Event timer_;

 TimerQueue timer_queue_;
};

//

#endif
 
/


// --------------------------------------------------------------
// 
// Copyright (C) 2008 - All Rights Reserved
// 
// Author:  LiuYin
// File:  UpcallHandler_T
// Version:  1.0
// Date:   2008-4-10
// 
// Purpose: 
// 
// --------------------------------------------------------------

#ifndef UpcallHandler_T_H
#define UpcallHandler_T_H

//

#include <ACE/Timer_Queue_T.h>

//

template <class TimerHandler, class Mutex>
class UpcallHandler_T
{
 typedef UpcallHandler_T<TimerHandler, Mutex> This;

 typedef ACE_Timer_Queue_T<TimerHandler *, This, Mutex> TimerQueue;
public:
 // The signature of this method changed at ACE 5.4. The 'recurring_timer' parameter was added.
 int timeout(TimerQueue &timer_queue, TimerHandler *handler, const void *arg, int recurring_timer, const ACE_Time_Value &cur_time)
 {
  return handler->handle_timer_out();
 }
 // This method is called when the timer queue is destroyed and the timer is still contained in it.
 int deletion(TimerQueue &timer_queue, TimerHandler *handler, const void *arg)
 {
  return handler->handle_timer_queue_deletion();
 }

 // The following methods don't appear before ACE 5.4, so aren't referenced in APG (it's based on ACE 5.3).

 // This method is called when a timer is registered.
 int registration(TimerQueue &timer_queue, TimerHandler *handler, const void *arg)
 {
  return handler->handle_timer_registration();
 }
 // This method is called before the timer expires.
 int preinvoke(TimerQueue &timer_queue, TimerHandler *handler, const void *arg, int recurring_timer, const ACE_Time_Value &cur_time, const void *&upcall_act)
 {
  return 0;
 }
 // This method is called after the timer expires.
 int postinvoke(TimerQueue &timer_queue, TimerHandler *handler, const void *arg, int recurring_timer, const ACE_Time_Value &cur_time, const void *upcall_act)
 {
  return 0;
 }
 // This method is called when a handler is cancelled.
 int cancel_type(TimerQueue &timer_queue, TimerHandler *handler, int dont_call, int &requires_reference_counting)
 {
  int ret = 0;
  if (!dont_call) {
   ret = handler->handle_timer_cancel();
  }
  return ret;
 }

 // This method is called when a timer is cancelled.
 int cancel_timer(TimerQueue &timer_queue, TimerHandler *handler, int dont_call, int requires_reference_counting)
 {
  int ret = 0;
  if (!dont_call) {
   ret = handler->handle_timer_cancel();
  }
  return ret;
 }
};

//

#endif

/


#include <ACE/OS_NS_unistd.h>
#include <ACE/Timer_Heap_T.h>

#include "TimerPump_T.h"
#include "UpcallHandler_T.h"

//

class CTimerHandler
{
public:
 int handle_timer_out()
 {
  ACE_DEBUG((LM_DEBUG, "int handle_time_out()/n"));
  return 0;
 }

 int handle_timer_queue_deletion()
 {
  ACE_DEBUG((LM_DEBUG, "int handle_time_queue_deletion()/n"));
  return 0;
 }

 int handle_timer_registration()
 {
  ACE_DEBUG((LM_DEBUG, "int handle_timer_registration()/n"));
  return 0;
 }

 int handle_timer_cancel()
 {
  ACE_DEBUG((LM_DEBUG, "int handleCancel()/n"));
  return 0;
 }
};

typedef UpcallHandler_T<CTimerHandler, ACE_Thread_Mutex> CUpcallHandler;
typedef ACE_Timer_Heap_T<CTimerHandler *, CUpcallHandler, ACE_Thread_Mutex> CTimerQueue;
typedef TimerPump_T<CTimerHandler, CTimerQueue> CTimerPump;

//

int ACE_TMAIN(int, ACE_TCHAR *[])
{
 CTimerPump timer_pump;
 CTimerHandler time_handler1;

 timer_pump.start();

 timer_pump.schedule(time_handler1, 0, ACE_OS::gettimeofday(), ACE_Time_Value(2));

 ACE_OS::sleep(15);
 timer_pump.stop();
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值