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 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 22 | #include "common.hpp" |
| 23 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 24 | #include "scheduler.hpp" |
| 25 | |
| 26 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame^] | 27 | namespace util { |
| 28 | namespace scheduler { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 29 | |
| 30 | struct EventIdImpl |
| 31 | { |
| 32 | EventIdImpl(const Scheduler::EventQueue::iterator& event) |
| 33 | : m_event(event) |
| 34 | , m_isValid(true) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | invalidate() |
| 40 | { |
| 41 | m_isValid = false; |
| 42 | } |
| 43 | |
| 44 | bool |
| 45 | isValid() const |
| 46 | { |
| 47 | return m_isValid; |
| 48 | } |
| 49 | |
| 50 | operator const Scheduler::EventQueue::iterator&() const |
| 51 | { |
| 52 | return m_event; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | reset(const Scheduler::EventQueue::iterator& newIterator) |
| 57 | { |
| 58 | m_event = newIterator; |
| 59 | m_isValid = true; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | Scheduler::EventQueue::iterator m_event; |
| 64 | bool m_isValid; |
| 65 | }; |
| 66 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 67 | Scheduler::EventInfo::EventInfo(const time::nanoseconds& after, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 68 | const Event& event) |
| 69 | : m_scheduledTime(time::steady_clock::now() + after) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 70 | , m_event(event) |
| 71 | { |
| 72 | } |
| 73 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 74 | Scheduler::EventInfo::EventInfo(const time::steady_clock::TimePoint& when, |
| 75 | const EventInfo& previousEvent) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 76 | : m_scheduledTime(when) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 77 | , m_event(previousEvent.m_event) |
| 78 | , m_eventId(previousEvent.m_eventId) |
| 79 | { |
| 80 | } |
| 81 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 82 | time::nanoseconds |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 83 | Scheduler::EventInfo::expiresFromNow() const |
| 84 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 85 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 86 | if (now > m_scheduledTime) |
| 87 | return time::seconds(0); // event should be scheduled ASAP |
| 88 | else |
| 89 | return m_scheduledTime - now; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | Scheduler::Scheduler(boost::asio::io_service& ioService) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 94 | : m_scheduledEvent(m_events.end()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 95 | , m_deadlineTimer(ioService) |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 96 | , m_isEventExecuting(false) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 97 | { |
| 98 | } |
| 99 | |
| 100 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 101 | Scheduler::scheduleEvent(const time::nanoseconds& after, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 102 | const Event& event) |
| 103 | { |
Alexander Afanasyev | 72a1178 | 2014-06-19 13:46:57 -0700 | [diff] [blame] | 104 | EventQueue::iterator i = m_events.insert(EventInfo(after, event)); |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 105 | |
| 106 | // On OSX 10.9, boost, and C++03 the following doesn't work without ndn:: |
| 107 | // because the argument-dependent lookup prefers STL to boost |
| 108 | i->m_eventId = ndn::make_shared<EventIdImpl>(i); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 109 | |
| 110 | if (!m_isEventExecuting) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 111 | { |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 112 | if (m_scheduledEvent == m_events.end() || |
| 113 | *i < *m_scheduledEvent) |
| 114 | { |
| 115 | m_deadlineTimer.expires_from_now(after); |
| 116 | m_deadlineTimer.async_wait(bind(&Scheduler::onEvent, this, _1)); |
| 117 | m_scheduledEvent = i; |
| 118 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | return i->m_eventId; |
| 122 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 124 | void |
| 125 | Scheduler::cancelEvent(const EventId& eventId) |
| 126 | { |
Yingdi Yu | ab13655 | 2014-02-03 14:31:02 -0800 | [diff] [blame] | 127 | if (!static_cast<bool>(eventId) || !eventId->isValid()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 128 | return; // event already fired or cancelled |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 130 | if (static_cast<EventQueue::iterator>(*eventId) != m_scheduledEvent) { |
| 131 | m_events.erase(*eventId); |
| 132 | eventId->invalidate(); |
| 133 | return; |
| 134 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 136 | m_deadlineTimer.cancel(); |
| 137 | m_events.erase(static_cast<EventQueue::iterator>(*eventId)); |
| 138 | eventId->invalidate(); |
| 139 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 140 | if (!m_isEventExecuting) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 141 | { |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 142 | if (!m_events.empty()) |
| 143 | { |
| 144 | m_deadlineTimer.expires_from_now(m_events.begin()->expiresFromNow()); |
| 145 | m_deadlineTimer.async_wait(bind(&Scheduler::onEvent, this, _1)); |
| 146 | m_scheduledEvent = m_events.begin(); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | m_scheduledEvent = m_events.end(); |
| 151 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | void |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 156 | Scheduler::cancelAllEvents() |
| 157 | { |
| 158 | m_events.clear(); |
| 159 | m_deadlineTimer.cancel(); |
| 160 | } |
| 161 | |
| 162 | void |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 163 | Scheduler::onEvent(const boost::system::error_code& error) |
| 164 | { |
| 165 | if (error) // e.g., cancelled |
| 166 | { |
| 167 | return; |
| 168 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 169 | |
| 170 | m_isEventExecuting = true; |
| 171 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 172 | // process all expired events |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 173 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 174 | while(!m_events.empty() && m_events.begin()->m_scheduledTime <= now) |
| 175 | { |
| 176 | EventQueue::iterator head = m_events.begin(); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 177 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 178 | Event event = head->m_event; |
Alexander Afanasyev | 72a1178 | 2014-06-19 13:46:57 -0700 | [diff] [blame] | 179 | head->m_eventId->invalidate(); |
| 180 | m_events.erase(head); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 181 | |
| 182 | event(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (!m_events.empty()) |
| 186 | { |
| 187 | m_deadlineTimer.expires_from_now(m_events.begin()->m_scheduledTime - now); |
| 188 | m_deadlineTimer.async_wait(bind(&Scheduler::onEvent, this, _1)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 189 | m_scheduledEvent = m_events.begin(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 190 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 191 | else |
| 192 | { |
| 193 | m_scheduledEvent = m_events.end(); |
| 194 | } |
| 195 | |
| 196 | m_isEventExecuting = false; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame^] | 200 | } // namespace scheduler |
| 201 | } // namespace util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 202 | } // namespace ndn |