blob: 89892e1d20e91d33acbaa57a901df48e78fb4bda [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Afanasyevf6468892014-01-29 01:04:14 -080011 */
12
13#ifndef NDN_UTIL_SCHEDULER_HPP
14#define NDN_UTIL_SCHEDULER_HPP
15
16#include "../common.hpp"
17#include "monotonic_deadline_timer.hpp"
18
19namespace ndn {
20
21struct EventIdImpl; ///< \brief Private storage of information about the event
22/**
23 * \brief Opaque type (shared_ptr) representing ID of the scheduled event
24 */
25typedef shared_ptr<EventIdImpl> EventId;
26
27/**
28 * \brief Generic scheduler
29 */
30class Scheduler
31{
32public:
33 typedef function<void()> Event;
34
35 Scheduler(boost::asio::io_service& ioService);
36
37 /**
38 * \brief Schedule one time event after the specified delay
39 * \returns EventId that can be used to cancel the scheduled event
40 */
41 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042 scheduleEvent(const time::nanoseconds& after, const Event& event);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080043
44 /**
45 * \brief Schedule periodic event that should be fired every specified period.
46 * First event will be fired after the specified delay.
47 * \returns EventId that can be used to cancel the scheduled event
48 */
49 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050 schedulePeriodicEvent(const time::nanoseconds& after,
51 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080052 const Event& event);
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070053
Alexander Afanasyevf6468892014-01-29 01:04:14 -080054 /**
55 * \brief Cancel scheduled event
56 */
57 void
58 cancelEvent(const EventId& eventId);
59
60private:
61 void
62 onEvent(const boost::system::error_code& code);
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070063
Alexander Afanasyevf6468892014-01-29 01:04:14 -080064private:
Alexander Afanasyevf6468892014-01-29 01:04:14 -080065 struct EventInfo
66 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067 EventInfo(const time::nanoseconds& after,
68 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080069 const Event& event);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070070
71 EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080072
73 bool
74 operator <=(const EventInfo& other) const
75 {
76 return this->m_scheduledTime <= other.m_scheduledTime;
77 }
78
79 bool
80 operator <(const EventInfo& other) const
81 {
82 return this->m_scheduledTime < other.m_scheduledTime;
83 }
84
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070085 time::nanoseconds
Alexander Afanasyevf6468892014-01-29 01:04:14 -080086 expiresFromNow() const;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070087
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 time::steady_clock::TimePoint m_scheduledTime;
89 time::nanoseconds m_period;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080090 Event m_event;
91 mutable EventId m_eventId;
92 };
93
94 typedef std::multiset<EventInfo> EventQueue;
95 friend struct EventIdImpl;
96
97 EventQueue m_events;
98 EventQueue::iterator m_scheduledEvent;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099 monotonic_deadline_timer m_deadlineTimer;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800100
101 bool m_isEventExecuting;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800102};
103
104} // namespace ndn
105
106#endif // NDN_UTIL_SCHEDULER_HPP