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 | |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 28 | #include "ns3/simulator.h" |
| 29 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 30 | #include <set> |
| 31 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 32 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 33 | namespace util { |
| 34 | namespace scheduler { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 35 | |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 36 | /** \class EventId |
| 37 | * \brief Opaque type (shared_ptr) representing ID of a scheduled event |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 38 | */ |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 39 | typedef std::shared_ptr<ns3::EventId> EventId; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * \brief Generic scheduler |
| 43 | */ |
| 44 | class Scheduler |
| 45 | { |
| 46 | public: |
| 47 | typedef function<void()> Event; |
| 48 | |
| 49 | Scheduler(boost::asio::io_service& ioService); |
| 50 | |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 51 | ~Scheduler(); |
| 52 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 53 | /** |
| 54 | * \brief Schedule one time event after the specified delay |
| 55 | * \returns EventId that can be used to cancel the scheduled event |
| 56 | */ |
| 57 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 58 | scheduleEvent(const time::nanoseconds& after, const Event& event); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 61 | * \brief Cancel scheduled event |
| 62 | */ |
| 63 | void |
| 64 | cancelEvent(const EventId& eventId); |
| 65 | |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 66 | /** |
| 67 | * \brief Cancel all scheduled events |
| 68 | */ |
| 69 | void |
| 70 | cancelAllEvents(); |
| 71 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 72 | private: |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 73 | struct EventInfo |
| 74 | { |
Alexander Afanasyev | 72a1178 | 2014-06-19 13:46:57 -0700 | [diff] [blame] | 75 | EventInfo(const time::nanoseconds& after, const Event& event); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 76 | |
| 77 | EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 78 | |
| 79 | bool |
| 80 | operator <=(const EventInfo& other) const |
| 81 | { |
| 82 | return this->m_scheduledTime <= other.m_scheduledTime; |
| 83 | } |
| 84 | |
| 85 | bool |
| 86 | operator <(const EventInfo& other) const |
| 87 | { |
| 88 | return this->m_scheduledTime < other.m_scheduledTime; |
| 89 | } |
| 90 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 91 | time::nanoseconds |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 92 | expiresFromNow() const; |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 94 | time::steady_clock::TimePoint m_scheduledTime; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 95 | Event m_event; |
| 96 | mutable EventId m_eventId; |
| 97 | }; |
| 98 | |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 99 | typedef std::multiset<EventId> EventQueue; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 100 | |
| 101 | EventQueue m_events; |
| 102 | EventQueue::iterator m_scheduledEvent; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 105 | } // namespace scheduler |
| 106 | |
| 107 | using util::scheduler::Scheduler; |
| 108 | |
| 109 | } // namespace util |
| 110 | |
| 111 | // for backwards compatibility |
| 112 | using util::scheduler::Scheduler; |
| 113 | using util::scheduler::EventId; |
| 114 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 115 | } // namespace ndn |
| 116 | |
| 117 | #endif // NDN_UTIL_SCHEDULER_HPP |