Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_UTIL_SCHEDULER_HPP |
| 8 | #define NDN_UTIL_SCHEDULER_HPP |
| 9 | |
| 10 | #include "../common.hpp" |
| 11 | #include "monotonic_deadline_timer.hpp" |
| 12 | |
| 13 | namespace ndn { |
| 14 | |
| 15 | struct EventIdImpl; ///< \brief Private storage of information about the event |
| 16 | /** |
| 17 | * \brief Opaque type (shared_ptr) representing ID of the scheduled event |
| 18 | */ |
| 19 | typedef shared_ptr<EventIdImpl> EventId; |
| 20 | |
| 21 | /** |
| 22 | * \brief Generic scheduler |
| 23 | */ |
| 24 | class Scheduler |
| 25 | { |
| 26 | public: |
| 27 | typedef function<void()> Event; |
| 28 | |
| 29 | Scheduler(boost::asio::io_service& ioService); |
| 30 | |
| 31 | /** |
| 32 | * \brief Schedule one time event after the specified delay |
| 33 | * \returns EventId that can be used to cancel the scheduled event |
| 34 | */ |
| 35 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 36 | scheduleEvent(const time::nanoseconds& after, const Event& event); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * \brief Schedule periodic event that should be fired every specified period. |
| 40 | * First event will be fired after the specified delay. |
| 41 | * \returns EventId that can be used to cancel the scheduled event |
| 42 | */ |
| 43 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 44 | schedulePeriodicEvent(const time::nanoseconds& after, |
| 45 | const time::nanoseconds& period, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 46 | const Event& event); |
| 47 | |
| 48 | /** |
| 49 | * \brief Cancel scheduled event |
| 50 | */ |
| 51 | void |
| 52 | cancelEvent(const EventId& eventId); |
| 53 | |
| 54 | private: |
| 55 | void |
| 56 | onEvent(const boost::system::error_code& code); |
| 57 | |
| 58 | private: |
| 59 | boost::asio::io_service& m_ioService; |
| 60 | |
| 61 | struct EventInfo |
| 62 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 63 | EventInfo(const time::nanoseconds& after, |
| 64 | const time::nanoseconds& period, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 65 | const Event& event); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 66 | |
| 67 | EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 68 | |
| 69 | bool |
| 70 | operator <=(const EventInfo& other) const |
| 71 | { |
| 72 | return this->m_scheduledTime <= other.m_scheduledTime; |
| 73 | } |
| 74 | |
| 75 | bool |
| 76 | operator <(const EventInfo& other) const |
| 77 | { |
| 78 | return this->m_scheduledTime < other.m_scheduledTime; |
| 79 | } |
| 80 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 81 | time::nanoseconds |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 82 | expiresFromNow() const; |
| 83 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 84 | time::steady_clock::TimePoint m_scheduledTime; |
| 85 | time::nanoseconds m_period; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 86 | Event m_event; |
| 87 | mutable EventId m_eventId; |
| 88 | }; |
| 89 | |
| 90 | typedef std::multiset<EventInfo> EventQueue; |
| 91 | friend struct EventIdImpl; |
| 92 | |
| 93 | EventQueue m_events; |
| 94 | EventQueue::iterator m_scheduledEvent; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 95 | monotonic_deadline_timer m_deadlineTimer; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 96 | |
| 97 | bool m_isEventExecuting; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace ndn |
| 101 | |
| 102 | #endif // NDN_UTIL_SCHEDULER_HPP |