blob: 9af996a3f82399186febcb8f15c08543ab2cf494 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyevf6468892014-01-29 01:04:14 -080020 */
21
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080022#include "common.hpp"
23
Alexander Afanasyevf6468892014-01-29 01:04:14 -080024#include "scheduler.hpp"
25
Spyridon Mastorakis578f42a2015-08-13 18:27:49 -070026namespace ns3 {
27
28/// @cond include_hidden
29
30template<>
31struct EventMemberImplObjTraits<std::function<void()>> {
32 typedef std::function<void()> T;
33 static T&
34 GetReference(T& p)
35 {
36 return p;
37 }
38};
39
40/// @endcond
41
42} // namespace ns3
43
Alexander Afanasyevf6468892014-01-29 01:04:14 -080044namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080045namespace util {
46namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080047
48struct EventIdImpl
49{
50 EventIdImpl(const Scheduler::EventQueue::iterator& event)
51 : m_event(event)
52 , m_isValid(true)
53 {
54 }
55
56 void
57 invalidate()
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
74 reset(const Scheduler::EventQueue::iterator& newIterator)
75 {
76 m_event = newIterator;
77 m_isValid = true;
78 }
79
80private:
81 Scheduler::EventQueue::iterator m_event;
82 bool m_isValid;
83};
84
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070085Scheduler::EventInfo::EventInfo(const time::nanoseconds& after,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070086 const Event& event)
87 : m_scheduledTime(time::steady_clock::now() + after)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080088 , m_event(event)
89{
90}
91
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070092Scheduler::EventInfo::EventInfo(const time::steady_clock::TimePoint& when,
93 const EventInfo& previousEvent)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080094 : m_scheduledTime(when)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080095 , m_event(previousEvent.m_event)
96 , m_eventId(previousEvent.m_eventId)
97{
98}
99
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100time::nanoseconds
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800101Scheduler::EventInfo::expiresFromNow() const
102{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700103 time::steady_clock::TimePoint now = time::steady_clock::now();
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800104 if (now > m_scheduledTime)
105 return time::seconds(0); // event should be scheduled ASAP
106 else
107 return m_scheduledTime - now;
108}
109
110
111Scheduler::Scheduler(boost::asio::io_service& ioService)
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700112 : m_scheduledEvent(m_events.end())
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800113{
114}
115
116EventId
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117Scheduler::scheduleEvent(const time::nanoseconds& after,
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800118 const Event& event)
119{
Spyridon Mastorakis578f42a2015-08-13 18:27:49 -0700120 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;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800125}
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700126
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800127void
128Scheduler::cancelEvent(const EventId& eventId)
129{
Spyridon Mastorakis578f42a2015-08-13 18:27:49 -0700130 if (eventId != nullptr) {
131 ns3::Simulator::Remove(*eventId);
132 const_cast<EventId&>(eventId).reset();
133 m_events.erase(eventId);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800134 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800135}
136
137void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700138Scheduler::cancelAllEvents()
139{
Spyridon Mastorakis578f42a2015-08-13 18:27:49 -0700140 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 }
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700146 m_events.clear();
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700147}
148
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800149} // namespace scheduler
150} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800151} // namespace ndn