blob: b2a7831ef600910e5ae9ffa34d371857d0734e40 [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
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070019#include <set>
20
Alexander Afanasyevf6468892014-01-29 01:04:14 -080021namespace ndn {
22
23struct EventIdImpl; ///< \brief Private storage of information about the event
24/**
25 * \brief Opaque type (shared_ptr) representing ID of the scheduled event
26 */
27typedef shared_ptr<EventIdImpl> EventId;
28
29/**
30 * \brief Generic scheduler
31 */
32class Scheduler
33{
34public:
35 typedef function<void()> Event;
36
37 Scheduler(boost::asio::io_service& ioService);
38
39 /**
40 * \brief Schedule one time event after the specified delay
41 * \returns EventId that can be used to cancel the scheduled event
42 */
43 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070044 scheduleEvent(const time::nanoseconds& after, const Event& event);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080045
46 /**
47 * \brief Schedule periodic event that should be fired every specified period.
48 * First event will be fired after the specified delay.
49 * \returns EventId that can be used to cancel the scheduled event
50 */
51 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070052 schedulePeriodicEvent(const time::nanoseconds& after,
53 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080054 const Event& event);
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070055
Alexander Afanasyevf6468892014-01-29 01:04:14 -080056 /**
57 * \brief Cancel scheduled event
58 */
59 void
60 cancelEvent(const EventId& eventId);
61
62private:
63 void
64 onEvent(const boost::system::error_code& code);
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070065
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066private:
Alexander Afanasyevf6468892014-01-29 01:04:14 -080067 struct EventInfo
68 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 EventInfo(const time::nanoseconds& after,
70 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080071 const Event& event);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072
73 EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080074
75 bool
76 operator <=(const EventInfo& other) const
77 {
78 return this->m_scheduledTime <= other.m_scheduledTime;
79 }
80
81 bool
82 operator <(const EventInfo& other) const
83 {
84 return this->m_scheduledTime < other.m_scheduledTime;
85 }
86
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070087 time::nanoseconds
Alexander Afanasyevf6468892014-01-29 01:04:14 -080088 expiresFromNow() const;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070089
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090 time::steady_clock::TimePoint m_scheduledTime;
91 time::nanoseconds m_period;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080092 Event m_event;
93 mutable EventId m_eventId;
94 };
95
96 typedef std::multiset<EventInfo> EventQueue;
97 friend struct EventIdImpl;
98
99 EventQueue m_events;
100 EventQueue::iterator m_scheduledEvent;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101 monotonic_deadline_timer m_deadlineTimer;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800102
103 bool m_isEventExecuting;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800104};
105
106} // namespace ndn
107
108#endif // NDN_UTIL_SCHEDULER_HPP