Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Timer.h
//------------------------------------------------------------------------------
// Copyright (c) 1999,2005 by Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
// Create: 09/27/1999
//------------------------------------------------------------------------------
#ifndef TIMER_H
#define TIMER_H
#if !defined(WIN32)
# include <sys/select.h>
#endif
#include <sys/time.h>
#include "EventHandler.h"
#include "assa/TimeVal.h"
namespace ASSA {
class TimerQueue; // forward declaration
class Timer
{
public:
Timer ();
Timer (const EventHandler* eh_,
const TimeVal& tm_,
const TimeVal& delta_,
const std::string& name_);
Timer (const Timer& t_);
~Timer ();
Timer& operator= (const Timer& t_);
bool operator< (const Timer& t_) const;
bool operator== (const Timer& t_) const;
EventHandler* getHandler () const { return m_eh; }
const TimeVal& getExpirationTime () const { return m_timer; }
const TimeVal& getDeltaTime () const { return m_delta; }
void rescheduleExpirationTime ();
void dump (void);
void set_id (const std::string& id_) { m_id = id_; }
std::string get_id () const { return m_id; }
private:
EventHandler* m_eh;
TimeVal m_timer;
TimeVal m_delta;
std::string m_id;
};
//------------------------------------------------------------------------------
// Timer class inlines
//------------------------------------------------------------------------------
inline
Timer::
Timer ()
: m_eh (NULL), m_id ("<unknown>")
{
trace("Timer::Timer");
}
inline
Timer::
Timer (const EventHandler* eh_, const TimeVal& tm_,
const TimeVal& delta_, const std::string& name_)
: m_eh ((EventHandler*) eh_), m_timer (tm_), m_delta (delta_), m_id (name_)
{
trace("Timer::Timer(EH*, TV&)");
}
inline
Timer::
Timer (const Timer& t_)
: m_eh (t_.m_eh), m_timer (t_.m_timer),
m_delta (t_.m_delta), m_id (t_.m_id)
{
trace("Timer::Timer(Timer&)");
}
inline
Timer::
~Timer ()
{
trace("Timer::~Timer");
}
inline Timer&
Timer::
operator=(const Timer& t_)
{
m_eh = t_.m_eh;
m_timer = t_.m_timer;
m_delta = t_.m_delta;
m_id = t_.m_id;
return *this;
}
inline bool
Timer::
operator<(const Timer& t_) const
{
return m_timer < t_.m_timer;
}
inline bool
Timer::
operator==(const Timer& t_) const
{
return m_timer == t_.m_timer;
}
inline void
Timer::
rescheduleExpirationTime ()
{
TimeVal now (TimeVal::gettimeofday ());
m_timer = now + m_delta;
}
inline void
Timer::
dump (void)
{
DL((REACT,"Timer %s (EH=%s) expires at %s (delta=%s)\n",
get_id ().c_str (),
m_eh->get_id ().c_str (),
m_timer.fmtString ().c_str(),
m_delta.fmt_mm_ss_mls ().c_str()));
}
struct TimerCompare
{
bool operator() (const Timer* t1_, const Timer* t2_) const
{
return (*t1_ < *t2_);
}
};
} // end namespace ASSA
#endif /* TIMER_H */
EventHandler.h
An abstract interface for handling I/O events, timers, and such.
trace
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition Logger.h:429
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
TimeVal.h
Class TimeVal is a wrapper around UNIX timeval structure.
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::EventHandler
EventHandler class.
Definition EventHandler.h:103
ASSA::EventHandler::get_id
std::string get_id() const
Retrieve EventHandler ID.
Definition EventHandler.h:157
ASSA::TimeVal
Definition TimeVal.h:31
ASSA::TimeVal::fmtString
string fmtString(const char *fmt_=NULL) const
Format timeval structure into readable format.
Definition TimeVal.cpp:146
ASSA::TimeVal::fmt_mm_ss_mls
string fmt_mm_ss_mls() const
Format timeval structure in readable format MM:SS.MLS.
Definition TimeVal.cpp:189
ASSA::TimeVal::gettimeofday
static TimeVal gettimeofday()
Shields off underlying OS differences in getting current time.
Definition TimeVal.cpp:44
ASSA::Timer
Definition Timer.h:36
ASSA::Timer::m_timer
TimeVal m_timer
When the timer should be triggered.
Definition Timer.h:96
ASSA::Timer::m_eh
EventHandler * m_eh
Pointer to EventHandler.
Definition Timer.h:93
ASSA::Timer::getDeltaTime
const TimeVal & getDeltaTime() const
Get Delta time.
Definition Timer.h:74
ASSA::Timer::~Timer
~Timer()
Destructor: do-nothing.
Definition Timer.h:136
ASSA::Timer::operator<
bool operator<(const Timer &t_) const
Less-that by time.
Definition Timer.h:155
ASSA::Timer::operator==
bool operator==(const Timer &t_) const
Equal by time.
Definition Timer.h:162
ASSA::Timer::m_delta
TimeVal m_delta
Absolute value used when Reactor needs to reschedule the timer.
Definition Timer.h:99
ASSA::Timer::Timer
Timer()
Default constructor.
Definition Timer.h:110
ASSA::Timer::operator=
Timer & operator=(const Timer &t_)
Assignment operator.
Definition Timer.h:143
ASSA::Timer::set_id
void set_id(const std::string &id_)
Set Timer ID.
Definition Timer.h:85
ASSA::Timer::m_id
std::string m_id
Timer's ID.
Definition Timer.h:102
ASSA::Timer::get_id
std::string get_id() const
Retrieve Timer ID.
Definition Timer.h:89
ASSA::Timer::getExpirationTime
const TimeVal & getExpirationTime() const
Get Expiration Time.
Definition Timer.h:71
ASSA::Timer::dump
void dump(void)
Dump contents to logfile.
Definition Timer.h:177
ASSA::Timer::rescheduleExpirationTime
void rescheduleExpirationTime()
Reschedule expiration time with Delta value.
Definition Timer.h:169
ASSA::Timer::getHandler
EventHandler * getHandler() const
Get EventHandler pointer.
Definition Timer.h:68
ASSA
Definition Acceptor.h:40
ASSA::REACT
@ REACT
Class Reactor/PrioriyQueue messages
Definition LogMask.h:39
ASSA::TimerCompare
TimerCompare class compares two Timers base on their expiration timestamp.
Definition Timer.h:190
ASSA::TimerCompare::operator()
bool operator()(const Timer *t1_, const Timer *t2_) const
Definition Timer.h:191