1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright (C) 2015-2020 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef TIMER_MGR_H
#define TIMER_MGR_H
#include <asiolink/interval_timer.h>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
namespace isc {
namespace dhcp {
/// @brief Forward declaration of the @c TimerMgr implementation.
class TimerMgrImpl;
typedef boost::shared_ptr<TimerMgrImpl> TimerMgrImplPtr;
/// @brief Forward declaration of the @c TimerMgr.
class TimerMgr;
/// @brief Type definition of the shared pointer to @c TimerMgr.
typedef boost::shared_ptr<TimerMgr> TimerMgrPtr;
/// @brief Manages a pool of asynchronous interval timers.
///
/// This class holds a pool of asynchronous interval timers.
///
/// This class is useful for performing periodic actions at the specified
/// intervals, e.g. act upon expired leases (leases reclamation) or
/// return declined leases back to the address pool. Other applications
/// may be added in the future.
///
/// The @c TimerMgr is a singleton, thus its instance is available from
/// different places in the server code. This is convenient because timers
/// can be installed by different configuration parsers or they can be
/// re-scheduled from the callback functions.
///
/// The timer is registered using the @c TimerMgr::registerTimer method.
/// Each registered timer has a unique name. It is not possible to register
/// multiple timers with the same name. Each registered timer is associated
/// with the callback function supplied by the caller. This callback function
/// performs the tasks to be executed periodically according to the timer's
/// interval.
///
/// The registered timer's interval does not begin to elapse until the
/// @c TimerMgr::setup method is called for it.
///
/// Before the @c TimerMgr can be used the server process must call
/// @c TimerMgr::setIOService to associate the manager with the IO service
/// that the server is using to its run tasks.
///
/// @note Only scheduling new timer (calling @ref setup) and canceling existing
/// timer (calling @ref cancel) are thread safe.
/// Registering new timers (calling @ref registerTimer) and unregistering
/// existing timers (calling @ref unregisterTimer) must be handled before
/// starting processing threads.
class TimerMgr : public boost::noncopyable {
public:
/// @brief Returns pointer to the sole instance of the @c TimerMgr.
static const TimerMgrPtr& instance();
/// @brief Destructor.
///
/// Stops the worker thread if it is running and unregisters any
/// registered timers.
~TimerMgr();
/// @brief Sets IO service to be used by the Timer Manager.
///
/// @param io_service Pointer to the new IO service.
void setIOService(const asiolink::IOServicePtr& io_service);
/// @name Registering, unregistering and scheduling the timers.
//@{
/// @brief Registers new timer in the @c TimerMgr.
///
/// @param timer_name Unique name for the timer.
/// @param callback Pointer to the callback function to be invoked
/// when the timer elapses, e.g. function processing expired leases
/// in the DHCP server.
/// @param interval Timer interval in milliseconds.
/// @param scheduling_mode Scheduling mode of the timer as described in
/// @c asiolink::IntervalTimer::Mode.
///
/// @throw BadValue if the timer name is invalid or duplicate.
void registerTimer(const std::string& timer_name,
const asiolink::IntervalTimer::Callback& callback,
const long interval,
const asiolink::IntervalTimer::Mode& scheduling_mode);
/// @brief Unregisters specified timer.
///
/// This method cancels the timer if it is setup and removes it from the
/// internal collection of timers.
///
/// @param timer_name Name of the timer to be unregistered.
///
/// @throw BadValue if the specified timer hasn't been registered.
void unregisterTimer(const std::string& timer_name);
/// @brief Unregisters all timers.
void unregisterTimers();
/// @brief Checks if the timer with a specified name has been registered.
///
/// @param timer_name Name of the timer.
/// @return true if the timer with the specified name has been registered,
/// false otherwise.
bool isTimerRegistered(const std::string& timer_name);
/// @brief Returns the number of registered timers.
size_t timersCount() const;
/// @brief Schedules the execution of the interval timer.
///
/// This method schedules the timer, i.e. the callback will be executed
/// after specified interval elapses. The interval has been specified
/// during timer registration. Depending on the mode selected during the
/// timer registration, the callback will be executed once after it has
/// been scheduled or until it is cancelled. Though, in the former case
/// the timer can be re-scheduled in the callback function.
///
/// @param timer_name Unique timer name.
///
/// @throw BadValue if the timer hasn't been registered.
void setup(const std::string& timer_name);
/// @brief Cancels the execution of the interval timer.
///
/// This method has no effect if the timer hasn't been scheduled with
/// the @c TimerMgr::setup method.
///
/// @param timer_name Unique timer name.
///
/// @throw BadValue if the timer hasn't been registered.
void cancel(const std::string& timer_name);
//@}
private:
/// @brief Private default constructor.
///
/// The @c TimerMgr is a singleton class which instance must be created
/// using the @c TimerMgr::instance method. Private constructor enforces
/// construction via @c TimerMgr::instance.
TimerMgr();
/// @brief The @c TimerMgr implementation.
TimerMgrImplPtr impl_;
};
} // end of namespace isc::dhcp
} // end of namespace isc
#endif // TIMER_MGR_H
|