blob: c5d5e9a0b75601c2d228a8e09d62182a9638e939 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5bcab562017-07-16 17:19:14 +00002/*
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -08003 * Copyright (c) 2013-2017 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
Junxiao Shi5bcab562017-07-16 17:19:14 +000025#include "time.hpp"
Spyridon Mastorakisa4e32eb2015-08-13 18:27:49 -070026#include "../common.hpp"
Davide Pesaventobdcedf42017-10-15 14:56:28 -040027
Spyridon Mastorakisa4e32eb2015-08-13 18:27:49 -070028#include "ns3/simulator.h"
29
30#include <set>
Junxiao Shi5bcab562017-07-16 17:19:14 +000031#include <boost/asio/io_service.hpp>
Davide Pesaventobdcedf42017-10-15 14:56:28 -040032#include <set>
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070033
Alexander Afanasyevf6468892014-01-29 01:04:14 -080034namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080035namespace util {
Junxiao Shi5bcab562017-07-16 17:19:14 +000036
37namespace detail {
38class MonotonicDeadlineTimer;
39} // namespace detail
40
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080041namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080042
Spyridon Mastorakisa4e32eb2015-08-13 18:27:49 -070043typedef function<void()> EventCallback;
44
45/** \class EventId
46 * \brief Opaque type (shared_ptr) representing ID of a scheduled event
Alexander Afanasyevf6468892014-01-29 01:04:14 -080047 */
Spyridon Mastorakisa4e32eb2015-08-13 18:27:49 -070048typedef std::shared_ptr<ns3::EventId> EventId;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080049
50/**
51 * \brief Generic scheduler
52 */
Junxiao Shid50f2b42016-08-10 02:59:59 +000053class Scheduler : noncopyable
Alexander Afanasyevf6468892014-01-29 01:04:14 -080054{
55public:
Spyridon Mastorakisa4e32eb2015-08-13 18:27:49 -070056 /**
57 * \deprecated use EventCallback
58 */
59 typedef EventCallback Event;
60
Junxiao Shid50f2b42016-08-10 02:59:59 +000061 explicit
Alexander Afanasyevf6468892014-01-29 01:04:14 -080062 Scheduler(boost::asio::io_service& ioService);
63
Junxiao Shi5bcab562017-07-16 17:19:14 +000064 ~Scheduler();
65
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066 /**
Junxiao Shid50f2b42016-08-10 02:59:59 +000067 * \brief Schedule a one-time event after the specified delay
Junxiao Shi86dfa532016-08-10 03:00:11 +000068 * \return EventId that can be used to cancel the scheduled event
Alexander Afanasyevf6468892014-01-29 01:04:14 -080069 */
70 EventId
Spyridon Mastorakisa4e32eb2015-08-13 18:27:49 -070071 scheduleEvent(const time::nanoseconds& after, const Event& event);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080072
73 /**
Junxiao Shid50f2b42016-08-10 02:59:59 +000074 * \brief Cancel a scheduled event
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075 */
76 void
77 cancelEvent(const EventId& eventId);
78
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -070079 /**
80 * \brief Cancel all scheduled events
81 */
82 void
83 cancelAllEvents();
84
Alexander Afanasyevf6468892014-01-29 01:04:14 -080085private:
Spyridon Mastorakisa4e32eb2015-08-13 18:27:49 -070086 struct EventInfo
87 {
88 EventInfo(const time::nanoseconds& after, const Event& event);
Junxiao Shid50f2b42016-08-10 02:59:59 +000089
Spyridon Mastorakisa4e32eb2015-08-13 18:27:49 -070090 EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070091
Spyridon Mastorakisa4e32eb2015-08-13 18:27:49 -070092 bool
93 operator <=(const EventInfo& other) const
94 {
95 return this->m_scheduledTime <= other.m_scheduledTime;
96 }
97
98 bool
99 operator <(const EventInfo& other) const
100 {
101 return this->m_scheduledTime < other.m_scheduledTime;
102 }
103
104 time::nanoseconds
105 expiresFromNow() const;
106
107 time::steady_clock::TimePoint m_scheduledTime;
108 Event m_event;
109 mutable EventId m_eventId;
110 };
111
112 typedef std::multiset<EventId> EventQueue;
113
114 EventQueue m_events;
115 EventQueue::iterator m_scheduledEvent;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800116};
117
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800118} // namespace scheduler
119
120using util::scheduler::Scheduler;
121
122} // namespace util
123
124// for backwards compatibility
125using util::scheduler::Scheduler;
126using util::scheduler::EventId;
127
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800128} // namespace ndn
129
130#endif // NDN_UTIL_SCHEDULER_HPP