00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef TIMER_H
00015 #define TIMER_H
00016
00017 #if !defined(WIN32)
00018 # include <sys/select.h>
00019 #endif
00020
00021 #include <sys/time.h>
00022
00023 #include "EventHandler.h"
00024 #include "assa/TimeVal.h"
00025
00026 namespace ASSA {
00027
00028 class TimerQueue;
00029
00035 class Timer
00036 {
00037 public:
00039 Timer ();
00040
00047 Timer (const EventHandler* eh_,
00048 const TimeVal& tm_,
00049 const TimeVal& delta_,
00050 const std::string& name_);
00051
00053 Timer (const Timer& t_);
00054
00056 ~Timer ();
00057
00059 Timer& operator= (const Timer& t_);
00060
00062 bool operator< (const Timer& t_) const;
00063
00065 bool operator== (const Timer& t_) const;
00066
00068 EventHandler* getHandler () const { return m_eh; }
00069
00071 const TimeVal& getExpirationTime () const { return m_timer; }
00072
00074 const TimeVal& getDeltaTime () const { return m_delta; }
00075
00077 void rescheduleExpirationTime ();
00078
00080 void dump (void);
00081
00085 void set_id (const std::string& id_) { m_id = id_; }
00086
00089 std::string get_id () const { return m_id; }
00090
00091 private:
00093 EventHandler* m_eh;
00094
00096 TimeVal m_timer;
00097
00099 TimeVal m_delta;
00100
00102 std::string m_id;
00103 };
00104
00105
00106
00107
00108 inline
00109 Timer::
00110 Timer ()
00111 : m_eh (NULL), m_id ("<unknown>")
00112 {
00113 trace("Timer::Timer");
00114 }
00115
00116 inline
00117 Timer::
00118 Timer (const EventHandler* eh_, const TimeVal& tm_,
00119 const TimeVal& delta_, const std::string& name_)
00120 : m_eh ((EventHandler*) eh_), m_timer (tm_), m_delta (delta_), m_id (name_)
00121 {
00122 trace("Timer::Timer(EH*, TV&)");
00123 }
00124
00125 inline
00126 Timer::
00127 Timer (const Timer& t_)
00128 : m_eh (t_.m_eh), m_timer (t_.m_timer),
00129 m_delta (t_.m_delta), m_id (t_.m_id)
00130 {
00131 trace("Timer::Timer(Timer&)");
00132 }
00133
00134 inline
00135 Timer::
00136 ~Timer ()
00137 {
00138 trace("Timer::~Timer");
00139 }
00140
00141 inline Timer&
00142 Timer::
00143 operator=(const Timer& t_)
00144 {
00145 m_eh = t_.m_eh;
00146 m_timer = t_.m_timer;
00147 m_delta = t_.m_delta;
00148 m_id = t_.m_id;
00149
00150 return *this;
00151 }
00152
00153 inline bool
00154 Timer::
00155 operator<(const Timer& t_) const
00156 {
00157 return m_timer < t_.m_timer;
00158 }
00159
00160 inline bool
00161 Timer::
00162 operator==(const Timer& t_) const
00163 {
00164 return m_timer == t_.m_timer;
00165 }
00166
00167 inline void
00168 Timer::
00169 rescheduleExpirationTime ()
00170 {
00171 TimeVal now (TimeVal::gettimeofday ());
00172 m_timer = now + m_delta;
00173 }
00174
00175 inline void
00176 Timer::
00177 dump (void)
00178 {
00179 DL((REACT,"Timer %s (EH=%s) expires at %s (delta=%s)\n",
00180 get_id ().c_str (),
00181 m_eh->get_id ().c_str (),
00182 m_timer.fmtString ().c_str(),
00183 m_delta.fmt_mm_ss_mls ().c_str()));
00184 }
00185
00189 struct TimerCompare
00190 {
00191 bool operator() (const Timer* t1_, const Timer* t2_) const
00192 {
00193 return (*t1_ < *t2_);
00194 }
00195 };
00196
00197 }
00198
00199 #endif