NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
scheduler.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2020 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 
23 
24 #include <boost/scope_exit.hpp>
25 
26 namespace ndn {
27 namespace scheduler {
28 
31 class EventInfo : noncopyable
32 {
33 public:
35  : callback(std::move(cb))
36  , expireTime(time::steady_clock::now() + after)
37  , context(context)
38  {
39  }
40 
43  {
44  return std::max(expireTime - time::steady_clock::now(), 0_ns);
45  }
46 
47 public:
49  Scheduler::EventQueue::const_iterator queueIt;
51  bool isExpired = false;
52  uint32_t context = 0;
53 };
54 
55 EventId::EventId(Scheduler& sched, weak_ptr<EventInfo> info)
56  : CancelHandle([&sched, info] { sched.cancelImpl(info.lock()); })
57  , m_info(std::move(info))
58 {
59 }
60 
61 EventId::operator bool() const noexcept
62 {
63  auto sp = m_info.lock();
64  return sp != nullptr && !sp->isExpired;
65 }
66 
67 void
68 EventId::reset() noexcept
69 {
70  *this = {};
71 }
72 
73 std::ostream&
74 operator<<(std::ostream& os, const EventId& eventId)
75 {
76  return os << eventId.m_info.lock();
77 }
78 
79 bool
80 Scheduler::EventQueueCompare::operator()(const shared_ptr<EventInfo>& a,
81  const shared_ptr<EventInfo>& b) const noexcept
82 {
83  return a->expireTime < b->expireTime;
84 }
85 
87 {
88 }
89 
91 {
92  cancelAllEvents();
93 }
94 
95 EventId
97 {
98  BOOST_ASSERT(callback != nullptr);
99 
100  auto i = m_queue.insert(std::make_shared<EventInfo>(after, std::move(callback), ns3::Simulator::GetContext()));
101  (*i)->queueIt = i;
102 
103  if (!m_isEventExecuting && i == m_queue.begin()) {
104  // the new event is the first one to expire
105  scheduleNext();
106  }
107 
108  return EventId(*this, *i);
109 }
110 
111 void
112 Scheduler::cancelImpl(const shared_ptr<EventInfo>& info)
113 {
114  if (info == nullptr || info->isExpired) {
115  return;
116  }
117 
118  if (m_timerEvent) {
119  if (!m_timerEvent->IsExpired()) {
120  ns3::Simulator::Remove(*m_timerEvent);
121  }
122  m_timerEvent.reset();
123  }
124  m_queue.erase(info->queueIt);
125 
126  if (!m_isEventExecuting) {
127  scheduleNext();
128  }
129 }
130 
131 void
133 {
134  m_queue.clear();
135  if (m_timerEvent) {
136  if (!m_timerEvent->IsExpired()) {
137  ns3::Simulator::Remove(*m_timerEvent);
138  }
139  m_timerEvent.reset();
140  }
141 }
142 
143 void
144 Scheduler::scheduleNext()
145 {
146  if (!m_queue.empty()) {
147  m_timerEvent = ns3::Simulator::Schedule(ns3::NanoSeconds((*m_queue.begin())->expiresFromNow().count()),
148  &Scheduler::executeEvent, this);
149  }
150 }
151 
152 void
153 Scheduler::executeEvent()
154 {
155  m_isEventExecuting = true;
156 
157  m_timerEvent.reset();
158  BOOST_SCOPE_EXIT(this_) {
159  this_->m_isEventExecuting = false;
160  this_->scheduleNext();
161  } BOOST_SCOPE_EXIT_END
162 
163  // process all expired events
164  auto now = time::steady_clock::now();
165  while (!m_queue.empty()) {
166  auto head = m_queue.begin();
167  shared_ptr<EventInfo> info = *head;
168  if (info->expireTime > now) {
169  break;
170  }
171 
172  m_queue.erase(head);
173  info->isExpired = true;
174  if (ns3::Simulator::GetContext() == info->context) {
175  info->callback();
176  }
177  else {
178  ns3::Simulator::ScheduleWithContext(info->context, ns3::Seconds(0), ns3::MakeEvent(info->callback));
179  }
180  }
181 }
182 
183 } // namespace scheduler
184 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
void reset() noexcept
Clear this EventId without canceling.
Definition: scheduler.cpp:68
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:45
static time_point now() noexcept
Definition: time.cpp:80
SignatureInfo info
STL namespace.
EventId schedule(time::nanoseconds after, EventCallback callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:96
EventCallback callback
Definition: scheduler.cpp:48
A handle for a scheduled event.
Definition: scheduler.hpp:58
Scheduler(DummyIoService &ioService)
Definition: scheduler.cpp:86
EventId() noexcept=default
Constructs an empty EventId.
std::ostream & operator<<(std::ostream &os, const EventId &eventId)
Definition: scheduler.cpp:74
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
EventInfo(time::nanoseconds after, EventCallback &&cb, uint32_t context)
Definition: scheduler.cpp:34
Generic time-based scheduler.
Definition: scheduler.hpp:132
Stores internal information about a scheduled event.
Definition: scheduler.cpp:31
Scheduler::EventQueue::const_iterator queueIt
Definition: scheduler.cpp:49
time::steady_clock::TimePoint expireTime
Definition: scheduler.cpp:50
NDN_CXX_NODISCARD time::nanoseconds expiresFromNow() const
Definition: scheduler.cpp:42
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
void cancelAllEvents()
Cancel all scheduled events.
Definition: scheduler.cpp:132
time_point TimePoint
Definition: time.hpp:233