Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 22 | #include "common.hpp" |
| 23 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 24 | #include "scheduler.hpp" |
| 25 | |
Spyridon Mastorakis | 578f42a | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 26 | namespace ns3 { |
| 27 | |
| 28 | /// @cond include_hidden |
| 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 | |
| 40 | /// @endcond |
| 41 | |
| 42 | } // namespace ns3 |
| 43 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 44 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 45 | namespace util { |
| 46 | namespace scheduler { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 47 | |
| 48 | struct 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 | |
| 80 | private: |
| 81 | Scheduler::EventQueue::iterator m_event; |
| 82 | bool m_isValid; |
| 83 | }; |
| 84 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 85 | Scheduler::EventInfo::EventInfo(const time::nanoseconds& after, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 86 | const Event& event) |
| 87 | : m_scheduledTime(time::steady_clock::now() + after) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 88 | , m_event(event) |
| 89 | { |
| 90 | } |
| 91 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 92 | Scheduler::EventInfo::EventInfo(const time::steady_clock::TimePoint& when, |
| 93 | const EventInfo& previousEvent) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 94 | : m_scheduledTime(when) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 95 | , m_event(previousEvent.m_event) |
| 96 | , m_eventId(previousEvent.m_eventId) |
| 97 | { |
| 98 | } |
| 99 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 100 | time::nanoseconds |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 101 | Scheduler::EventInfo::expiresFromNow() const |
| 102 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 103 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 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) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 112 | : m_scheduledEvent(m_events.end()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 113 | { |
| 114 | } |
| 115 | |
| 116 | EventId |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 117 | Scheduler::scheduleEvent(const time::nanoseconds& after, |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 118 | const Event& event) |
| 119 | { |
Spyridon Mastorakis | 578f42a | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 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; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 125 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 127 | void |
| 128 | Scheduler::cancelEvent(const EventId& eventId) |
| 129 | { |
Spyridon Mastorakis | 578f42a | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 130 | if (eventId != nullptr) { |
| 131 | ns3::Simulator::Remove(*eventId); |
| 132 | const_cast<EventId&>(eventId).reset(); |
| 133 | m_events.erase(eventId); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 134 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 138 | Scheduler::cancelAllEvents() |
| 139 | { |
Spyridon Mastorakis | 578f42a | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 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 | } |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 146 | m_events.clear(); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 149 | } // namespace scheduler |
| 150 | } // namespace util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 151 | } // namespace ndn |