Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -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 NFD_CORE_SCHEDULER_HPP |
| 8 | #define NFD_CORE_SCHEDULER_HPP |
| 9 | |
| 10 | #include "common.hpp" |
| 11 | #include "monotonic_deadline_timer.hpp" |
| 12 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame^] | 13 | namespace nfd { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 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 |
| 36 | scheduleEvent(const time::Duration& after, const Event& event); |
| 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 |
| 44 | schedulePeriodicEvent(const time::Duration& after, |
| 45 | const time::Duration& period, |
| 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 | { |
| 63 | EventInfo(const time::Duration& after, |
| 64 | const time::Duration& period, |
| 65 | const Event& event); |
| 66 | EventInfo(const time::Point& when, const EventInfo& previousEvent); |
| 67 | |
| 68 | bool |
| 69 | operator <=(const EventInfo& other) const |
| 70 | { |
| 71 | return this->m_scheduledTime <= other.m_scheduledTime; |
| 72 | } |
| 73 | |
| 74 | bool |
| 75 | operator <(const EventInfo& other) const |
| 76 | { |
| 77 | return this->m_scheduledTime < other.m_scheduledTime; |
| 78 | } |
| 79 | |
| 80 | time::Duration |
| 81 | expiresFromNow() const; |
| 82 | |
| 83 | time::Point m_scheduledTime; |
| 84 | time::Duration m_period; |
| 85 | Event m_event; |
| 86 | mutable EventId m_eventId; |
| 87 | }; |
| 88 | |
| 89 | typedef std::multiset<EventInfo> EventQueue; |
| 90 | friend struct EventIdImpl; |
| 91 | |
| 92 | EventQueue m_events; |
| 93 | EventQueue::iterator m_scheduledEvent; |
| 94 | boost::asio::monotonic_deadline_timer m_deadlineTimer; |
| 95 | }; |
| 96 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame^] | 97 | } // namespace nfd |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 98 | |
| 99 | #endif // NFD_CORE_SCHEDULER_HPP |