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