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, |
| 66 | const time::nanoseconds& period, |
| 67 | const Event& event) |
| 68 | : m_scheduledTime(time::steady_clock::now() + after) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 69 | , m_period(period) |
| 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) |
| 77 | , m_period(previousEvent.m_period) |
| 78 | , m_event(previousEvent.m_event) |
| 79 | , m_eventId(previousEvent.m_eventId) |
| 80 | { |
| 81 | } |
| 82 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 83 | time::nanoseconds |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 84 | Scheduler::EventInfo::expiresFromNow() const |
| 85 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 86 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 87 | if (now > m_scheduledTime) |
| 88 | return time::seconds(0); // event should be scheduled ASAP |
| 89 | else |
| 90 | return m_scheduledTime - now; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | Scheduler::Scheduler(boost::asio::io_service& ioService) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 95 | : m_scheduledEvent(m_events.end()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 96 | , m_deadlineTimer(ioService) |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 97 | , m_isEventExecuting(false) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 98 | { |
| 99 | } |
| 100 | |
| 101 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 102 | Scheduler::scheduleEvent(const time::nanoseconds& after, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 103 | const Event& event) |
| 104 | { |
| 105 | return schedulePeriodicEvent(after, time::nanoseconds(-1), event); |
| 106 | } |
| 107 | |
| 108 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 109 | Scheduler::schedulePeriodicEvent(const time::nanoseconds& after, |
| 110 | const time::nanoseconds& period, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 111 | const Event& event) |
| 112 | { |
| 113 | EventQueue::iterator i = m_events.insert(EventInfo(after, period, event)); |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 114 | |
| 115 | // On OSX 10.9, boost, and C++03 the following doesn't work without ndn:: |
| 116 | // because the argument-dependent lookup prefers STL to boost |
| 117 | i->m_eventId = ndn::make_shared<EventIdImpl>(i); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 118 | |
| 119 | if (!m_isEventExecuting) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 120 | { |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 121 | if (m_scheduledEvent == m_events.end() || |
| 122 | *i < *m_scheduledEvent) |
| 123 | { |
| 124 | m_deadlineTimer.expires_from_now(after); |
| 125 | m_deadlineTimer.async_wait(bind(&Scheduler::onEvent, this, _1)); |
| 126 | m_scheduledEvent = i; |
| 127 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return i->m_eventId; |
| 131 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 133 | void |
| 134 | Scheduler::cancelEvent(const EventId& eventId) |
| 135 | { |
Yingdi Yu | ab13655 | 2014-02-03 14:31:02 -0800 | [diff] [blame] | 136 | if (!static_cast<bool>(eventId) || !eventId->isValid()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 137 | return; // event already fired or cancelled |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 139 | if (static_cast<EventQueue::iterator>(*eventId) != m_scheduledEvent) { |
| 140 | m_events.erase(*eventId); |
| 141 | eventId->invalidate(); |
| 142 | return; |
| 143 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 145 | m_deadlineTimer.cancel(); |
| 146 | m_events.erase(static_cast<EventQueue::iterator>(*eventId)); |
| 147 | eventId->invalidate(); |
| 148 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 149 | if (!m_isEventExecuting) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 150 | { |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 151 | if (!m_events.empty()) |
| 152 | { |
| 153 | m_deadlineTimer.expires_from_now(m_events.begin()->expiresFromNow()); |
| 154 | m_deadlineTimer.async_wait(bind(&Scheduler::onEvent, this, _1)); |
| 155 | m_scheduledEvent = m_events.begin(); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | m_scheduledEvent = m_events.end(); |
| 160 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | Scheduler::onEvent(const boost::system::error_code& error) |
| 166 | { |
| 167 | if (error) // e.g., cancelled |
| 168 | { |
| 169 | return; |
| 170 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 171 | |
| 172 | m_isEventExecuting = true; |
| 173 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 174 | // process all expired events |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 175 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 176 | while(!m_events.empty() && m_events.begin()->m_scheduledTime <= now) |
| 177 | { |
| 178 | EventQueue::iterator head = m_events.begin(); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 179 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 180 | Event event = head->m_event; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 181 | if (head->m_period < time::nanoseconds::zero()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 182 | { |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 183 | head->m_eventId->invalidate(); |
| 184 | m_events.erase(head); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 185 | } |
| 186 | else |
| 187 | { |
| 188 | // "reschedule" and update EventId data of the event |
| 189 | EventInfo event(now + head->m_period, *head); |
| 190 | EventQueue::iterator i = m_events.insert(event); |
| 191 | i->m_eventId->reset(i); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 192 | m_events.erase(head); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 193 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 194 | |
| 195 | event(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (!m_events.empty()) |
| 199 | { |
| 200 | m_deadlineTimer.expires_from_now(m_events.begin()->m_scheduledTime - now); |
| 201 | m_deadlineTimer.async_wait(bind(&Scheduler::onEvent, this, _1)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 202 | m_scheduledEvent = m_events.begin(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 203 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 204 | else |
| 205 | { |
| 206 | m_scheduledEvent = m_events.end(); |
| 207 | } |
| 208 | |
| 209 | m_isEventExecuting = false; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
| 213 | } // namespace ndn |