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  EventId(std::nullptr_t = nullptr)
61  {
62  }
63 
68  explicit
69  operator bool() const;
70 
74  bool
75  operator==(const EventId& other) const;
76 
77  bool
78  operator!=(const EventId& other) const
79  {
80  return !this->operator==(other);
81  }
82 
88  void
90  {
91  m_info.reset();
92  }
93 
94 private:
95  explicit
96  EventId(const weak_ptr<EventInfo>& info)
97  : m_info(info)
98  {
99  }
100 
101 private:
102  weak_ptr<EventInfo> m_info;
103 
104  friend class Scheduler;
105  friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
106 };
107 
108 std::ostream&
109 operator<<(std::ostream& os, const EventId& eventId);
110 
112 {
113 public:
114  bool
115  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const;
116 };
117 
118 using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
119 
123 class Scheduler : noncopyable
124 {
125 public:
126  explicit
127  Scheduler(boost::asio::io_service& ioService);
128 
129  ~Scheduler();
130 
135  EventId
136  scheduleEvent(time::nanoseconds after, const EventCallback& callback);
137 
141  void
142  cancelEvent(const EventId& eventId);
143 
147  void
148  cancelAllEvents();
149 
150 private:
154  void
155  scheduleNext();
156 
163  void
164  executeEvent(const boost::system::error_code& code);
165 
166 private:
167  unique_ptr<detail::SteadyTimer> m_timer;
168  EventQueue m_queue;
169  bool m_isEventExecuting;
170 };
171 
172 } // namespace scheduler
173 
175 
176 } // namespace util
177 
178 // for backwards compatibility
181 
182 } // namespace ndn
183 
184 #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:36
void reset()
clear this EventId
Definition: scheduler.hpp:89
bool operator!=(const EventId &other) const
Definition: scheduler.hpp:78
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:118
EventId(std::nullptr_t=nullptr)
Constructs an empty EventId.
Definition: scheduler.hpp:60
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:265
Identifies a scheduled event.
Definition: scheduler.hpp:53