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-2021 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_CXX_UTIL_SCHEDULER_HPP
23 #define NDN_CXX_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 
35 namespace util {
36 } // namespace util
37 
38 namespace scheduler {
39 
40 class Scheduler;
41 class EventInfo;
42 
45 using EventCallback = std::function<void()>;
46 
59 {
60 public:
63  EventId() noexcept = default;
64 
69  explicit
70  operator bool() const noexcept;
71 
75  void
76  reset() noexcept;
77 
78 private:
79  // NOTE: the following "hidden friend" operators are available via
80  // argument-dependent lookup only and must be defined inline.
81 
85  friend bool
86  operator==(const EventId& lhs, const EventId& rhs) noexcept
87  {
88  return (!lhs && !rhs) ||
89  (!lhs.m_info.owner_before(rhs.m_info) &&
90  !rhs.m_info.owner_before(lhs.m_info));
91  }
92 
93  friend bool
94  operator!=(const EventId& lhs, const EventId& rhs) noexcept
95  {
96  return !(lhs == rhs);
97  }
98 
99 private:
100  EventId(Scheduler& sched, weak_ptr<EventInfo> info);
101 
102 private:
103  weak_ptr<EventInfo> m_info;
104 
105  friend Scheduler;
106  friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
107 };
108 
109 std::ostream&
110 operator<<(std::ostream& os, const EventId& eventId);
111 
129 
132 class Scheduler : noncopyable
133 {
134 public:
135  explicit
136  Scheduler(DummyIoService& ioService);
137 
138  ~Scheduler();
139 
143  EventId
144  schedule(time::nanoseconds after, EventCallback callback);
145 
148  void
149  cancelAllEvents();
150 
151 private:
152  void
153  cancelImpl(const shared_ptr<EventInfo>& info);
154 
157  void
158  scheduleNext();
159 
162  void
163  executeEvent();
164 
165 private:
166  class EventQueueCompare
167  {
168  public:
169  bool
170  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept;
171  };
172 
173  using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
174  EventQueue m_queue;
175 
176  bool m_isEventExecuting = false;
177  std::optional<ns3::EventId> m_timerEvent;
178 
179  friend EventId;
180  friend EventInfo;
181 };
182 
183 } // namespace scheduler
184 
186 
187 } // namespace ndn
188 
189 #endif // NDN_CXX_UTIL_SCHEDULER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:45
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:29
friend bool operator==(const EventId &lhs, const EventId &rhs) noexcept
Determine whether this and other refer to the same event, or are both empty/expired/cancelled.
Definition: scheduler.hpp:86
SignatureInfo info
A handle for a scheduled event.
Definition: scheduler.hpp:58
Generic time-based scheduler.
Definition: scheduler.hpp:132
Stores internal information about a scheduled event.
Definition: scheduler.cpp:31
Handle to cancel an operation.
friend bool operator!=(const EventId &lhs, const EventId &rhs) noexcept
Definition: scheduler.hpp:94
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50