| Kea 3.0.2-git

isc::asiolink::IntervalTimer Class Reference

The `IntervalTimer` class is a wrapper for the ASIO `boost::asio::deadline_timer` class. More...

`#include <interval_timer.h>`

## Public Types
The type of timer callback function
enum | Mode { REPEATING , ONE_SHOT }
| Defines possible timer modes used to setup a timer. More...
typedef std::function< void()> | Callback

## Constructors and Destructor
Note: The copy constructor and the assignment operator are intentionally defined as private, making this class non-copyable.
| IntervalTimer (const IOServicePtr &io_service)
| The constructor with `IOService`.
| ~IntervalTimer ()
| The destructor.
void | setup (const Callback &cbfunc, const long interval, const Mode &mode=REPEATING)
| Register timer callback function and interval.
void | cancel ()
| Cancel the timer.
long | getInterval () const
| Return the timer interval.

## Detailed Description

The `IntervalTimer` class is a wrapper for the ASIO `boost::asio::deadline_timer` class.

This class is implemented to use `boost::asio::deadline_timer` as interval timer.

`setup()` sets a timer to expire on (now + interval), a call back function, and an interval mode.

`IntervalTimerImpl::callback()` is called by the timer when it expires.

The function calls the call back function set by `setup()` and if the the interval mode indicates a repeating interval, will reschedule the timer to expire in (now + interval) milliseconds.

The type of call back function is `void(void)`.

The call back function will not be called if the instance of this class is destroyed before the timer is expired.

Sample code:

void function_to_call_back() {
// this function will be called periodically
}
int interval_in_milliseconds = 1000;
IOService io_service;
IntervalTimer intervalTimer(io_service);
intervalTimer.setup(function_to_call_back, interval_in_milliseconds);
io_service.run();
isc::asiolink::IOService
The IOService class is a wrapper for the ASIO io_context class.
Definition io_service.h:32
isc::asiolink::IOService::run
void run()
Start the underlying event loop.
Definition io_service.cc:133
isc::asiolink::IntervalTimer
The IntervalTimer class is a wrapper for the ASIO boost::asio::deadline_timer class.
Definition interval_timer.h:52

Definition at line 52 of file interval_timer.h.

## Member Typedef Documentation

## ◆ Callback

typedef std::function<void()> isc::asiolink::IntervalTimer::Callback

Definition at line 55 of file interval_timer.h.

## Member Enumeration Documentation

## ◆ Mode

enum isc::asiolink::IntervalTimer::Mode

Defines possible timer modes used to setup a timer.

- REPEATING - Timer will reschedule itself after each expiration
- ONE_SHOT - Timer will expire after one interval and not reschedule.

Enumerator
REPEATING |
ONE_SHOT |

Definition at line 60 of file interval_timer.h.

## Constructor & Destructor Documentation

## ◆ IntervalTimer()

isc::asiolink::IntervalTimer::IntervalTimer | ( | const IOServicePtr & | io_service | ) |

The constructor with `IOService`.

This constructor may throw a standard exception if memory allocation fails inside the method. This constructor may also throw `boost::system::system_error`.

Parameters

  io_service | A smart pointer to an instance of IOService

Definition at line 179 of file interval_timer.cc.

## ◆ ~IntervalTimer()

isc::asiolink::IntervalTimer::~IntervalTimer | ( |  | ) |

The destructor.

This destructor never throws an exception.

On the destruction of this class the timer will be canceled inside `boost::asio::deadline_timer`.

Definition at line 183 of file interval_timer.cc.

References cancel().

Here is the call graph for this function:

## Member Function Documentation

## ◆ cancel()

void isc::asiolink::IntervalTimer::cancel | ( |  | ) |

Cancel the timer.

If the timer has been set up, this method cancels any asynchronous events waiting on the timer and stops the timer itself. If the timer has already been canceled, this method effectively does nothing.

This method never throws an exception.

Definition at line 195 of file interval_timer.cc.

Referenced by ~IntervalTimer().

## ◆ getInterval()

long isc::asiolink::IntervalTimer::getInterval | ( |  | ) | const

Return the timer interval.

This method returns the timer interval in milliseconds if it's running; if the timer has been canceled it returns 0.

This method never throws an exception.

Definition at line 200 of file interval_timer.cc.

## ◆ setup()

void isc::asiolink::IntervalTimer::setup | ( | const Callback & | cbfunc,
|  | const long | interval,
|  | const Mode & | mode = REPEATING )

Register timer callback function and interval.

This function sets callback function and interval in milliseconds. Timer will actually start after calling `IOService::run()`.

Parameters

  cbfunc | A reference to a function `void(void)` to call back when the timer is expired (should not be an empty functor)
  interval | Interval in milliseconds (greater than 0)
  mode | Determines if the timer will automatically reschedule after each expiration (the default) or behave as a one-shot which will run for a single interval and not reschedule.

Note: IntervalTimer will not pass `boost::system::error_code` to call back function. In case the timer is canceled, the function will not be called.

Exceptions

  isc::InvalidParameter | cbfunc is empty
  isc::BadValue | interval is less than or equal to 0
  isc::Unexpected | internal runtime error

Definition at line 189 of file interval_timer.cc.

The documentation for this class was generated from the following files:

- interval_timer.h
- interval_timer.cc