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