scheduler.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_UTIL_SCHEDULER_HPP
23 #define NDN_UTIL_SCHEDULER_HPP
24 
25 #include "../common.hpp"
27 
28 #include <set>
29 
30 namespace ndn {
31 namespace util {
32 namespace scheduler {
33 
37 typedef function<void()> EventCallback;
38 
42 class EventInfo;
43 
47 class EventId
48 {
49 public:
54  EventId(std::nullptr_t = nullptr)
55  {
56  }
57 
62  explicit
63  operator bool() const
64  {
65  return !this->operator!();
66  }
67 
72  bool
73  operator!() const;
74 
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 
92  void
94  {
95  m_info.reset();
96  }
97 
98 private:
99  explicit
100  EventId(const weak_ptr<EventInfo>& info)
101  : m_info(info)
102  {
103  }
104 
105 private:
106  weak_ptr<EventInfo> m_info;
107 
108  friend class Scheduler;
109  friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
110 };
111 
112 std::ostream&
113 operator<<(std::ostream& os, const EventId& eventId);
114 
116 {
117 public:
118  bool
119  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const;
120 };
121 
122 typedef std::multiset<shared_ptr<EventInfo>, EventQueueCompare> EventQueue;
123 
127 class Scheduler : noncopyable
128 {
129 public:
133  typedef EventCallback Event;
134 
135  explicit
136  Scheduler(boost::asio::io_service& ioService);
137 
142  EventId
143  scheduleEvent(const time::nanoseconds& after, const EventCallback& callback);
144 
148  void
149  cancelEvent(const EventId& eventId);
150 
154  void
155  cancelAllEvents();
156 
157 private:
161  void
162  scheduleNext();
163 
170  void
171  executeEvent(const boost::system::error_code& code);
172 
173 private:
174  monotonic_deadline_timer m_deadlineTimer;
175  EventQueue m_queue;
176  bool m_isEventExecuting;
177 };
178 
179 } // namespace scheduler
180 
182 
183 } // namespace util
184 
185 // for backwards compatibility
188 
189 } // namespace ndn
190 
191 #endif // NDN_UTIL_SCHEDULER_HPP
function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:37
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
bool operator()(const shared_ptr< EventInfo > &a, const shared_ptr< EventInfo > &b) const
Definition: scheduler.cpp:72
void reset()
clear this EventId
Definition: scheduler.hpp:93
void cancelEvent(const EventId &eventId)
Cancel a scheduled event.
Definition: scheduler.cpp:100
friend std::ostream & operator<<(std::ostream &os, const EventId &eventId)
Definition: scheduler.cpp:66
bool operator!=(const EventId &other) const
Definition: scheduler.hpp:82
void cancelAllEvents()
Cancel all scheduled events.
Definition: scheduler.cpp:118
Scheduler(boost::asio::io_service &ioService)
Definition: scheduler.cpp:77
bool operator==(const EventId &other) const
Definition: scheduler.cpp:59
std::multiset< shared_ptr< EventInfo >, EventQueueCompare > EventQueue
Definition: scheduler.hpp:122
EventId(std::nullptr_t=nullptr)
Constructs an empty EventId.
Definition: scheduler.hpp:54
EventId scheduleEvent(const time::nanoseconds &after, const EventCallback &callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:84
boost::asio::basic_deadline_timer< time::steady_clock > monotonic_deadline_timer
Identifies a scheduled event.
Definition: scheduler.hpp:47
std::ostream & operator<<(std::ostream &os, const EventId &eventId)
Definition: scheduler.cpp:66