blob: cc8bccee3787c023b490091aa48771d546578724 [file] [log] [blame]
Alexander Afanasyevf6468892014-01-29 01:04:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_UTIL_SCHEDULER_HPP
8#define NDN_UTIL_SCHEDULER_HPP
9
10#include "../common.hpp"
11#include "monotonic_deadline_timer.hpp"
12
13namespace ndn {
14
15struct EventIdImpl; ///< \brief Private storage of information about the event
16/**
17 * \brief Opaque type (shared_ptr) representing ID of the scheduled event
18 */
19typedef shared_ptr<EventIdImpl> EventId;
20
21/**
22 * \brief Generic scheduler
23 */
24class Scheduler
25{
26public:
27 typedef function<void()> Event;
28
29 Scheduler(boost::asio::io_service& ioService);
30
31 /**
32 * \brief Schedule one time event after the specified delay
33 * \returns EventId that can be used to cancel the scheduled event
34 */
35 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070036 scheduleEvent(const time::nanoseconds& after, const Event& event);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080037
38 /**
39 * \brief Schedule periodic event that should be fired every specified period.
40 * First event will be fired after the specified delay.
41 * \returns EventId that can be used to cancel the scheduled event
42 */
43 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070044 schedulePeriodicEvent(const time::nanoseconds& after,
45 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080046 const Event& event);
47
48 /**
49 * \brief Cancel scheduled event
50 */
51 void
52 cancelEvent(const EventId& eventId);
53
54private:
55 void
56 onEvent(const boost::system::error_code& code);
57
58private:
Alexander Afanasyevf6468892014-01-29 01:04:14 -080059 struct EventInfo
60 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061 EventInfo(const time::nanoseconds& after,
62 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080063 const Event& event);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064
65 EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066
67 bool
68 operator <=(const EventInfo& other) const
69 {
70 return this->m_scheduledTime <= other.m_scheduledTime;
71 }
72
73 bool
74 operator <(const EventInfo& other) const
75 {
76 return this->m_scheduledTime < other.m_scheduledTime;
77 }
78
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079 time::nanoseconds
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080 expiresFromNow() const;
81
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070082 time::steady_clock::TimePoint m_scheduledTime;
83 time::nanoseconds m_period;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080084 Event m_event;
85 mutable EventId m_eventId;
86 };
87
88 typedef std::multiset<EventInfo> EventQueue;
89 friend struct EventIdImpl;
90
91 EventQueue m_events;
92 EventQueue::iterator m_scheduledEvent;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070093 monotonic_deadline_timer m_deadlineTimer;
Yingdi Yuf2a82092014-02-03 16:49:15 -080094
95 bool m_isEventExecuting;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080096};
97
98} // namespace ndn
99
100#endif // NDN_UTIL_SCHEDULER_HPP