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-2018 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 #include "../net/asio-fwd.hpp"
27 
28 #include <boost/system/error_code.hpp>
29 #include <set>
30 
31 namespace ndn {
32 namespace util {
33 
34 namespace detail {
35 class SteadyTimer;
36 } // namespace detail
37 
38 namespace scheduler {
39 
43 using EventCallback = std::function<void()>;
44 
48 class EventInfo;
49 
53 class EventId
54 {
55 public:
60  constexpr
61  EventId(std::nullptr_t = nullptr) noexcept
62  {
63  }
64 
69  explicit
70  operator bool() const noexcept;
71 
75  bool
76  operator==(const EventId& other) const noexcept;
77 
78  bool
79  operator!=(const EventId& other) const noexcept
80  {
81  return !this->operator==(other);
82  }
83 
89  void
90  reset() noexcept
91  {
92  m_info.reset();
93  }
94 
95 private:
96  explicit
97  EventId(weak_ptr<EventInfo> info) noexcept
98  : m_info(std::move(info))
99  {
100  }
101 
102 private:
103  weak_ptr<EventInfo> m_info;
104 
105  friend class Scheduler;
106  friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
107 };
108 
109 std::ostream&
110 operator<<(std::ostream& os, const EventId& eventId);
111 
113 {
114 public:
115  bool
116  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept;
117 };
118 
119 using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
120 
124 class Scheduler : noncopyable
125 {
126 public:
127  explicit
128  Scheduler(boost::asio::io_service& ioService);
129 
130  ~Scheduler();
131 
136  EventId
137  scheduleEvent(time::nanoseconds after, const EventCallback& callback);
138 
142  void
143  cancelEvent(const EventId& eventId);
144 
148  void
149  cancelAllEvents();
150 
151 private:
155  void
156  scheduleNext();
157 
164  void
165  executeEvent(const boost::system::error_code& code);
166 
167 private:
168  unique_ptr<detail::SteadyTimer> m_timer;
169  EventQueue m_queue;
170  bool m_isEventExecuting;
171 };
172 
173 } // namespace scheduler
174 
176 
177 } // namespace util
178 
179 // for backwards compatibility
182 
183 } // namespace ndn
184 
185 #endif // NDN_UTIL_SCHEDULER_HPP
void reset() noexcept
clear this EventId
Definition: scheduler.hpp:90
constexpr EventId(std::nullptr_t=nullptr) noexcept
Constructs an empty EventId.
Definition: scheduler.hpp:61
bool operator!=(const EventId &other) const noexcept
Definition: scheduler.hpp:79
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:65
std::ostream & operator<<(std::ostream &os, LoggerTimestamp)
Write a timestamp to os.
Definition: logger.cpp:127
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:43
std::multiset< shared_ptr< EventInfo >, EventQueueCompare > EventQueue
Definition: scheduler.hpp:119
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:328
Identifies a scheduled event.
Definition: scheduler.hpp:53