blob: 1e5b80a3ae6de7da4ede9edd0a659ad8e1099f5d [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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#ifndef NDN_UTIL_SCHEDULER_HPP
23#define NDN_UTIL_SCHEDULER_HPP
24
25#include "../common.hpp"
26#include "monotonic_deadline_timer.hpp"
27
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070028#include <set>
29
Alexander Afanasyevf6468892014-01-29 01:04:14 -080030namespace ndn {
31
32struct EventIdImpl; ///< \brief Private storage of information about the event
33/**
34 * \brief Opaque type (shared_ptr) representing ID of the scheduled event
35 */
36typedef shared_ptr<EventIdImpl> EventId;
37
38/**
39 * \brief Generic scheduler
40 */
41class Scheduler
42{
43public:
44 typedef function<void()> Event;
45
46 Scheduler(boost::asio::io_service& ioService);
47
48 /**
49 * \brief Schedule one time event after the specified delay
50 * \returns EventId that can be used to cancel the scheduled event
51 */
52 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053 scheduleEvent(const time::nanoseconds& after, const Event& event);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080054
55 /**
56 * \brief Schedule periodic event that should be fired every specified period.
57 * First event will be fired after the specified delay.
58 * \returns EventId that can be used to cancel the scheduled event
59 */
60 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061 schedulePeriodicEvent(const time::nanoseconds& after,
62 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080063 const Event& event);
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070064
Alexander Afanasyevf6468892014-01-29 01:04:14 -080065 /**
66 * \brief Cancel scheduled event
67 */
68 void
69 cancelEvent(const EventId& eventId);
70
71private:
72 void
73 onEvent(const boost::system::error_code& code);
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070074
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075private:
Alexander Afanasyevf6468892014-01-29 01:04:14 -080076 struct EventInfo
77 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070078 EventInfo(const time::nanoseconds& after,
79 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080 const Event& event);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081
82 EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080083
84 bool
85 operator <=(const EventInfo& other) const
86 {
87 return this->m_scheduledTime <= other.m_scheduledTime;
88 }
89
90 bool
91 operator <(const EventInfo& other) const
92 {
93 return this->m_scheduledTime < other.m_scheduledTime;
94 }
95
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070096 time::nanoseconds
Alexander Afanasyevf6468892014-01-29 01:04:14 -080097 expiresFromNow() const;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070098
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099 time::steady_clock::TimePoint m_scheduledTime;
100 time::nanoseconds m_period;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800101 Event m_event;
102 mutable EventId m_eventId;
103 };
104
105 typedef std::multiset<EventInfo> EventQueue;
106 friend struct EventIdImpl;
107
108 EventQueue m_events;
109 EventQueue::iterator m_scheduledEvent;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110 monotonic_deadline_timer m_deadlineTimer;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800111
112 bool m_isEventExecuting;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800113};
114
115} // namespace ndn
116
117#endif // NDN_UTIL_SCHEDULER_HPP