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