blob: c9dc3cff1e94ff561a43a649336f7c70e9022e87 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -08003 * Copyright (c) 2013-2015 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
25#include "../common.hpp"
26#include "monotonic_deadline_timer.hpp"
27
Spyridon Mastorakis578f42a2015-08-13 18:27:49 -070028#include "ns3/simulator.h"
29
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030#include <set>
31
Alexander Afanasyevf6468892014-01-29 01:04:14 -080032namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080033namespace util {
34namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080035
36struct EventIdImpl; ///< \brief Private storage of information about the event
Spyridon Mastorakis578f42a2015-08-13 18:27:49 -070037
38/** \class EventId
39 * \brief Opaque type (shared_ptr) representing ID of a scheduled event
Alexander Afanasyevf6468892014-01-29 01:04:14 -080040 */
Spyridon Mastorakis578f42a2015-08-13 18:27:49 -070041typedef std::shared_ptr<ns3::EventId> EventId;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080042
43/**
44 * \brief Generic scheduler
45 */
46class Scheduler
47{
48public:
49 typedef function<void()> Event;
50
51 Scheduler(boost::asio::io_service& ioService);
52
53 /**
54 * \brief Schedule one time event after the specified delay
55 * \returns EventId that can be used to cancel the scheduled event
56 */
57 EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058 scheduleEvent(const time::nanoseconds& after, const Event& event);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080059
60 /**
Alexander Afanasyevf6468892014-01-29 01:04:14 -080061 * \brief Cancel scheduled event
62 */
63 void
64 cancelEvent(const EventId& eventId);
65
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -070066 /**
67 * \brief Cancel all scheduled events
68 */
69 void
70 cancelAllEvents();
71
Alexander Afanasyevf6468892014-01-29 01:04:14 -080072private:
Alexander Afanasyevf6468892014-01-29 01:04:14 -080073 struct EventInfo
74 {
Alexander Afanasyev72a11782014-06-19 13:46:57 -070075 EventInfo(const time::nanoseconds& after, const Event& event);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076
77 EventInfo(const time::steady_clock::TimePoint& when, const EventInfo& previousEvent);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080078
79 bool
80 operator <=(const EventInfo& other) const
81 {
82 return this->m_scheduledTime <= other.m_scheduledTime;
83 }
84
85 bool
86 operator <(const EventInfo& other) const
87 {
88 return this->m_scheduledTime < other.m_scheduledTime;
89 }
90
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091 time::nanoseconds
Alexander Afanasyevf6468892014-01-29 01:04:14 -080092 expiresFromNow() const;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070093
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094 time::steady_clock::TimePoint m_scheduledTime;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080095 Event m_event;
96 mutable EventId m_eventId;
97 };
98
Spyridon Mastorakis578f42a2015-08-13 18:27:49 -070099 typedef std::multiset<EventId> EventQueue;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800100 friend struct EventIdImpl;
101
102 EventQueue m_events;
103 EventQueue::iterator m_scheduledEvent;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800104};
105
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800106} // namespace scheduler
107
108using util::scheduler::Scheduler;
109
110} // namespace util
111
112// for backwards compatibility
113using util::scheduler::Scheduler;
114using util::scheduler::EventId;
115
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800116} // namespace ndn
117
118#endif // NDN_UTIL_SCHEDULER_HPP