All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scheduler.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
7 #ifndef NDN_UTIL_SCHEDULER_HPP
8 #define NDN_UTIL_SCHEDULER_HPP
9 
10 #include "../common.hpp"
12 
13 namespace ndn {
14 
15 struct EventIdImpl;
16 
19 typedef shared_ptr<EventIdImpl> EventId;
20 
24 class Scheduler
25 {
26 public:
27  typedef function<void()> Event;
28 
29  Scheduler(boost::asio::io_service& ioService);
30 
35  EventId
36  scheduleEvent(const time::nanoseconds& after, const Event& event);
37 
43  EventId
44  schedulePeriodicEvent(const time::nanoseconds& after,
45  const time::nanoseconds& period,
46  const Event& event);
47 
51  void
52  cancelEvent(const EventId& eventId);
53 
54 private:
55  void
56  onEvent(const boost::system::error_code& code);
57 
58 private:
59  struct EventInfo
60  {
61  EventInfo(const time::nanoseconds& after,
62  const time::nanoseconds& period,
63  const Event& event);
64 
65  EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
66 
67  bool
68  operator <=(const EventInfo& other) const
69  {
70  return this->m_scheduledTime <= other.m_scheduledTime;
71  }
72 
73  bool
74  operator <(const EventInfo& other) const
75  {
76  return this->m_scheduledTime < other.m_scheduledTime;
77  }
78 
79  time::nanoseconds
80  expiresFromNow() const;
81 
82  time::steady_clock::TimePoint m_scheduledTime;
83  time::nanoseconds m_period;
84  Event m_event;
85  mutable EventId m_eventId;
86  };
87 
88  typedef std::multiset<EventInfo> EventQueue;
89  friend struct EventIdImpl;
90 
91  EventQueue m_events;
92  EventQueue::iterator m_scheduledEvent;
93  monotonic_deadline_timer m_deadlineTimer;
94 
95  bool m_isEventExecuting;
96 };
97 
98 } // namespace ndn
99 
100 #endif // NDN_UTIL_SCHEDULER_HPP
time_point TimePoint
Definition: time.hpp:84
function< void()> Event
Definition: scheduler.hpp:27
EventId scheduleEvent(const time::nanoseconds &after, const Event &event)
Schedule one time event after the specified delay.
Definition: scheduler.cpp:86
Generic scheduler.
Definition: scheduler.hpp:24
boost::asio::basic_deadline_timer< time::steady_clock::TimePoint::clock > monotonic_deadline_timer
shared_ptr< EventIdImpl > EventId
Private storage of information about the event.
Definition: scheduler.hpp:15
void cancelEvent(const EventId &eventId)
Cancel scheduled event.
Definition: scheduler.cpp:115
Scheduler(boost::asio::io_service &ioService)
Definition: scheduler.cpp:78
EventId schedulePeriodicEvent(const time::nanoseconds &after, const time::nanoseconds &period, const Event &event)
Schedule periodic event that should be fired every specified period.
Definition: scheduler.cpp:93