scheduler.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * 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.
20  */
21 
22 #ifndef NDN_UTIL_SCHEDULER_HPP
23 #define NDN_UTIL_SCHEDULER_HPP
24 
25 #include "time.hpp"
26 
27 #include <boost/asio/io_service.hpp>
28 #include <set>
29 
30 namespace ndn {
31 namespace util {
32 
33 namespace detail {
34 class MonotonicDeadlineTimer;
35 } // namespace detail
36 
37 namespace scheduler {
38 
42 using EventCallback = std::function<void()>;
43 
47 class EventInfo;
48 
52 class EventId
53 {
54 public:
59  EventId(std::nullptr_t = nullptr)
60  {
61  }
62 
67  explicit
68  operator bool() const;
69 
73  bool
74  operator==(const EventId& other) const;
75 
76  bool
77  operator!=(const EventId& other) const
78  {
79  return !this->operator==(other);
80  }
81 
87  void
89  {
90  m_info.reset();
91  }
92 
93 private:
94  explicit
95  EventId(const weak_ptr<EventInfo>& info)
96  : m_info(info)
97  {
98  }
99 
100 private:
101  weak_ptr<EventInfo> m_info;
102 
103  friend class Scheduler;
104  friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
105 };
106 
107 std::ostream&
108 operator<<(std::ostream& os, const EventId& eventId);
109 
111 {
112 public:
113  bool
114  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const;
115 };
116 
117 using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
118 
122 class Scheduler : noncopyable
123 {
124 public:
125  explicit
126  Scheduler(boost::asio::io_service& ioService);
127 
128  ~Scheduler();
129 
134  EventId
135  scheduleEvent(time::nanoseconds after, const EventCallback& callback);
136 
140  void
141  cancelEvent(const EventId& eventId);
142 
146  void
147  cancelAllEvents();
148 
149 private:
153  void
154  scheduleNext();
155 
162  void
163  executeEvent(const boost::system::error_code& code);
164 
165 private:
166  unique_ptr<detail::MonotonicDeadlineTimer> m_deadlineTimer;
167  EventQueue m_queue;
168  bool m_isEventExecuting;
169 };
170 
171 } // namespace scheduler
172 
174 
175 } // namespace util
176 
177 // for backwards compatibility
180 
181 } // namespace ndn
182 
183 #endif // NDN_UTIL_SCHEDULER_HPP
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
std::ostream & operator<<(std::ostream &os, LogLevel level)
output LogLevel as string
Definition: logger.cpp:34
void reset()
clear this EventId
Definition: scheduler.hpp:88
bool operator!=(const EventId &other) const
Definition: scheduler.hpp:77
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:42
std::multiset< shared_ptr< EventInfo >, EventQueueCompare > EventQueue
Definition: scheduler.hpp:117
EventId(std::nullptr_t=nullptr)
Constructs an empty EventId.
Definition: scheduler.hpp:59
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:265
Identifies a scheduled event.
Definition: scheduler.hpp:52