2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-03-30 19:56:54 +02:00
|
|
|
time_t Timer::NextCall;
|
2012-04-19 08:46:41 +02:00
|
|
|
Timer::CollectionType Timer::Timers;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the Timer class.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
Timer::Timer(void)
|
|
|
|
{
|
|
|
|
m_Interval = 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Retrieves when the next timer is due.
|
|
|
|
*
|
|
|
|
* @returns Time when the next timer is due.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
time_t Timer::GetNextCall(void)
|
|
|
|
{
|
2012-03-30 19:56:54 +02:00
|
|
|
if (NextCall < time(NULL))
|
2012-03-28 13:24:49 +02:00
|
|
|
Timer::RescheduleTimers();
|
|
|
|
|
2012-03-30 19:56:54 +02:00
|
|
|
return NextCall;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Reschedules all timers, thereby updating the NextCall
|
|
|
|
* timestamp used by the GetNextCall() function.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void Timer::RescheduleTimers(void)
|
|
|
|
{
|
|
|
|
/* Make sure we wake up at least once every 30 seconds */
|
2012-03-30 19:56:54 +02:00
|
|
|
NextCall = time(NULL) + 30;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-19 08:46:41 +02:00
|
|
|
for (Timer::CollectionType::iterator i = Timers.begin(); i != Timers.end(); i++) {
|
2012-04-02 20:50:35 +02:00
|
|
|
Timer::Ptr timer = i->lock();
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
if (timer == NULL)
|
|
|
|
continue;
|
|
|
|
|
2012-03-30 19:56:54 +02:00
|
|
|
if (timer->m_Next < NextCall)
|
|
|
|
NextCall = timer->m_Next;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Calls all expired timers and reschedules them.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void Timer::CallExpiredTimers(void)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
|
2012-04-20 16:21:43 +02:00
|
|
|
Timer::CollectionType::iterator prev, i;
|
|
|
|
for (i = Timers.begin(); i != Timers.end(); ) {
|
|
|
|
Timer::Ptr timer = i->lock();
|
|
|
|
|
|
|
|
prev = i;
|
2012-03-28 13:24:49 +02:00
|
|
|
i++;
|
|
|
|
|
2012-04-20 16:21:43 +02:00
|
|
|
if (!timer) {
|
|
|
|
Timers.erase(prev);
|
2012-03-28 13:24:49 +02:00
|
|
|
continue;
|
2012-04-20 16:21:43 +02:00
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
if (timer->m_Next <= now) {
|
|
|
|
timer->Call();
|
|
|
|
timer->Reschedule(now + timer->GetInterval());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Calls this timer. Note: the timer delegate must not call
|
|
|
|
* Disable() on any other timers than the timer that originally
|
|
|
|
* invoked the delegate.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void Timer::Call(void)
|
|
|
|
{
|
2012-06-16 13:06:06 +02:00
|
|
|
OnTimerExpired(GetSelf());
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Sets the interval for this timer.
|
|
|
|
*
|
|
|
|
* @param interval The new interval.
|
|
|
|
*/
|
2012-06-15 19:32:41 +02:00
|
|
|
void Timer::SetInterval(time_t interval)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
m_Interval = interval;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the interval for this timer.
|
|
|
|
*
|
|
|
|
* @returns The interval.
|
|
|
|
*/
|
2012-06-15 19:32:41 +02:00
|
|
|
time_t Timer::GetInterval(void) const
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return m_Interval;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Registers the timer and starts processing events for it.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void Timer::Start(void)
|
|
|
|
{
|
2012-06-14 11:18:20 +02:00
|
|
|
Stop();
|
|
|
|
|
2012-06-16 13:06:06 +02:00
|
|
|
Timers.push_back(GetSelf());
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
Reschedule(time(NULL) + m_Interval);
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Unregisters the timer and stops processing events for it.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void Timer::Stop(void)
|
|
|
|
{
|
2012-05-21 12:53:38 +02:00
|
|
|
Timers.remove_if(WeakPtrEqual<Timer>(this));
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Reschedules this timer.
|
|
|
|
*
|
|
|
|
* @param next The time when this timer should be called again.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void Timer::Reschedule(time_t next)
|
|
|
|
{
|
|
|
|
m_Next = next;
|
|
|
|
RescheduleTimers();
|
|
|
|
}
|