blob: b7364ce4b6b90479d04b942075c9bc8497596f16 [file] [log] [blame]
Alexander Afanasyevf6468892014-01-29 01:04:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_UTIL_SCHEDULER_HPP
8#define NDN_UTIL_SCHEDULER_HPP
9
10#include "../common.hpp"
11#include "monotonic_deadline_timer.hpp"
12
13namespace ndn {
14
15struct EventIdImpl; ///< \brief Private storage of information about the event
16/**
17 * \brief Opaque type (shared_ptr) representing ID of the scheduled event
18 */
19typedef shared_ptr<EventIdImpl> EventId;
20
21/**
22 * \brief Generic scheduler
23 */
24class Scheduler
25{
26public:
27 typedef function<void()> Event;
28
29 Scheduler(boost::asio::io_service& ioService);
30
31 /**
32 * \brief Schedule one time event after the specified delay
33 * \returns EventId that can be used to cancel the scheduled event
34 */
35 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070036 scheduleEvent(const time::nanoseconds& after, const Event& event);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080037
38 /**
39 * \brief Schedule periodic event that should be fired every specified period.
40 * First event will be fired 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 schedulePeriodicEvent(const time::nanoseconds& after,
45 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080046 const Event& event);
47
48 /**
49 * \brief Cancel scheduled event
50 */
51 void
52 cancelEvent(const EventId& eventId);
53
54private:
55 void
56 onEvent(const boost::system::error_code& code);
57
58private:
59 boost::asio::io_service& m_ioService;
60
61 struct EventInfo
62 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063 EventInfo(const time::nanoseconds& after,
64 const time::nanoseconds& period,
Alexander Afanasyevf6468892014-01-29 01:04:14 -080065 const Event& event);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066
67 EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068
69 bool
70 operator <=(const EventInfo& other) const
71 {
72 return this->m_scheduledTime <= other.m_scheduledTime;
73 }
74
75 bool
76 operator <(const EventInfo& other) const
77 {
78 return this->m_scheduledTime < other.m_scheduledTime;
79 }
80
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081 time::nanoseconds
Alexander Afanasyevf6468892014-01-29 01:04:14 -080082 expiresFromNow() const;
83
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084 time::steady_clock::TimePoint m_scheduledTime;
85 time::nanoseconds m_period;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080086 Event m_event;
87 mutable EventId m_eventId;
88 };
89
90 typedef std::multiset<EventInfo> EventQueue;
91 friend struct EventIdImpl;
92
93 EventQueue m_events;
94 EventQueue::iterator m_scheduledEvent;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070095 monotonic_deadline_timer m_deadlineTimer;
Yingdi Yuf2a82092014-02-03 16:49:15 -080096
97 bool m_isEventExecuting;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080098};
99
100} // namespace ndn
101
102#endif // NDN_UTIL_SCHEDULER_HPP