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