libassa  3.5.1
Timer.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Timer.h
4 //------------------------------------------------------------------------------
5 // Copyright (c) 1999,2005 by Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 // Create: 09/27/1999
13 //------------------------------------------------------------------------------
14 #ifndef TIMER_H
15 #define TIMER_H
16 
17 #if !defined(WIN32)
18 # include <sys/select.h>
19 #endif
20 
21 #include <sys/time.h>
22 
23 #include "EventHandler.h"
24 #include "assa/TimeVal.h"
25 
26 namespace ASSA {
27 
28 class TimerQueue; // forward declaration
29 
35 class Timer
36 {
37 public:
39  Timer ();
40 
47  Timer (const EventHandler* eh_,
48  const TimeVal& tm_,
49  const TimeVal& delta_,
50  const std::string& name_);
51 
53  Timer (const Timer& t_);
54 
56  ~Timer ();
57 
59  Timer& operator= (const Timer& t_);
60 
62  bool operator< (const Timer& t_) const;
63 
65  bool operator== (const Timer& t_) const;
66 
68  EventHandler* getHandler () const { return m_eh; }
69 
71  const TimeVal& getExpirationTime () const { return m_timer; }
72 
74  const TimeVal& getDeltaTime () const { return m_delta; }
75 
78 
80  void dump (void);
81 
85  void set_id (const std::string& id_) { m_id = id_; }
86 
89  std::string get_id () const { return m_id; }
90 
91 private:
94 
97 
100 
102  std::string m_id;
103 };
104 //------------------------------------------------------------------------------
105 // Timer class inlines
106 //------------------------------------------------------------------------------
107 
108 inline
110 Timer ()
111  : m_eh (NULL), m_id ("<unknown>")
112 {
113  trace("Timer::Timer");
114 }
115 
116 inline
118 Timer (const EventHandler* eh_, const TimeVal& tm_,
119  const TimeVal& delta_, const std::string& name_)
120  : m_eh ((EventHandler*) eh_), m_timer (tm_), m_delta (delta_), m_id (name_)
121 {
122  trace("Timer::Timer(EH*, TV&)");
123 }
124 
125 inline
127 Timer (const Timer& t_)
128  : m_eh (t_.m_eh), m_timer (t_.m_timer),
129  m_delta (t_.m_delta), m_id (t_.m_id)
130 {
131  trace("Timer::Timer(Timer&)");
132 }
133 
134 inline
136 ~Timer ()
137 {
138  trace("Timer::~Timer");
139 }
140 
141 inline Timer&
143 operator=(const Timer& t_)
144 {
145  m_eh = t_.m_eh;
146  m_timer = t_.m_timer;
147  m_delta = t_.m_delta;
148  m_id = t_.m_id;
149 
150  return *this;
151 }
152 
153 inline bool
154 Timer::
155 operator<(const Timer& t_) const
156 {
157  return m_timer < t_.m_timer;
158 }
159 
160 inline bool
162 operator==(const Timer& t_) const
163 {
164  return m_timer == t_.m_timer;
165 }
166 
167 inline void
170 {
172  m_timer = now + m_delta;
173 }
174 
175 inline void
177 dump (void)
178 {
179  DL((REACT,"Timer %s (EH=%s) expires at %s (delta=%s)\n",
180  get_id ().c_str (),
181  m_eh->get_id ().c_str (),
182  m_timer.fmtString ().c_str(),
183  m_delta.fmt_mm_ss_mls ().c_str()));
184 }
185 
190 {
191  bool operator() (const Timer* t1_, const Timer* t2_) const
192  {
193  return (*t1_ < *t2_);
194  }
195 };
196 
197 } // end namespace ASSA
198 
199 #endif /* TIMER_H */
An abstract interface for handling I/O events, timers, and such.
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition: Logger.h:429
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
Class TimeVal is a wrapper around UNIX timeval structure.
EventHandler class.
Definition: EventHandler.h:103
std::string get_id() const
Retrieve EventHandler ID.
Definition: EventHandler.h:157
string fmtString(const char *fmt_=NULL) const
Format timeval structure into readable format.
Definition: TimeVal.cpp:146
string fmt_mm_ss_mls() const
Format timeval structure in readable format MM:SS.MLS.
Definition: TimeVal.cpp:189
static TimeVal gettimeofday()
Shields off underlying OS differences in getting current time.
Definition: TimeVal.cpp:44
TimeVal m_timer
When the timer should be triggered.
Definition: Timer.h:96
EventHandler * m_eh
Pointer to EventHandler.
Definition: Timer.h:93
~Timer()
Destructor: do-nothing.
Definition: Timer.h:136
bool operator<(const Timer &t_) const
Less-that by time.
Definition: Timer.h:155
bool operator==(const Timer &t_) const
Equal by time.
Definition: Timer.h:162
EventHandler * getHandler() const
Get EventHandler pointer.
Definition: Timer.h:68
TimeVal m_delta
Absolute value used when Reactor needs to reschedule the timer.
Definition: Timer.h:99
Timer()
Default constructor.
Definition: Timer.h:110
Timer & operator=(const Timer &t_)
Assignment operator.
Definition: Timer.h:143
void set_id(const std::string &id_)
Set Timer ID.
Definition: Timer.h:85
std::string m_id
Timer's ID.
Definition: Timer.h:102
std::string get_id() const
Retrieve Timer ID.
Definition: Timer.h:89
const TimeVal & getExpirationTime() const
Get Expiration Time.
Definition: Timer.h:71
const TimeVal & getDeltaTime() const
Get Delta time.
Definition: Timer.h:74
void dump(void)
Dump contents to logfile.
Definition: Timer.h:177
void rescheduleExpirationTime()
Reschedule expiration time with Delta value.
Definition: Timer.h:169
Definition: Acceptor.h:40
@ REACT
Class Reactor/PrioriyQueue messages
Definition: LogMask.h:39
TimerCompare class compares two Timers base on their expiration timestamp.
Definition: Timer.h:190
bool operator()(const Timer *t1_, const Timer *t2_) const
Definition: Timer.h:191