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-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 
23 
24 #include <boost/scope_exit.hpp>
25 
26 namespace ndn {
27 namespace util {
28 namespace scheduler {
29 
32 class EventInfo : noncopyable
33 {
34 public:
35  EventInfo(time::nanoseconds after, const EventCallback& callback)
36  : expireTime(time::steady_clock::now() + after)
37  , isExpired(false)
39  {
40  }
41 
42  time::nanoseconds
44  {
45  return std::max(expireTime - time::steady_clock::now(), 0_ns);
46  }
47 
48 public:
50  bool isExpired;
52  EventQueue::const_iterator queueIt;
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 bool
68 EventId::operator==(const EventId& other) const noexcept
69 {
70  return (!*this && !other) ||
71  !(m_info.owner_before(other.m_info) || other.m_info.owner_before(m_info));
72 }
73 
74 void
75 EventId::reset() noexcept
76 {
77  *this = {};
78 }
79 
80 std::ostream&
81 operator<<(std::ostream& os, const EventId& eventId)
82 {
83  return os << eventId.m_info.lock();
84 }
85 
86 bool
87 EventQueueCompare::operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept
88 {
89  return a->expireTime < b->expireTime;
90 }
91 
93  : m_isEventExecuting(false)
94 {
95 }
96 
98 {
100 }
101 
102 EventId
103 Scheduler::scheduleEvent(time::nanoseconds after, const EventCallback& callback)
104 {
105  BOOST_ASSERT(callback != nullptr);
106 
107  EventQueue::iterator i = m_queue.insert(make_shared<EventInfo>(after, callback));
108  (*i)->queueIt = i;
109 
110  if (!m_isEventExecuting && i == m_queue.begin()) {
111  // the new event is the first one to expire
112  this->scheduleNext();
113  }
114 
115  return EventId(*this, *i);
116 }
117 
118 void
119 Scheduler::cancelImpl(const shared_ptr<EventInfo>& info)
120 {
121  if (info == nullptr || info->isExpired) {
122  return;
123  }
124 
125  if (info->queueIt == m_queue.begin()) {
126  if (m_timerEvent) {
127  if (!m_timerEvent->IsExpired()) {
128  ns3::Simulator::Remove(*m_timerEvent);
129  }
130  m_timerEvent.reset();
131  }
132  }
133  m_queue.erase(info->queueIt);
134 
135  if (!m_isEventExecuting) {
136  this->scheduleNext();
137  }
138 }
139 
140 void
142 {
143  m_queue.clear();
144  if (m_timerEvent) {
145  if (!m_timerEvent->IsExpired()) {
146  ns3::Simulator::Remove(*m_timerEvent);
147  }
148  m_timerEvent.reset();
149  }
150 }
151 
152 void
153 Scheduler::scheduleNext()
154 {
155  if (!m_queue.empty()) {
156  m_timerEvent = ns3::Simulator::Schedule(ns3::NanoSeconds((*m_queue.begin())->expiresFromNow().count()),
157  &Scheduler::executeEvent, this);
158  }
159 }
160 
161 void
162 Scheduler::executeEvent()
163 {
164  m_isEventExecuting = true;
165 
166  m_timerEvent.reset();
167  BOOST_SCOPE_EXIT(this_) {
168  this_->m_isEventExecuting = false;
169  this_->scheduleNext();
170  } BOOST_SCOPE_EXIT_END
171 
172  // process all expired events
173  auto now = time::steady_clock::now();
174  while (!m_queue.empty()) {
175  auto head = m_queue.begin();
176  shared_ptr<EventInfo> info = *head;
177  if (info->expireTime > now) {
178  break;
179  }
180 
181  m_queue.erase(head);
182  info->isExpired = true;
183  info->callback();
184  }
185 }
186 
187 } // namespace scheduler
188 } // namespace util
189 } // namespace ndn
void reset() noexcept
Clear this EventId without canceling.
Definition: scheduler.cpp:75
time_point TimePoint
Definition: time.hpp:225
Copyright (c) 2011-2015 Regents of the University of California.
EventId() noexcept=default
Constructs an empty EventId.
static time_point now() noexcept
Definition: time.cpp:80
EventQueue::const_iterator queueIt
Definition: scheduler.cpp:52
Scheduler(DummyIoService &ioService)
Definition: scheduler.cpp:92
EventId scheduleEvent(time::nanoseconds after, const EventCallback &callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:103
void cancelAllEvents()
Cancel all scheduled events.
Definition: scheduler.cpp:141
Table::const_iterator iterator
Definition: cs-internal.hpp:41
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:42
EventInfo(time::nanoseconds after, const EventCallback &callback)
Definition: scheduler.cpp:35
Stores internal information about a scheduled event.
Definition: scheduler.cpp:32
time::nanoseconds expiresFromNow() const
Definition: scheduler.cpp:43
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
std::ostream & operator<<(std::ostream &os, const EventId &eventId)
Definition: scheduler.cpp:81
time::steady_clock::TimePoint expireTime
Definition: scheduler.cpp:49
bool operator()(const shared_ptr< EventInfo > &a, const shared_ptr< EventInfo > &b) const noexcept
Definition: scheduler.cpp:87