Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 25 | #include "time.hpp" |
Spyridon Mastorakis | a4e32eb | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 26 | #include "../common.hpp" |
Davide Pesavento | bdcedf4 | 2017-10-15 14:56:28 -0400 | [diff] [blame] | 27 | |
Spyridon Mastorakis | a4e32eb | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 28 | #include "ns3/simulator.h" |
| 29 | |
| 30 | #include <set> |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 31 | #include <boost/asio/io_service.hpp> |
Davide Pesavento | bdcedf4 | 2017-10-15 14:56:28 -0400 | [diff] [blame] | 32 | #include <set> |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 34 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 35 | namespace util { |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 36 | |
| 37 | namespace detail { |
| 38 | class MonotonicDeadlineTimer; |
| 39 | } // namespace detail |
| 40 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 41 | namespace scheduler { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 42 | |
Spyridon Mastorakis | a4e32eb | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 43 | typedef function<void()> EventCallback; |
| 44 | |
| 45 | /** \class EventId |
| 46 | * \brief Opaque type (shared_ptr) representing ID of a scheduled event |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 47 | */ |
Spyridon Mastorakis | a4e32eb | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 48 | typedef std::shared_ptr<ns3::EventId> EventId; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * \brief Generic scheduler |
| 52 | */ |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 53 | class Scheduler : noncopyable |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 54 | { |
| 55 | public: |
Spyridon Mastorakis | a4e32eb | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 56 | /** |
| 57 | * \deprecated use EventCallback |
| 58 | */ |
| 59 | typedef EventCallback Event; |
| 60 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 61 | explicit |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 62 | Scheduler(boost::asio::io_service& ioService); |
| 63 | |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 64 | ~Scheduler(); |
| 65 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 66 | /** |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 67 | * \brief Schedule a one-time event after the specified delay |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 68 | * \return EventId that can be used to cancel the scheduled event |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 69 | */ |
| 70 | EventId |
Spyridon Mastorakis | a4e32eb | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 71 | scheduleEvent(const time::nanoseconds& after, const Event& event); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 74 | * \brief Cancel a scheduled event |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 75 | */ |
| 76 | void |
| 77 | cancelEvent(const EventId& eventId); |
| 78 | |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 79 | /** |
| 80 | * \brief Cancel all scheduled events |
| 81 | */ |
| 82 | void |
| 83 | cancelAllEvents(); |
| 84 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 85 | private: |
Spyridon Mastorakis | a4e32eb | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 86 | struct EventInfo |
| 87 | { |
| 88 | EventInfo(const time::nanoseconds& after, const Event& event); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 89 | |
Spyridon Mastorakis | a4e32eb | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 90 | EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 91 | |
Spyridon Mastorakis | a4e32eb | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 92 | bool |
| 93 | operator <=(const EventInfo& other) const |
| 94 | { |
| 95 | return this->m_scheduledTime <= other.m_scheduledTime; |
| 96 | } |
| 97 | |
| 98 | bool |
| 99 | operator <(const EventInfo& other) const |
| 100 | { |
| 101 | return this->m_scheduledTime < other.m_scheduledTime; |
| 102 | } |
| 103 | |
| 104 | time::nanoseconds |
| 105 | expiresFromNow() const; |
| 106 | |
| 107 | time::steady_clock::TimePoint m_scheduledTime; |
| 108 | Event m_event; |
| 109 | mutable EventId m_eventId; |
| 110 | }; |
| 111 | |
| 112 | typedef std::multiset<EventId> EventQueue; |
| 113 | |
| 114 | EventQueue m_events; |
| 115 | EventQueue::iterator m_scheduledEvent; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 118 | } // namespace scheduler |
| 119 | |
| 120 | using util::scheduler::Scheduler; |
| 121 | |
| 122 | } // namespace util |
| 123 | |
| 124 | // for backwards compatibility |
| 125 | using util::scheduler::Scheduler; |
| 126 | using util::scheduler::EventId; |
| 127 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 128 | } // namespace ndn |
| 129 | |
| 130 | #endif // NDN_UTIL_SCHEDULER_HPP |