blob: 2d669b120516e6eecc519915e54d29b864eb24e9 [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/*
Davide Pesavento5afbb0b2018-01-01 17:24:18 -05003 * Copyright (c) 2013-2018 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"
Davide Pesavento4d0d0962017-12-19 22:23:14 -050026#include "../net/asio-fwd.hpp"
Davide Pesaventobdcedf42017-10-15 14:56:28 -040027
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050028#include <boost/system/error_code.hpp>
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070029#include "../common.hpp"
30
31#include "ns3/simulator.h"
32
33#include <set>
34#include <boost/asio/io_service.hpp>
Davide Pesaventobdcedf42017-10-15 14:56:28 -040035#include <set>
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070036
Alexander Afanasyevf6468892014-01-29 01:04:14 -080037namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080038namespace util {
Junxiao Shi5bcab562017-07-16 17:19:14 +000039
40namespace detail {
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050041class SteadyTimer;
Junxiao Shi5bcab562017-07-16 17:19:14 +000042} // namespace detail
43
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080044namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080045
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070046typedef function<void()> EventCallback;
47
48/** \class EventId
49 * \brief Opaque type (shared_ptr) representing ID of a scheduled event
Alexander Afanasyevf6468892014-01-29 01:04:14 -080050 */
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070051typedef std::shared_ptr<ns3::EventId> EventId;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080052
53/**
54 * \brief Generic scheduler
55 */
Junxiao Shid50f2b42016-08-10 02:59:59 +000056class Scheduler : noncopyable
Alexander Afanasyevf6468892014-01-29 01:04:14 -080057{
58public:
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070059 /**
60 * \deprecated use EventCallback
61 */
62 typedef EventCallback Event;
63
Junxiao Shid50f2b42016-08-10 02:59:59 +000064 explicit
Alexander Afanasyevf6468892014-01-29 01:04:14 -080065 Scheduler(boost::asio::io_service& ioService);
66
Junxiao Shi5bcab562017-07-16 17:19:14 +000067 ~Scheduler();
68
Alexander Afanasyevf6468892014-01-29 01:04:14 -080069 /**
Junxiao Shid50f2b42016-08-10 02:59:59 +000070 * \brief Schedule a one-time event after the specified delay
Junxiao Shi86dfa532016-08-10 03:00:11 +000071 * \return EventId that can be used to cancel the scheduled event
Alexander Afanasyevf6468892014-01-29 01:04:14 -080072 */
73 EventId
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070074 scheduleEvent(const time::nanoseconds& after, const Event& event);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075
76 /**
Junxiao Shid50f2b42016-08-10 02:59:59 +000077 * \brief Cancel a scheduled event
Alexander Afanasyevf6468892014-01-29 01:04:14 -080078 */
79 void
80 cancelEvent(const EventId& eventId);
81
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -070082 /**
83 * \brief Cancel all scheduled events
84 */
85 void
86 cancelAllEvents();
87
Alexander Afanasyevf6468892014-01-29 01:04:14 -080088private:
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070089 struct EventInfo
90 {
91 EventInfo(const time::nanoseconds& after, const Event& event);
Junxiao Shid50f2b42016-08-10 02:59:59 +000092
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070093 EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070094
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070095 bool
96 operator <=(const EventInfo& other) const
97 {
98 return this->m_scheduledTime <= other.m_scheduledTime;
99 }
100
101 bool
102 operator <(const EventInfo& other) const
103 {
104 return this->m_scheduledTime < other.m_scheduledTime;
105 }
106
107 time::nanoseconds
108 expiresFromNow() const;
109
110 time::steady_clock::TimePoint m_scheduledTime;
111 Event m_event;
112 mutable EventId m_eventId;
113 };
114
115 typedef std::multiset<EventId> EventQueue;
116
117 EventQueue m_events;
118 EventQueue::iterator m_scheduledEvent;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800119};
120
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800121} // namespace scheduler
122
123using util::scheduler::Scheduler;
124
125} // namespace util
126
127// for backwards compatibility
128using util::scheduler::Scheduler;
129using util::scheduler::EventId;
130
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800131} // namespace ndn
132
133#endif // NDN_UTIL_SCHEDULER_HPP