NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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 "common.hpp"
23 
24 #include "scheduler.hpp"
25 
26 namespace ns3 {
27 
29 
30 template<>
31 struct EventMemberImplObjTraits<std::function<void()>> {
32  typedef std::function<void()> T;
33  static T&
34  GetReference(T& p)
35  {
36  return p;
37  }
38 };
39 
41 
42 } // namespace ns3
43 
44 namespace ndn {
45 namespace util {
46 namespace scheduler {
47 
49 {
51  : m_event(event)
52  , m_isValid(true)
53  {
54  }
55 
56  void
58  {
59  m_isValid = false;
60  }
61 
62  bool
63  isValid() const
64  {
65  return m_isValid;
66  }
67 
68  operator const Scheduler::EventQueue::iterator&() const
69  {
70  return m_event;
71  }
72 
73  void
75  {
76  m_event = newIterator;
77  m_isValid = true;
78  }
79 
80 private:
82  bool m_isValid;
83 };
84 
85 Scheduler::EventInfo::EventInfo(const time::nanoseconds& after,
86  const Event& event)
87  : m_scheduledTime(time::steady_clock::now() + after)
88  , m_event(event)
89 {
90 }
91 
92 Scheduler::EventInfo::EventInfo(const time::steady_clock::TimePoint& when,
93  const EventInfo& previousEvent)
94  : m_scheduledTime(when)
95  , m_event(previousEvent.m_event)
96  , m_eventId(previousEvent.m_eventId)
97 {
98 }
99 
100 time::nanoseconds
101 Scheduler::EventInfo::expiresFromNow() const
102 {
104  if (now > m_scheduledTime)
105  return time::seconds(0); // event should be scheduled ASAP
106  else
107  return m_scheduledTime - now;
108 }
109 
110 
111 Scheduler::Scheduler(boost::asio::io_service& ioService)
112  : m_scheduledEvent(m_events.end())
113 {
114 }
115 
116 EventId
117 Scheduler::scheduleEvent(const time::nanoseconds& after,
118  const Event& event)
119 {
120  ns3::EventId id = ns3::Simulator::Schedule(ns3::NanoSeconds(after.count()),
121  &std::function<void()>::operator(), event);
122  auto id_ptr = std::make_shared<ns3::EventId>(id);
123  m_events.insert(id_ptr);
124  return id_ptr;
125 }
126 
127 void
129 {
130  if (eventId != nullptr) {
131  ns3::Simulator::Remove(*eventId);
132  const_cast<EventId&>(eventId).reset();
133  m_events.erase(eventId);
134  }
135 }
136 
137 void
139 {
140  for (auto i = m_events.begin(); i != m_events.end(); i++) {
141  if ((*i) != nullptr) {
142  ns3::Simulator::Remove((**i));
143  const_cast<EventId&>(*i).reset();
144  }
145  }
146  m_events.clear();
147 }
148 
149 } // namespace scheduler
150 } // namespace util
151 } // namespace ndn
time_point TimePoint
Definition: time.hpp:108
Copyright (c) 2011-2015 Regents of the University of California.
EventIdImpl(const Scheduler::EventQueue::iterator &event)
Definition: scheduler.cpp:50
static time_point now() noexcept
Definition: time.cpp:79
STL namespace.
EventId scheduleEvent(const time::nanoseconds &after, const Event &event)
Schedule one time event after the specified delay.
Definition: scheduler.cpp:117
void cancelEvent(const EventId &eventId)
Cancel scheduled event.
Definition: scheduler.cpp:128
std::shared_ptr< ns3::EventId > EventId
Definition: scheduler.hpp:39
void cancelAllEvents()
Cancel all scheduled events.
Definition: scheduler.cpp:138
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Private storage of information about the event.
Definition: scheduler.hpp:36
Copyright (c) 2011-2015 Regents of the University of California.
void reset(const Scheduler::EventQueue::iterator &newIterator)
Definition: scheduler.cpp:74