00001
00002
00003
00004
00005
00006
00007
00008 #include "DCPS/DdsDcps_pch.h"
00009
00010 #include "Watchdog.h"
00011 #include "Service_Participant.h"
00012
00013 namespace OpenDDS {
00014 namespace DCPS {
00015
00016 namespace {
00017 struct CommandBase : ReactorInterceptor::Command {
00018 explicit CommandBase(ReactorInterceptor* inter)
00019 : interceptor_(inter) {}
00020 ReactorInterceptor* const interceptor_;
00021 };
00022
00023 struct ScheduleCommand : CommandBase {
00024 ScheduleCommand(ReactorInterceptor* inter, const void* act,
00025 const ACE_Time_Value& interval,
00026 long* timer_id)
00027 : CommandBase(inter)
00028 , act_(act)
00029 , interval_(interval)
00030 , timer_id_(timer_id)
00031 {}
00032
00033 void execute()
00034 {
00035 *timer_id_ = interceptor_->reactor()->schedule_timer(interceptor_, act_,
00036 interval_, interval_);
00037 }
00038
00039 const void* const act_;
00040 const ACE_Time_Value interval_;
00041 long* timer_id_;
00042 };
00043
00044 struct CancelCommand : CommandBase {
00045 CancelCommand(ReactorInterceptor* inter, long timer_id)
00046 : CommandBase(inter)
00047 , timer_id_(timer_id)
00048 {}
00049
00050 void execute()
00051 {
00052 if (timer_id_ >= 0) {
00053 interceptor_->reactor()->cancel_timer(timer_id_);
00054 } else {
00055 interceptor_->reactor()->cancel_timer(interceptor_);
00056 }
00057 }
00058
00059 const long timer_id_;
00060 };
00061
00062 struct ResetCommand : CommandBase {
00063 ResetCommand(ReactorInterceptor* inter, long timer_id,
00064 const ACE_Time_Value& interval)
00065 : CommandBase(inter)
00066 , timer_id_(timer_id)
00067 , interval_(interval)
00068 {}
00069
00070 void execute()
00071 {
00072 interceptor_->reactor()->reset_timer_interval(timer_id_, interval_);
00073 }
00074
00075 const long timer_id_;
00076 const ACE_Time_Value interval_;
00077 };
00078 }
00079
00080 Watchdog::Watchdog(const ACE_Time_Value& interval)
00081 : ReactorInterceptor(TheServiceParticipant->reactor(),
00082 TheServiceParticipant->reactor_owner())
00083 , interval_(interval)
00084 {
00085 }
00086
00087 Watchdog::~Watchdog()
00088 {
00089 }
00090
00091 bool Watchdog::reactor_is_shut_down() const
00092 {
00093 return TheServiceParticipant->is_shut_down();
00094 }
00095
00096 void Watchdog::reset_interval(const ACE_Time_Value& interval)
00097 {
00098 if (this->interval_ != interval) {
00099 this->interval_ = interval;
00100 this->reschedule_deadline();
00101 }
00102 }
00103
00104 long Watchdog::schedule_timer(const void* act, const ACE_Time_Value& interval)
00105 {
00106 long timer_id = -1;
00107 ScheduleCommand c(this, act, interval, &timer_id);
00108 execute_or_enqueue(c);
00109 wait();
00110 return timer_id;
00111 }
00112
00113 int Watchdog::cancel_timer(long timer_id)
00114 {
00115 CancelCommand c(this, timer_id);
00116 execute_or_enqueue(c);
00117 return 1;
00118 }
00119
00120 void Watchdog::cancel_all()
00121 {
00122 CancelCommand c(this, -1);
00123 execute_or_enqueue(c);
00124 }
00125
00126 int Watchdog::reset_timer_interval(long timer_id)
00127 {
00128 ResetCommand c(this, timer_id, interval_);
00129 execute_or_enqueue(c);
00130 return 0;
00131 }
00132
00133 }
00134 }