Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 13 | #include "common.hpp" |
| 14 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 15 | #include "scheduler.hpp" |
| 16 | |
| 17 | namespace ndn { |
| 18 | |
| 19 | struct EventIdImpl |
| 20 | { |
| 21 | EventIdImpl(const Scheduler::EventQueue::iterator& event) |
| 22 | : m_event(event) |
| 23 | , m_isValid(true) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | invalidate() |
| 29 | { |
| 30 | m_isValid = false; |
| 31 | } |
| 32 | |
| 33 | bool |
| 34 | isValid() const |
| 35 | { |
| 36 | return m_isValid; |
| 37 | } |
| 38 | |
| 39 | operator const Scheduler::EventQueue::iterator&() const |
| 40 | { |
| 41 | return m_event; |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | reset(const Scheduler::EventQueue::iterator& newIterator) |
| 46 | { |
| 47 | m_event = newIterator; |
| 48 | m_isValid = true; |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | Scheduler::EventQueue::iterator m_event; |
| 53 | bool m_isValid; |
| 54 | }; |
| 55 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 56 | Scheduler::EventInfo::EventInfo(const time::nanoseconds& after, |
| 57 | const time::nanoseconds& period, |
| 58 | const Event& event) |
| 59 | : m_scheduledTime(time::steady_clock::now() + after) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 60 | , m_period(period) |
| 61 | , m_event(event) |
| 62 | { |
| 63 | } |
| 64 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 65 | Scheduler::EventInfo::EventInfo(const time::steady_clock::TimePoint& when, |
| 66 | const EventInfo& previousEvent) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 67 | : m_scheduledTime(when) |
| 68 | , m_period(previousEvent.m_period) |
| 69 | , m_event(previousEvent.m_event) |
| 70 | , m_eventId(previousEvent.m_eventId) |
| 71 | { |
| 72 | } |
| 73 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 74 | time::nanoseconds |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 75 | Scheduler::EventInfo::expiresFromNow() const |
| 76 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 77 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 78 | if (now > m_scheduledTime) |
| 79 | return time::seconds(0); // event should be scheduled ASAP |
| 80 | else |
| 81 | return m_scheduledTime - now; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | Scheduler::Scheduler(boost::asio::io_service& ioService) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 86 | : m_scheduledEvent(m_events.end()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 87 | , m_deadlineTimer(ioService) |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 88 | , m_isEventExecuting(false) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 89 | { |
| 90 | } |
| 91 | |
| 92 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 93 | Scheduler::scheduleEvent(const time::nanoseconds& after, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 94 | const Event& event) |
| 95 | { |
| 96 | return schedulePeriodicEvent(after, time::nanoseconds(-1), event); |
| 97 | } |
| 98 | |
| 99 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 100 | Scheduler::schedulePeriodicEvent(const time::nanoseconds& after, |
| 101 | const time::nanoseconds& period, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 102 | const Event& event) |
| 103 | { |
| 104 | EventQueue::iterator i = m_events.insert(EventInfo(after, period, 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 |
| 156 | Scheduler::onEvent(const boost::system::error_code& error) |
| 157 | { |
| 158 | if (error) // e.g., cancelled |
| 159 | { |
| 160 | return; |
| 161 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 162 | |
| 163 | m_isEventExecuting = true; |
| 164 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 165 | // process all expired events |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 166 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 167 | while(!m_events.empty() && m_events.begin()->m_scheduledTime <= now) |
| 168 | { |
| 169 | EventQueue::iterator head = m_events.begin(); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 170 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 171 | Event event = head->m_event; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 172 | if (head->m_period < time::nanoseconds::zero()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 173 | { |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 174 | head->m_eventId->invalidate(); |
| 175 | m_events.erase(head); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
| 179 | // "reschedule" and update EventId data of the event |
| 180 | EventInfo event(now + head->m_period, *head); |
| 181 | EventQueue::iterator i = m_events.insert(event); |
| 182 | i->m_eventId->reset(i); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 183 | m_events.erase(head); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 184 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 185 | |
| 186 | event(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (!m_events.empty()) |
| 190 | { |
| 191 | m_deadlineTimer.expires_from_now(m_events.begin()->m_scheduledTime - now); |
| 192 | m_deadlineTimer.async_wait(bind(&Scheduler::onEvent, this, _1)); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 193 | m_scheduledEvent = m_events.begin(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 194 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 195 | else |
| 196 | { |
| 197 | m_scheduledEvent = m_events.end(); |
| 198 | } |
| 199 | |
| 200 | m_isEventExecuting = false; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
| 204 | } // namespace ndn |