2012-03-28 13:24:49 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <algorithm>
|
|
|
|
#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
|
|
|
|
|
|
|
Timer::Timer(void)
|
|
|
|
{
|
|
|
|
m_Interval = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: the timer delegate must not call Disable() on any other timers than
|
|
|
|
* the timer that originally invoked the delegate */
|
|
|
|
void Timer::Call(void)
|
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
TimerEventArgs tea;
|
|
|
|
tea.Source = shared_from_this();
|
|
|
|
tea.UserArgs = m_UserArgs;
|
|
|
|
OnTimerExpired(tea);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::SetInterval(unsigned int interval)
|
|
|
|
{
|
|
|
|
m_Interval = interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Timer::GetInterval(void) const
|
|
|
|
{
|
|
|
|
return m_Interval;
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
void Timer::SetUserArgs(const EventArgs& userArgs)
|
2012-04-04 12:22:46 +02:00
|
|
|
{
|
|
|
|
m_UserArgs = userArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
EventArgs Timer::GetUserArgs(void) const
|
2012-04-04 12:22:46 +02:00
|
|
|
{
|
|
|
|
return m_UserArgs;
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
void Timer::Start(void)
|
|
|
|
{
|
2012-04-23 09:48:20 +02:00
|
|
|
Timers.push_back(static_pointer_cast<Timer>(shared_from_this()));
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
Reschedule(time(NULL) + m_Interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::Stop(void)
|
|
|
|
{
|
2012-04-23 09:48:20 +02:00
|
|
|
Timers.remove_if(weak_ptr_eq_raw<Timer>(this));
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::Reschedule(time_t next)
|
|
|
|
{
|
|
|
|
m_Next = next;
|
|
|
|
RescheduleTimers();
|
|
|
|
}
|