blob: 15200c9859641d2e1c3e0939adcf41140a75b591 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Junxiao Shid50f2b42016-08-10 02:59:59 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevf6468892014-01-29 01:04:14 -080020 */
21
22#include "scheduler.hpp"
Junxiao Shi86dfa532016-08-10 03:00:11 +000023#include <boost/scope_exit.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080024
25namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080026namespace util {
27namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080028
Junxiao Shid50f2b42016-08-10 02:59:59 +000029class EventInfo : noncopyable
Alexander Afanasyevf6468892014-01-29 01:04:14 -080030{
Junxiao Shid50f2b42016-08-10 02:59:59 +000031public:
32 EventInfo(time::nanoseconds after, const EventCallback& callback)
33 : expireTime(time::steady_clock::now() + after)
34 , isExpired(false)
35 , callback(callback)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080036 {
37 }
38
Junxiao Shid50f2b42016-08-10 02:59:59 +000039 time::nanoseconds
40 expiresFromNow() const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080041 {
Junxiao Shid50f2b42016-08-10 02:59:59 +000042 return std::max(expireTime - time::steady_clock::now(), time::nanoseconds::zero());
Alexander Afanasyevf6468892014-01-29 01:04:14 -080043 }
44
Junxiao Shid50f2b42016-08-10 02:59:59 +000045public:
46 time::steady_clock::TimePoint expireTime;
47 bool isExpired;
48 EventCallback callback;
49 EventQueue::const_iterator queueIt;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080050};
51
Junxiao Shid50f2b42016-08-10 02:59:59 +000052bool
53EventId::operator!() const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080054{
Junxiao Shid50f2b42016-08-10 02:59:59 +000055 return m_info.expired() || m_info.lock()->isExpired;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080056}
57
Junxiao Shid50f2b42016-08-10 02:59:59 +000058bool
59EventId::operator==(const EventId& other) const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080060{
Junxiao Shid50f2b42016-08-10 02:59:59 +000061 return (!(*this) && !other) ||
62 !(m_info.owner_before(other.m_info) || other.m_info.owner_before(m_info));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080063}
64
Junxiao Shid50f2b42016-08-10 02:59:59 +000065std::ostream&
66operator<<(std::ostream& os, const EventId& eventId)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080067{
Junxiao Shid50f2b42016-08-10 02:59:59 +000068 return os << eventId.m_info.lock();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080069}
70
Junxiao Shid50f2b42016-08-10 02:59:59 +000071bool
72EventQueueCompare::operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const
73{
74 return a->expireTime < b->expireTime;
75}
Alexander Afanasyevf6468892014-01-29 01:04:14 -080076
77Scheduler::Scheduler(boost::asio::io_service& ioService)
Junxiao Shid50f2b42016-08-10 02:59:59 +000078 : m_deadlineTimer(ioService)
Yingdi Yuf2a82092014-02-03 16:49:15 -080079 , m_isEventExecuting(false)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080{
81}
82
83EventId
Junxiao Shid50f2b42016-08-10 02:59:59 +000084Scheduler::scheduleEvent(const time::nanoseconds& after, const EventCallback& callback)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080085{
Junxiao Shid50f2b42016-08-10 02:59:59 +000086 BOOST_ASSERT(callback != nullptr);
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070087
Junxiao Shid50f2b42016-08-10 02:59:59 +000088 EventQueue::iterator i = m_queue.insert(make_shared<EventInfo>(after, callback));
89 (*i)->queueIt = i;
Yingdi Yuf2a82092014-02-03 16:49:15 -080090
Junxiao Shid50f2b42016-08-10 02:59:59 +000091 if (!m_isEventExecuting && i == m_queue.begin()) {
92 // the new event is the first one to expire
93 this->scheduleNext();
94 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -080095
Junxiao Shid50f2b42016-08-10 02:59:59 +000096 return EventId(*i);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080097}
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070098
Alexander Afanasyevf6468892014-01-29 01:04:14 -080099void
100Scheduler::cancelEvent(const EventId& eventId)
101{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000102 shared_ptr<EventInfo> info = eventId.m_info.lock();
103 if (info == nullptr || info->isExpired) {
104 return; // event already expired or cancelled
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800105 }
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700106
Junxiao Shid50f2b42016-08-10 02:59:59 +0000107 if (info->queueIt == m_queue.begin()) {
108 m_deadlineTimer.cancel();
109 }
110 m_queue.erase(info->queueIt);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800111
Junxiao Shid50f2b42016-08-10 02:59:59 +0000112 if (!m_isEventExecuting) {
113 this->scheduleNext();
114 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800115}
116
117void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700118Scheduler::cancelAllEvents()
119{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000120 m_queue.clear();
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700121 m_deadlineTimer.cancel();
122}
123
124void
Junxiao Shid50f2b42016-08-10 02:59:59 +0000125Scheduler::scheduleNext()
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800126{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000127 if (!m_queue.empty()) {
128 m_deadlineTimer.expires_from_now((*m_queue.begin())->expiresFromNow());
129 m_deadlineTimer.async_wait(bind(&Scheduler::executeEvent, this, _1));
130 }
131}
132
133void
134Scheduler::executeEvent(const boost::system::error_code& error)
135{
136 if (error) { // e.g., cancelled
137 return;
138 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800139
140 m_isEventExecuting = true;
141
Junxiao Shi86dfa532016-08-10 03:00:11 +0000142 BOOST_SCOPE_EXIT_ALL(this) {
143 m_isEventExecuting = false;
144 this->scheduleNext();
145 };
146
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800147 // process all expired events
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700148 time::steady_clock::TimePoint now = time::steady_clock::now();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000149 while (!m_queue.empty()) {
150 EventQueue::iterator head = m_queue.begin();
151 shared_ptr<EventInfo> info = *head;
152 if (info->expireTime > now) {
153 break;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800154 }
155
Junxiao Shid50f2b42016-08-10 02:59:59 +0000156 m_queue.erase(head);
157 info->isExpired = true;
158 info->callback();
159 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800160}
161
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800162} // namespace scheduler
163} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800164} // namespace ndn