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