2009-05-04 21:11:29 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2009 by Lothar May *
|
|
|
|
|
* *
|
|
|
|
|
* 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, Inc., *
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <core/timermanager.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
2009-06-01 19:49:16 +00:00
|
|
|
TimerManager::TimerManager(boost::shared_ptr<boost::asio::io_service> ioService)
|
|
|
|
|
: m_ioService(ioService), m_curTimerId(0)
|
2009-05-04 21:11:29 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned
|
2009-06-01 19:49:16 +00:00
|
|
|
TimerManager::RegisterTimer(unsigned timeoutMsec, boost::function<void()> timerHandler, bool autoRestart)
|
2009-05-04 21:11:29 +00:00
|
|
|
{
|
|
|
|
|
// Register a new timer callback.
|
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_timerMutex);
|
|
|
|
|
// Use a unique id for each timer.
|
|
|
|
|
unsigned id = GetNextTimerId();
|
2009-06-07 23:25:13 +00:00
|
|
|
boost::shared_ptr<TimerData> data(new TimerData);
|
|
|
|
|
data->timer.reset(
|
2009-06-01 19:49:16 +00:00
|
|
|
new boost::asio::deadline_timer(*m_ioService, boost::posix_time::milliseconds(timeoutMsec)));
|
2009-06-07 23:25:13 +00:00
|
|
|
data->userHandler = timerHandler;
|
|
|
|
|
data->durationMsec = timeoutMsec;
|
|
|
|
|
data->autoRestart = autoRestart;
|
|
|
|
|
data->cancelled = false;
|
2009-06-08 21:53:14 +00:00
|
|
|
data->timer->async_wait(boost::bind(&TimerManager::Handler, this, boost::asio::placeholders::error, data));
|
2009-06-01 19:49:16 +00:00
|
|
|
m_timerMap.insert(TimerMap::value_type(id, data));
|
|
|
|
|
|
2009-05-04 21:11:29 +00:00
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-01 19:49:16 +00:00
|
|
|
bool
|
2009-06-07 23:25:13 +00:00
|
|
|
TimerManager::AddTimer(unsigned timerId, unsigned timeoutMsec, boost::function<void()> timerHandler)
|
2009-06-01 19:49:16 +00:00
|
|
|
{
|
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_timerMutex);
|
|
|
|
|
bool restarted = false;
|
|
|
|
|
TimerMap::iterator pos = m_timerMap.find(timerId);
|
|
|
|
|
if (pos != m_timerMap.end())
|
|
|
|
|
{
|
2009-06-07 23:25:13 +00:00
|
|
|
pos->second->userHandler = timerHandler;
|
2009-06-08 21:53:14 +00:00
|
|
|
pos->second->timer.reset(
|
|
|
|
|
new boost::asio::deadline_timer(*m_ioService, boost::posix_time::milliseconds(timeoutMsec)));
|
|
|
|
|
pos->second->timer->async_wait(boost::bind(&TimerManager::Handler, this, boost::asio::placeholders::error, pos->second));
|
2009-06-01 19:49:16 +00:00
|
|
|
restarted = true;
|
|
|
|
|
}
|
|
|
|
|
return restarted;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-04 21:11:29 +00:00
|
|
|
bool
|
|
|
|
|
TimerManager::UnregisterTimer(unsigned timerId)
|
|
|
|
|
{
|
|
|
|
|
// Remove a timer callback from the map.
|
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_timerMutex);
|
|
|
|
|
bool unregistered = false;
|
2009-06-01 19:49:16 +00:00
|
|
|
TimerMap::iterator pos = m_timerMap.find(timerId);
|
|
|
|
|
if (pos != m_timerMap.end())
|
2009-05-04 21:11:29 +00:00
|
|
|
{
|
2009-06-07 23:25:13 +00:00
|
|
|
pos->second->cancelled = true;
|
2009-06-01 19:49:16 +00:00
|
|
|
m_timerMap.erase(pos);
|
|
|
|
|
unregistered = true;
|
2009-05-04 21:11:29 +00:00
|
|
|
}
|
|
|
|
|
return unregistered;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-07 23:25:13 +00:00
|
|
|
TimerManager::Handler(const boost::system::error_code &ec, boost::shared_ptr<TimerManager::TimerData> data)
|
2009-06-01 19:49:16 +00:00
|
|
|
{
|
2009-06-08 21:53:14 +00:00
|
|
|
boost::recursive_mutex::scoped_lock lock(m_timerMutex);
|
2009-06-07 23:25:13 +00:00
|
|
|
if (!ec && data && !data->cancelled)
|
2009-06-01 19:49:16 +00:00
|
|
|
{
|
2009-06-07 23:25:13 +00:00
|
|
|
data->userHandler();
|
|
|
|
|
if (data->autoRestart)
|
2009-06-01 19:49:16 +00:00
|
|
|
{
|
2009-06-08 21:53:14 +00:00
|
|
|
data->timer.reset(
|
|
|
|
|
new boost::asio::deadline_timer(*m_ioService, boost::posix_time::milliseconds(data->durationMsec)));
|
|
|
|
|
data->timer->async_wait(boost::bind(&TimerManager::Handler, this, boost::asio::placeholders::error, data));
|
2009-06-01 19:49:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*void
|
2009-05-04 21:11:29 +00:00
|
|
|
TimerManager::Process()
|
|
|
|
|
{
|
|
|
|
|
boost::recursive_mutex::scoped_lock lock(m_timerMutex);
|
|
|
|
|
bool timerOccurred;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
timerOccurred = false;
|
|
|
|
|
TimerMap::iterator i = m_timerMap.begin();
|
|
|
|
|
if (i != m_timerMap.end())
|
|
|
|
|
{
|
|
|
|
|
// Check whether the timer occured.
|
|
|
|
|
unsigned currentTicks = static_cast<unsigned>(m_softwareTimer.elapsed().total_milliseconds());
|
|
|
|
|
unsigned timerTicks = i->first;
|
|
|
|
|
if (currentTicks >= timerTicks)
|
|
|
|
|
{
|
|
|
|
|
// Grab a copy of the timer.
|
|
|
|
|
TimerData timer = i->second;
|
|
|
|
|
// Remove the timer, re-add if it is repeating.
|
|
|
|
|
m_timerMap.erase(i);
|
|
|
|
|
if (timer.repeat)
|
|
|
|
|
{
|
|
|
|
|
// Re-add the repeating timer.
|
|
|
|
|
// Try to be precise.
|
|
|
|
|
unsigned absoluteTimer = currentTicks + timer.msec;
|
|
|
|
|
unsigned tickDiff = currentTicks - timerTicks;
|
|
|
|
|
if (absoluteTimer >= tickDiff)
|
|
|
|
|
absoluteTimer -= tickDiff;
|
|
|
|
|
m_timerMap.insert(
|
|
|
|
|
TimerMap::value_type(absoluteTimer, timer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The callback may register/unregister a timer (a recursive mutex is used).
|
|
|
|
|
timer.handler();
|
|
|
|
|
timerOccurred = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (timerOccurred);
|
2009-06-01 19:49:16 +00:00
|
|
|
}*/
|
2009-05-04 21:11:29 +00:00
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
|
TimerManager::GetNextTimerId()
|
|
|
|
|
{
|
2009-05-31 13:17:02 +00:00
|
|
|
++m_curTimerId;
|
|
|
|
|
if (m_curTimerId == 0) // Id 0 is invalid.
|
|
|
|
|
++m_curTimerId;
|
|
|
|
|
return m_curTimerId;
|
2009-05-04 21:11:29 +00:00
|
|
|
}
|
|
|
|
|
|