blob: 5c271515938510f39e5d4b3dd94d2eef31c1eca8 [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 Mastorakis2d840992015-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
Spyridon Mastorakis2d840992015-08-13 18:27:49 -070036/** \class EventId
37 * \brief Opaque type (shared_ptr) representing ID of a scheduled event
Alexander Afanasyevf6468892014-01-29 01:04:14 -080038 */
Spyridon Mastorakis2d840992015-08-13 18:27:49 -070039typedef std::shared_ptr<ns3::EventId> EventId;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080040
41/**
42 * \brief Generic scheduler
43 */
44class Scheduler
45{
46public:
47 typedef function<void()> Event;
48
49 Scheduler(boost::asio::io_service& ioService);
50
Spyridon Mastorakis2d840992015-08-13 18:27:49 -070051 ~Scheduler();
52
Alexander Afanasyevf6468892014-01-29 01:04:14 -080053 /**
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 Mastorakis2d840992015-08-13 18:27:49 -070099 typedef std::multiset<EventId> EventQueue;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800100
101 EventQueue m_events;
102 EventQueue::iterator m_scheduledEvent;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800103};
104
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800105} // namespace scheduler
106
107using util::scheduler::Scheduler;
108
109} // namespace util
110
111// for backwards compatibility
112using util::scheduler::Scheduler;
113using util::scheduler::EventId;
114
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800115} // namespace ndn
116
117#endif // NDN_UTIL_SCHEDULER_HPP