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