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-07-13 15:03:24 +02:00
|
|
|
Timer::CollectionType Timer::m_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)
|
2012-08-03 15:35:27 +02:00
|
|
|
: m_Interval(0)
|
|
|
|
{ }
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
2012-06-22 11:19:58 +02:00
|
|
|
* Calls expired timers and returned when the next wake-up should happen.
|
2012-05-08 15:14:20 +02:00
|
|
|
*
|
|
|
|
* @returns Time when the next timer is due.
|
|
|
|
*/
|
2012-07-25 12:59:17 +02:00
|
|
|
double Timer::ProcessTimers(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-09-25 15:24:14 +02:00
|
|
|
double wakeup = 30; /* wake up at least once after this many seconds */
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
double st = Utility::GetTime();
|
2012-06-22 23:19:10 +02:00
|
|
|
|
2012-04-20 16:21:43 +02:00
|
|
|
Timer::CollectionType::iterator prev, i;
|
2012-07-13 15:03:24 +02:00
|
|
|
for (i = m_Timers.begin(); i != m_Timers.end(); ) {
|
2012-04-20 16:21:43 +02:00
|
|
|
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) {
|
2012-07-13 15:03:24 +02:00
|
|
|
m_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
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
double now = Utility::GetTime();
|
2012-06-22 11:19:58 +02:00
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
if (timer->m_Next <= now) {
|
|
|
|
timer->Call();
|
2012-06-22 11:19:58 +02:00
|
|
|
|
|
|
|
/* time may have changed depending on how long the
|
|
|
|
* timer call took - we need to fetch the current time */
|
2012-07-25 12:59:17 +02:00
|
|
|
now = Utility::GetTime();
|
2012-06-22 11:19:58 +02:00
|
|
|
|
2013-02-08 10:14:24 +01:00
|
|
|
double next = now + timer->GetInterval();
|
|
|
|
|
2013-02-08 10:39:54 +01:00
|
|
|
if (timer->m_Next <= now || next < timer->m_Next)
|
2013-02-08 10:14:24 +01:00
|
|
|
timer->Reschedule(next);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
2012-06-22 11:19:58 +02:00
|
|
|
|
|
|
|
assert(timer->m_Next > now);
|
|
|
|
|
|
|
|
if (timer->m_Next - now < wakeup)
|
|
|
|
wakeup = timer->m_Next - now;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
2012-06-22 11:19:58 +02:00
|
|
|
|
|
|
|
assert(wakeup > 0);
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
double et = Utility::GetTime();
|
2012-06-22 23:19:10 +02:00
|
|
|
|
2013-02-09 10:55:54 +01:00
|
|
|
if (et - st > 0.01) {
|
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "Timers took " << et - st << " seconds";
|
|
|
|
Logger::Write(LogDebug, "base", msgbuf.str());
|
|
|
|
}
|
2012-06-22 23:19:10 +02:00
|
|
|
|
2012-06-22 11:19:58 +02:00
|
|
|
return wakeup;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
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-07-25 12:59:17 +02:00
|
|
|
double st = Utility::GetTime();
|
2012-06-18 17:23:48 +02:00
|
|
|
|
2012-06-16 13:06:06 +02:00
|
|
|
OnTimerExpired(GetSelf());
|
2012-06-18 17:23:48 +02:00
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
double et = Utility::GetTime();
|
2012-06-18 17:23:48 +02:00
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
if (et - st > 1.0) {
|
2012-06-18 17:23:48 +02:00
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "Timer call took " << et - st << " seconds.";
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogWarning, "base", msgbuf.str());
|
2012-06-18 17:23:48 +02:00
|
|
|
}
|
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-07-25 12:59:17 +02:00
|
|
|
void Timer::SetInterval(double 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-07-25 12:59:17 +02:00
|
|
|
double 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-07-10 13:00:53 +02:00
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-06-14 11:18:20 +02:00
|
|
|
Stop();
|
|
|
|
|
2012-07-13 15:03:24 +02:00
|
|
|
m_Timers.push_back(GetSelf());
|
2012-06-18 01:58:13 +02:00
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
Reschedule(Utility::GetTime() + m_Interval);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
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-07-10 13:00:53 +02:00
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-07-13 15:03:24 +02:00
|
|
|
m_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-07-25 12:59:17 +02:00
|
|
|
void Timer::Reschedule(double next)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
m_Next = next;
|
|
|
|
}
|
2012-09-25 14:03:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjusts all timers by adding the specified amount of time to their
|
|
|
|
* next scheduled timestamp.
|
|
|
|
*
|
|
|
|
* @param adjustment The adjustment.
|
|
|
|
*/
|
|
|
|
void Timer::AdjustTimers(double adjustment)
|
|
|
|
{
|
2012-09-25 15:24:14 +02:00
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
|
|
|
Timer::CollectionType::iterator i;
|
|
|
|
for (i = m_Timers.begin(); i != m_Timers.end(); i++) {
|
|
|
|
Timer::Ptr timer = i->lock();
|
|
|
|
|
|
|
|
if (abs(now - (timer->m_Next + adjustment)) <
|
|
|
|
abs(now - timer->m_Next))
|
|
|
|
timer->m_Next += adjustment;
|
2012-09-25 14:03:41 +02:00
|
|
|
}
|
|
|
|
}
|