NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
scheduler.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * 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.
20  */
21 
22 #ifndef NDN_UTIL_SCHEDULER_HPP
23 #define NDN_UTIL_SCHEDULER_HPP
24 
27 #include "ndn-cxx/util/time.hpp"
28 
29 #include "ns3/simulator.h"
30 
31 #include <set>
32 
33 namespace ndn {
34 namespace util {
35 namespace scheduler {
36 
37 class Scheduler;
38 class EventInfo;
39 
42 using EventCallback = std::function<void()>;
43 
56 {
57 public:
60  EventId() noexcept = default;
61 
64  [[deprecated]]
65  EventId(std::nullptr_t) noexcept
66  {
67  }
68 
73  explicit
74  operator bool() const noexcept;
75 
79  bool
80  operator==(const EventId& other) const noexcept;
81 
82  bool
83  operator!=(const EventId& other) const noexcept
84  {
85  return !this->operator==(other);
86  }
87 
91  void
92  reset() noexcept;
93 
94 private:
95  EventId(Scheduler& sched, weak_ptr<EventInfo> info);
96 
97 private:
98  weak_ptr<EventInfo> m_info;
99 
100  friend class Scheduler;
101  friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
102 };
103 
104 std::ostream&
105 operator<<(std::ostream& os, const EventId& eventId);
106 
123 class ScopedEventId : public ndn::detail::ScopedCancelHandle
124 {
125 public:
126  using ScopedCancelHandle::ScopedCancelHandle;
127 
128  ScopedEventId() noexcept = default;
129 
132  [[deprecated]]
133  explicit
134  ScopedEventId(Scheduler& scheduler) noexcept
135  {
136  }
137 };
138 
140 {
141 public:
142  bool
143  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept;
144 };
145 
146 using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
147 
150 class Scheduler : noncopyable
151 {
152 public:
153  explicit
154  Scheduler(DummyIoService& ioService);
155 
156  ~Scheduler();
157 
161  EventId
162  scheduleEvent(time::nanoseconds after, const EventCallback& callback);
163 
168  void
169  cancelEvent(const EventId& eid)
170  {
171  eid.cancel();
172  }
173 
176  void
177  cancelAllEvents();
178 
179 private:
180  void
181  cancelImpl(const shared_ptr<EventInfo>& info);
182 
185  void
186  scheduleNext();
187 
190  void
191  executeEvent();
192 
193 private:
194  EventQueue m_queue;
195  bool m_isEventExecuting;
196  ndn::optional<ns3::EventId> m_timerEvent;
197 
198  friend EventId;
199 };
200 
201 } // namespace scheduler
202 
204 
205 } // namespace util
206 
207 // for backwards compatibility
208 using util::scheduler::Scheduler;
209 using util::scheduler::EventId;
210 
211 } // namespace ndn
212 
213 #endif // NDN_UTIL_SCHEDULER_HPP
void reset() noexcept
Clear this EventId without canceling.
Definition: scheduler.cpp:75
Copyright (c) 2011-2015 Regents of the University of California.
EventId() noexcept=default
Constructs an empty EventId.
EventId(std::nullptr_t) noexcept
Allow implicit conversion from nullptr.
Definition: scheduler.hpp:65
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:42
std::multiset< shared_ptr< EventInfo >, EventQueueCompare > EventQueue
Definition: scheduler.hpp:146
Stores internal information about a scheduled event.
Definition: scheduler.cpp:32
A scoped handle of scheduled event.
Definition: scheduler.hpp:123
Handle to cancel an operation.
void cancelEvent(const EventId &eid)
Cancel a scheduled event.
Definition: scheduler.hpp:169
A handle of scheduled event.
Definition: scheduler.hpp:55
bool operator==(const EventId &other) const noexcept
Determine whether this and other refer to the same event, or are both empty/expired/cancelled.
Definition: scheduler.cpp:68
void cancel() const
Cancel the operation.
ScopedEventId(Scheduler &scheduler) noexcept
Definition: scheduler.hpp:134