blob: c908ec05b88f0b77af2f4b55bb1702c6191ea39b [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Junxiao Shid50f2b42016-08-10 02:59:59 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevf6468892014-01-29 01:04:14 -080020 */
21
22#ifndef NDN_UTIL_SCHEDULER_HPP
23#define NDN_UTIL_SCHEDULER_HPP
24
25#include "../common.hpp"
26#include "monotonic_deadline_timer.hpp"
27
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070028#include <set>
29
Alexander Afanasyevf6468892014-01-29 01:04:14 -080030namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080031namespace util {
32namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080033
Alexander Afanasyevf6468892014-01-29 01:04:14 -080034/**
Junxiao Shid50f2b42016-08-10 02:59:59 +000035 * \brief Function to be invoked when a scheduled event expires
Alexander Afanasyevf6468892014-01-29 01:04:14 -080036 */
Junxiao Shid50f2b42016-08-10 02:59:59 +000037typedef function<void()> EventCallback;
38
39/**
40 * \brief Stores internal information about a scheduled event
41 */
42class EventInfo;
43
44/**
45 * \brief Identifies a scheduled event
46 */
47class EventId
48{
49public:
50 /**
51 * \brief Constructs an empty EventId
52 * \note EventId is implicitly convertible from nullptr.
53 */
54 EventId(std::nullptr_t = nullptr)
55 {
56 }
57
58 /**
59 * \retval true The event is valid.
60 * \retval false This EventId is empty, or the event is expired or cancelled.
61 */
62 explicit
63 operator bool() const
64 {
65 return !this->operator!();
66 }
67
68 /**
69 * \retval true This EventId is empty, or the event is expired or cancelled.
70 * \retval false The event is valid.
71 */
72 bool
73 operator!() const;
74
75 /**
76 * \return whether this and other refer to the same event, or are both empty/expired/cancelled
77 */
78 bool
79 operator==(const EventId& other) const;
80
81 bool
82 operator!=(const EventId& other) const
83 {
84 return !this->operator==(other);
85 }
86
87 /**
88 * \brief clear this EventId
89 * \note This does not cancel the event.
90 * \post !(*this)
91 */
92 void
93 reset()
94 {
95 m_info.reset();
96 }
97
98private:
99 explicit
100 EventId(const weak_ptr<EventInfo>& info)
101 : m_info(info)
102 {
103 }
104
105private:
106 weak_ptr<EventInfo> m_info;
107
108 friend class Scheduler;
109 friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
110};
111
112std::ostream&
113operator<<(std::ostream& os, const EventId& eventId);
114
115class EventQueueCompare
116{
117public:
118 bool
119 operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const;
120};
121
122typedef std::multiset<shared_ptr<EventInfo>, EventQueueCompare> EventQueue;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800123
124/**
125 * \brief Generic scheduler
126 */
Junxiao Shid50f2b42016-08-10 02:59:59 +0000127class Scheduler : noncopyable
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800128{
129public:
Junxiao Shid50f2b42016-08-10 02:59:59 +0000130 /**
131 * \deprecated use EventCallback
132 */
133 typedef EventCallback Event;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800134
Junxiao Shid50f2b42016-08-10 02:59:59 +0000135 explicit
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800136 Scheduler(boost::asio::io_service& ioService);
137
138 /**
Junxiao Shid50f2b42016-08-10 02:59:59 +0000139 * \brief Schedule a one-time event after the specified delay
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800140 * \returns EventId that can be used to cancel the scheduled event
141 */
142 EventId
Junxiao Shid50f2b42016-08-10 02:59:59 +0000143 scheduleEvent(const time::nanoseconds& after, const EventCallback& callback);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800144
145 /**
Junxiao Shid50f2b42016-08-10 02:59:59 +0000146 * \brief Cancel a scheduled event
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800147 */
148 void
149 cancelEvent(const EventId& eventId);
150
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700151 /**
152 * \brief Cancel all scheduled events
153 */
154 void
155 cancelAllEvents();
156
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800157private:
Junxiao Shid50f2b42016-08-10 02:59:59 +0000158 /**
159 * \brief Schedule the next event on the deadline timer
160 */
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800161 void
Junxiao Shid50f2b42016-08-10 02:59:59 +0000162 scheduleNext();
163
164 void
165 executeEvent(const boost::system::error_code& code);
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700166
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800167private:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700168 monotonic_deadline_timer m_deadlineTimer;
Junxiao Shid50f2b42016-08-10 02:59:59 +0000169 EventQueue m_queue;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800170 bool m_isEventExecuting;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800171};
172
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800173} // namespace scheduler
174
175using util::scheduler::Scheduler;
176
177} // namespace util
178
179// for backwards compatibility
180using util::scheduler::Scheduler;
181using util::scheduler::EventId;
182
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800183} // namespace ndn
184
185#endif // NDN_UTIL_SCHEDULER_HPP