Watchdog.cpp

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

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1