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-2018 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 #include "scheduler.hpp"
23 #include "detail/steady-timer.hpp"
24 
25 #include <boost/scope_exit.hpp>
26 
27 namespace ns3 {
28 
30 
31 template<>
32 struct EventMemberImplObjTraits<std::function<void()>> {
33  typedef std::function<void()> T;
34  static T&
35  GetReference(T& p)
36  {
37  return p;
38  }
39 };
40 
42 
43 } // namespace ns3
44 
45 namespace ndn {
46 namespace util {
47 namespace scheduler {
48 
50  : m_scheduledEvent(m_events.end())
51 {
52 }
53 
55 {
57 }
58 
59 EventId
60 Scheduler::scheduleEvent(const time::nanoseconds& after, const Event& event)
61 {
62  EventId eventId = std::make_shared<ns3::EventId>();
63  weak_ptr<ns3::EventId> eventWeak = eventId;
64  std::function<void()> eventWithCleanup = [this, event, eventWeak] () {
65  event();
66  shared_ptr<ns3::EventId> eventId = eventWeak.lock();
67  if (eventId != nullptr) {
68  this->m_events.erase(eventId); // remove the event from the set after it is executed
69  }
70  };
71 
72  ns3::EventId id = ns3::Simulator::Schedule(ns3::NanoSeconds(after.count()),
73  &std::function<void()>::operator(), eventWithCleanup);
74  *eventId = std::move(id);
75  m_events.insert(eventId);
76 
77  return eventId;
78 }
79 
80 void
82 {
83  if (eventId != nullptr) {
84  ns3::Simulator::Remove(*eventId);
85  m_events.erase(eventId);
86  const_cast<EventId&>(eventId).reset();
87  }
88 }
89 
90 void
92 {
93  for (auto i = m_events.begin(); i != m_events.end(); ) {
94  auto next = i;
95  ++next; // ns3::Simulator::Remove can call cancelEvent
96  if ((*i) != nullptr) {
97  ns3::Simulator::Remove((**i));
98  const_cast<EventId&>(*i).reset();
99  }
100  i = next;
101  }
102  m_events.clear();
103 }
104 
105 } // namespace scheduler
106 } // namespace util
107 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
Scheduler(DummyIoService &ioService)
Definition: scheduler.cpp:49
STL namespace.
EventId scheduleEvent(const time::nanoseconds &after, const Event &event)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:60
void cancelEvent(const EventId &eventId)
Cancel a scheduled event.
Definition: scheduler.cpp:81
void cancelAllEvents()
Cancel all scheduled events.
Definition: scheduler.cpp:91
std::shared_ptr< ns3::EventId > EventId
Definition: scheduler.hpp:51
Opaque type (shared_ptr) representing ID of a scheduled event.
Copyright (c) 2011-2015 Regents of the University of California.