15 #include <boost/scoped_ptr.hpp>
33 asiolink::IntervalTimer interval_timer_;
53 TimerInfo(asiolink::IOService& io_service,
57 : interval_timer_(io_service),
58 user_callback_(user_callback),
60 scheduling_mode_(mode) { };
97 void registerTimer(
const std::string& timer_name,
111 void unregisterTimer(
const std::string& timer_name);
117 void unregisterTimers();
124 bool isTimerRegistered(
const std::string& timer_name);
127 size_t timersCount()
const;
141 void setup(
const std::string& timer_name);
148 void cancel(
const std::string& timer_name);
166 void registerTimerInternal(
const std::string& timer_name,
180 void unregisterTimerInternal(
const std::string& timer_name);
186 void unregisterTimersInternal();
200 void setupInternal(
const std::string& timer_name);
207 void cancelInternal(
const std::string& timer_name);
213 void timerCallback(
const std::string& timer_name);
220 TimerInfoMap registered_timers_;
223 boost::scoped_ptr<std::mutex> mutex_;
226 TimerMgrImpl::TimerMgrImpl() : io_service_(new
IOService()),
227 registered_timers_(), mutex_(new
std::mutex) {
236 io_service_ = io_service;
244 if (MultiThreadingMgr::instance().getMode()) {
245 std::lock_guard<std::mutex> lock(*mutex_);
246 registerTimerInternal(timer_name, callback, interval, scheduling_mode);
248 registerTimerInternal(timer_name, callback, interval, scheduling_mode);
253 TimerMgrImpl::registerTimerInternal(
const std::string& timer_name,
258 if (timer_name.empty()) {
263 if (registered_timers_.find(timer_name) != registered_timers_.end()) {
265 << timer_name <<
"'");
271 TimerInfoPtr timer_info(
new TimerInfo(*io_service_, callback,
272 interval, scheduling_mode));
275 registered_timers_.insert(std::pair<std::string, TimerInfoPtr>(timer_name,
281 if (MultiThreadingMgr::instance().getMode()) {
282 std::lock_guard<std::mutex> lock(*mutex_);
283 unregisterTimerInternal(timer_name);
285 unregisterTimerInternal(timer_name);
290 TimerMgrImpl::unregisterTimerInternal(
const std::string& timer_name) {
292 TimerInfoMap::iterator timer_info_it = registered_timers_.find(timer_name);
295 if (timer_info_it == registered_timers_.end()) {
297 << timer_name <<
"'");
301 cancelInternal(timer_name);
304 registered_timers_.erase(timer_info_it);
309 if (MultiThreadingMgr::instance().getMode()) {
310 std::lock_guard<std::mutex> lock(*mutex_);
311 unregisterTimersInternal();
313 unregisterTimersInternal();
318 TimerMgrImpl::unregisterTimersInternal() {
328 TimerInfoMap registered_timers_copy(registered_timers_);
331 for (TimerInfoMap::iterator timer_info_it = registered_timers_copy.begin();
332 timer_info_it != registered_timers_copy.end(); ++timer_info_it) {
333 unregisterTimerInternal(timer_info_it->first);
339 if (MultiThreadingMgr::instance().getMode()) {
340 std::lock_guard<std::mutex> lock(*mutex_);
341 return (registered_timers_.find(timer_name) != registered_timers_.end());
343 return (registered_timers_.find(timer_name) != registered_timers_.end());
349 if (MultiThreadingMgr::instance().getMode()) {
350 std::lock_guard<std::mutex> lock(*mutex_);
351 return (registered_timers_.size());
353 return (registered_timers_.size());
359 if (MultiThreadingMgr::instance().getMode()) {
360 std::lock_guard<std::mutex> lock(*mutex_);
361 setupInternal(timer_name);
363 setupInternal(timer_name);
368 TimerMgrImpl::setupInternal(
const std::string& timer_name) {
370 TimerInfoMap::const_iterator timer_info_it = registered_timers_.find(timer_name);
371 if (timer_info_it == registered_timers_.end()) {
373 "no such timer registered");
378 const TimerInfoPtr& timer_info = timer_info_it->second;
381 timer_info->interval_timer_.setup(cb, timer_info->interval_,
382 timer_info->scheduling_mode_);
387 if (MultiThreadingMgr::instance().getMode()) {
388 std::lock_guard<std::mutex> lock(*mutex_);
389 cancelInternal(timer_name);
391 cancelInternal(timer_name);
396 TimerMgrImpl::cancelInternal(
const std::string& timer_name) {
398 TimerInfoMap::const_iterator timer_info_it = registered_timers_.find(timer_name);
399 if (timer_info_it == registered_timers_.end()) {
401 "no such timer registered");
404 timer_info_it->second->interval_timer_.cancel();
408 TimerMgrImpl::timerCallback(
const std::string& timer_name) {
410 TimerInfoMap::iterator timer_info_it = registered_timers_.find(timer_name);
411 if (timer_info_it != registered_timers_.end()) {
418 .arg(timer_info_it->first);
420 std::string error_string;
422 timer_info_it->second->user_callback_();
424 }
catch (
const std::exception& ex){
425 error_string = ex.what();
428 error_string =
"unknown reason";
432 if (!error_string.empty()) {
434 .arg(timer_info_it->first)
451 impl_->unregisterTimers();
465 impl_->registerTimer(timer_name, callback, interval, scheduling_mode);
475 impl_->unregisterTimer(timer_name);
484 impl_->unregisterTimers();
489 return (impl_->isTimerRegistered(timer_name));
494 return (impl_->timersCount());
504 impl_->setup(timer_name);
514 impl_->cancel(timer_name);
519 impl_->setIOService(io_service);
void unregisterTimers()
Unregisters all timers.
boost::shared_ptr< TimerMgr > TimerMgrPtr
Type definition of the shared pointer to TimerMgr.
const isc::log::MessageID DHCPSRV_TIMERMGR_RUN_TIMER_OPERATION
const isc::log::MessageID DHCPSRV_TIMERMGR_UNREGISTER_ALL_TIMERS
const isc::log::MessageID DHCPSRV_TIMERMGR_CALLBACK_FAILED
Implementation of the TimerMgr.
bool isTimerRegistered(const std::string &timer_name)
Checks if the timer with a specified name has been registered.
void registerTimer(const std::string &timer_name, const asiolink::IntervalTimer::Callback &callback, const long interval, const asiolink::IntervalTimer::Mode &scheduling_mode)
Registers new timer in the TimerMgr.
size_t timersCount() const
Returns the number of registered timers.
void cancel(const std::string &timer_name)
Cancels the execution of the interval timer.
void setIOService(const asiolink::IOServicePtr &io_service)
Sets IO service to be used by the Timer Manager.
Manages a pool of asynchronous interval timers.
void cancel(const std::string &timer_name)
Cancels the execution of the interval timer.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
bool isTimerRegistered(const std::string &timer_name)
Checks if the timer with a specified name has been registered.
std::function< void()> Callback
The IOService class is a wrapper for the ASIO io_service class.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void unregisterTimers()
Unregisters all timers.
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
Mode
Defines possible timer modes used to setup a timer.
boost::shared_ptr< TimerInfo > TimerInfoPtr
A type definition for the pointer to TimerInfo structure.
const isc::log::MessageID DHCPSRV_TIMERMGR_UNREGISTER_TIMER
void setup(const std::string &timer_name)
Schedules the execution of the interval timer.
const isc::log::MessageID DHCPSRV_TIMERMGR_STOP_TIMER
void setIOService(const IOServicePtr &io_service)
Sets IO service to be used by the Timer Manager.
Defines the logger used by the top-level component of kea-dhcp-ddns.
const isc::log::MessageID DHCPSRV_TIMERMGR_REGISTER_TIMER
void unregisterTimer(const std::string &timer_name)
Unregisters specified timer.
void setup(const std::string &timer_name)
Schedules the execution of the interval timer.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
void registerTimer(const std::string &timer_name, const asiolink::IntervalTimer::Callback &callback, const long interval, const asiolink::IntervalTimer::Mode &scheduling_mode)
Registers new timer in the TimerMgr.
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
size_t timersCount() const
Returns the number of registered timers.
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
const isc::log::MessageID DHCPSRV_TIMERMGR_START_TIMER
std::map< std::string, TimerInfoPtr > TimerInfoMap
A type definition for the map holding timers configuration.
void unregisterTimer(const std::string &timer_name)
Unregisters specified timer.