blob: 536061c6f817c28f768ad5ee9a9ff050f0ce5b8a [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
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080022#include "common.hpp"
23
Alexander Afanasyevf6468892014-01-29 01:04:14 -080024#include "scheduler.hpp"
25
26namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080027namespace util {
28namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080029
Junxiao Shid50f2b42016-08-10 02:59:59 +000030class EventInfo : noncopyable
Alexander Afanasyevf6468892014-01-29 01:04:14 -080031{
Junxiao Shid50f2b42016-08-10 02:59:59 +000032public:
33 EventInfo(time::nanoseconds after, const EventCallback& callback)
34 : expireTime(time::steady_clock::now() + after)
35 , isExpired(false)
36 , callback(callback)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080037 {
38 }
39
Junxiao Shid50f2b42016-08-10 02:59:59 +000040 time::nanoseconds
41 expiresFromNow() const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080042 {
Junxiao Shid50f2b42016-08-10 02:59:59 +000043 return std::max(expireTime - time::steady_clock::now(), time::nanoseconds::zero());
Alexander Afanasyevf6468892014-01-29 01:04:14 -080044 }
45
Junxiao Shid50f2b42016-08-10 02:59:59 +000046public:
47 time::steady_clock::TimePoint expireTime;
48 bool isExpired;
49 EventCallback callback;
50 EventQueue::const_iterator queueIt;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080051};
52
Junxiao Shid50f2b42016-08-10 02:59:59 +000053bool
54EventId::operator!() const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080055{
Junxiao Shid50f2b42016-08-10 02:59:59 +000056 return m_info.expired() || m_info.lock()->isExpired;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080057}
58
Junxiao Shid50f2b42016-08-10 02:59:59 +000059bool
60EventId::operator==(const EventId& other) const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080061{
Junxiao Shid50f2b42016-08-10 02:59:59 +000062 return (!(*this) && !other) ||
63 !(m_info.owner_before(other.m_info) || other.m_info.owner_before(m_info));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080064}
65
Junxiao Shid50f2b42016-08-10 02:59:59 +000066std::ostream&
67operator<<(std::ostream& os, const EventId& eventId)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068{
Junxiao Shid50f2b42016-08-10 02:59:59 +000069 return os << eventId.m_info.lock();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080070}
71
Junxiao Shid50f2b42016-08-10 02:59:59 +000072bool
73EventQueueCompare::operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const
74{
75 return a->expireTime < b->expireTime;
76}
Alexander Afanasyevf6468892014-01-29 01:04:14 -080077
78Scheduler::Scheduler(boost::asio::io_service& ioService)
Junxiao Shid50f2b42016-08-10 02:59:59 +000079 : m_deadlineTimer(ioService)
Yingdi Yuf2a82092014-02-03 16:49:15 -080080 , m_isEventExecuting(false)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080081{
82}
83
84EventId
Junxiao Shid50f2b42016-08-10 02:59:59 +000085Scheduler::scheduleEvent(const time::nanoseconds& after, const EventCallback& callback)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080086{
Junxiao Shid50f2b42016-08-10 02:59:59 +000087 BOOST_ASSERT(callback != nullptr);
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070088
Junxiao Shid50f2b42016-08-10 02:59:59 +000089 EventQueue::iterator i = m_queue.insert(make_shared<EventInfo>(after, callback));
90 (*i)->queueIt = i;
Yingdi Yuf2a82092014-02-03 16:49:15 -080091
Junxiao Shid50f2b42016-08-10 02:59:59 +000092 if (!m_isEventExecuting && i == m_queue.begin()) {
93 // the new event is the first one to expire
94 this->scheduleNext();
95 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -080096
Junxiao Shid50f2b42016-08-10 02:59:59 +000097 return EventId(*i);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080098}
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070099
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800100void
101Scheduler::cancelEvent(const EventId& eventId)
102{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000103 shared_ptr<EventInfo> info = eventId.m_info.lock();
104 if (info == nullptr || info->isExpired) {
105 return; // event already expired or cancelled
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800106 }
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700107
Junxiao Shid50f2b42016-08-10 02:59:59 +0000108 if (info->queueIt == m_queue.begin()) {
109 m_deadlineTimer.cancel();
110 }
111 m_queue.erase(info->queueIt);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800112
Junxiao Shid50f2b42016-08-10 02:59:59 +0000113 if (!m_isEventExecuting) {
114 this->scheduleNext();
115 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800116}
117
118void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700119Scheduler::cancelAllEvents()
120{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000121 m_queue.clear();
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700122 m_deadlineTimer.cancel();
123}
124
125void
Junxiao Shid50f2b42016-08-10 02:59:59 +0000126Scheduler::scheduleNext()
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800127{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000128 if (!m_queue.empty()) {
129 m_deadlineTimer.expires_from_now((*m_queue.begin())->expiresFromNow());
130 m_deadlineTimer.async_wait(bind(&Scheduler::executeEvent, this, _1));
131 }
132}
133
134void
135Scheduler::executeEvent(const boost::system::error_code& error)
136{
137 if (error) { // e.g., cancelled
138 return;
139 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800140
141 m_isEventExecuting = true;
142
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800143 // process all expired events
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144 time::steady_clock::TimePoint now = time::steady_clock::now();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000145 while (!m_queue.empty()) {
146 EventQueue::iterator head = m_queue.begin();
147 shared_ptr<EventInfo> info = *head;
148 if (info->expireTime > now) {
149 break;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800150 }
151
Junxiao Shid50f2b42016-08-10 02:59:59 +0000152 m_queue.erase(head);
153 info->isExpired = true;
154 info->callback();
155 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800156
157 m_isEventExecuting = false;
Junxiao Shid50f2b42016-08-10 02:59:59 +0000158 this->scheduleNext();
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800159}
160
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800161} // namespace scheduler
162} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800163} // namespace ndn