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 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 19 | #include <set> |
| 20 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 21 | namespace ndn { |
| 22 | |
| 23 | struct EventIdImpl; ///< \brief Private storage of information about the event |
| 24 | /** |
| 25 | * \brief Opaque type (shared_ptr) representing ID of the scheduled event |
| 26 | */ |
| 27 | typedef shared_ptr<EventIdImpl> EventId; |
| 28 | |
| 29 | /** |
| 30 | * \brief Generic scheduler |
| 31 | */ |
| 32 | class Scheduler |
| 33 | { |
| 34 | public: |
| 35 | typedef function<void()> Event; |
| 36 | |
| 37 | Scheduler(boost::asio::io_service& ioService); |
| 38 | |
| 39 | /** |
| 40 | * \brief Schedule one time event 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 | scheduleEvent(const time::nanoseconds& after, const Event& event); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * \brief Schedule periodic event that should be fired every specified period. |
| 48 | * First event will be fired after the specified delay. |
| 49 | * \returns EventId that can be used to cancel the scheduled event |
| 50 | */ |
| 51 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 52 | schedulePeriodicEvent(const time::nanoseconds& after, |
| 53 | const time::nanoseconds& period, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 54 | const Event& event); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 56 | /** |
| 57 | * \brief Cancel scheduled event |
| 58 | */ |
| 59 | void |
| 60 | cancelEvent(const EventId& eventId); |
| 61 | |
| 62 | private: |
| 63 | void |
| 64 | onEvent(const boost::system::error_code& code); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 66 | private: |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 67 | struct EventInfo |
| 68 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 69 | EventInfo(const time::nanoseconds& after, |
| 70 | const time::nanoseconds& period, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 71 | const Event& event); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 72 | |
| 73 | EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 74 | |
| 75 | bool |
| 76 | operator <=(const EventInfo& other) const |
| 77 | { |
| 78 | return this->m_scheduledTime <= other.m_scheduledTime; |
| 79 | } |
| 80 | |
| 81 | bool |
| 82 | operator <(const EventInfo& other) const |
| 83 | { |
| 84 | return this->m_scheduledTime < other.m_scheduledTime; |
| 85 | } |
| 86 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 87 | time::nanoseconds |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 88 | expiresFromNow() const; |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 90 | time::steady_clock::TimePoint m_scheduledTime; |
| 91 | time::nanoseconds m_period; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 92 | Event m_event; |
| 93 | mutable EventId m_eventId; |
| 94 | }; |
| 95 | |
| 96 | typedef std::multiset<EventInfo> EventQueue; |
| 97 | friend struct EventIdImpl; |
| 98 | |
| 99 | EventQueue m_events; |
| 100 | EventQueue::iterator m_scheduledEvent; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 101 | monotonic_deadline_timer m_deadlineTimer; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 102 | |
| 103 | bool m_isEventExecuting; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace ndn |
| 107 | |
| 108 | #endif // NDN_UTIL_SCHEDULER_HPP |