Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_UTIL_SCHEDULER_HPP |
| 23 | #define NDN_UTIL_SCHEDULER_HPP |
| 24 | |
| 25 | #include "../common.hpp" |
| 26 | #include "monotonic_deadline_timer.hpp" |
| 27 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 28 | #include <set> |
| 29 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 30 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 31 | namespace util { |
| 32 | namespace scheduler { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 33 | |
| 34 | struct EventIdImpl; ///< \brief Private storage of information about the event |
| 35 | /** |
| 36 | * \brief Opaque type (shared_ptr) representing ID of the scheduled event |
| 37 | */ |
| 38 | typedef shared_ptr<EventIdImpl> EventId; |
| 39 | |
| 40 | /** |
| 41 | * \brief Generic scheduler |
| 42 | */ |
| 43 | class Scheduler |
| 44 | { |
| 45 | public: |
| 46 | typedef function<void()> Event; |
| 47 | |
| 48 | Scheduler(boost::asio::io_service& ioService); |
| 49 | |
| 50 | /** |
| 51 | * \brief Schedule one time event after the specified delay |
| 52 | * \returns EventId that can be used to cancel the scheduled event |
| 53 | */ |
| 54 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 55 | scheduleEvent(const time::nanoseconds& after, const Event& event); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 58 | * \brief Cancel scheduled event |
| 59 | */ |
| 60 | void |
| 61 | cancelEvent(const EventId& eventId); |
| 62 | |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 63 | /** |
| 64 | * \brief Cancel all scheduled events |
| 65 | */ |
| 66 | void |
| 67 | cancelAllEvents(); |
| 68 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 69 | private: |
| 70 | void |
| 71 | onEvent(const boost::system::error_code& code); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 72 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 73 | private: |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 74 | struct EventInfo |
| 75 | { |
Alexander Afanasyev | 72a1178 | 2014-06-19 13:46:57 -0700 | [diff] [blame] | 76 | EventInfo(const time::nanoseconds& after, const Event& event); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 77 | |
| 78 | EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 79 | |
| 80 | bool |
| 81 | operator <=(const EventInfo& other) const |
| 82 | { |
| 83 | return this->m_scheduledTime <= other.m_scheduledTime; |
| 84 | } |
| 85 | |
| 86 | bool |
| 87 | operator <(const EventInfo& other) const |
| 88 | { |
| 89 | return this->m_scheduledTime < other.m_scheduledTime; |
| 90 | } |
| 91 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 92 | time::nanoseconds |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 93 | expiresFromNow() const; |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 94 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 95 | time::steady_clock::TimePoint m_scheduledTime; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 96 | Event m_event; |
| 97 | mutable EventId m_eventId; |
| 98 | }; |
| 99 | |
| 100 | typedef std::multiset<EventInfo> EventQueue; |
| 101 | friend struct EventIdImpl; |
| 102 | |
| 103 | EventQueue m_events; |
| 104 | EventQueue::iterator m_scheduledEvent; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 105 | monotonic_deadline_timer m_deadlineTimer; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 106 | |
| 107 | bool m_isEventExecuting; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 110 | } // namespace scheduler |
| 111 | |
| 112 | using util::scheduler::Scheduler; |
| 113 | |
| 114 | } // namespace util |
| 115 | |
| 116 | // for backwards compatibility |
| 117 | using util::scheduler::Scheduler; |
| 118 | using util::scheduler::EventId; |
| 119 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 120 | } // namespace ndn |
| 121 | |
| 122 | #endif // NDN_UTIL_SCHEDULER_HPP |