// -*- c++ -*-
//------------------------------------------------------------------------------
//                             Timer.h
//------------------------------------------------------------------------------
//  Copyright (c) 1999 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

#include <sys/select.h>
#include <sys/time.h>

#include "EventHandler.h"
#include "assa/TimeVal.h"

namespace ASSA {

class TimerQueue;		// forward declaration

/** @file Timer.h 
 *
 * Timer class represents tuple that is stored in TimerQueue.
 */

class Timer 
{
public:
	/// Default constructor
	Timer ();

	/// Contructor that takes EventHandler pointer and time
	Timer (const EventHandler* eh_, const TimeVal& tm_);

	/// Copy constructor
	Timer (const Timer& t_);

	/// Destructor: do-nothing
	~Timer ();

	/// Assignment operator
	Timer& operator= (const Timer& t_);

	/// Less-that by time
	bool operator< (const Timer& t_) const;

	/// Equal by time
	bool operator== (const Timer& t_) const;

	/// Get EventHandler pointer
	EventHandler* getHandler () const { return m_eh; }

	/// Get Expiration Time
	const TimeVal& getExpirationTime () const { return m_timer; }

	/// Dump contents to logfile
	void dump (void);

private:
	/// Pointer to EventHandler
	EventHandler* m_eh;

	/// When this timer should be triggered
	TimeVal m_timer;
};
//------------------------------------------------------------------------------
// Timer class inlines
//------------------------------------------------------------------------------

inline	
Timer::
Timer () 
	: m_eh (NULL) 
{
	trace("Timer::Timer");
}

inline
Timer::
Timer (const EventHandler* eh_, const TimeVal& tm_)
	: m_eh ((EventHandler*) eh_), m_timer (tm_) 
{
	trace("Timer::Timer(EH*, TV&)");
}

inline
Timer::
Timer (const Timer& t_)
	: m_eh (t_.m_eh), m_timer (t_.m_timer) 
{
	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;
	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::
dump (void)
{
	DL((REACT,"EvtHdlr: 0x%x, Expire: %s\n",
	    m_eh, m_timer.fmtString().c_str()));
}

/** 
 * TimerCompare class compares two Timers base on their expiration timestamp.
 */
struct TimerCompare 
{
	bool operator() (const Timer* t1_, const Timer* t2_) const 
		{
			return *t1_ < *t2_;
		}
};

} // end namespace ASSA

#endif /* TIMER_H */  
