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 | 2d84099 | 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 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 48 | Scheduler::EventInfo::EventInfo(const time::nanoseconds& after, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 49 | const Event& event) |
| 50 | : m_scheduledTime(time::steady_clock::now() + after) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 51 | , m_event(event) |
| 52 | { |
| 53 | } |
| 54 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 55 | Scheduler::EventInfo::EventInfo(const time::steady_clock::TimePoint& when, |
| 56 | const EventInfo& previousEvent) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 57 | : m_scheduledTime(when) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 58 | , m_event(previousEvent.m_event) |
| 59 | , m_eventId(previousEvent.m_eventId) |
| 60 | { |
| 61 | } |
| 62 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 63 | time::nanoseconds |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 64 | Scheduler::EventInfo::expiresFromNow() const |
| 65 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 66 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 67 | if (now > m_scheduledTime) |
| 68 | return time::seconds(0); // event should be scheduled ASAP |
| 69 | else |
| 70 | return m_scheduledTime - now; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | Scheduler::Scheduler(boost::asio::io_service& ioService) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 75 | : m_scheduledEvent(m_events.end()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 76 | { |
| 77 | } |
| 78 | |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 79 | Scheduler::~Scheduler() |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 80 | { |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 81 | cancelAllEvents(); |
| 82 | } |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 83 | |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 84 | EventId |
| 85 | Scheduler::scheduleEvent(const time::nanoseconds& after, const Event& event) |
| 86 | { |
| 87 | EventId eventId = std::make_shared<ns3::EventId>(); |
| 88 | weak_ptr<ns3::EventId> eventWeak = eventId; |
| 89 | std::function<void()> eventWithCleanup = [this, event, eventWeak] () { |
| 90 | event(); |
| 91 | shared_ptr<ns3::EventId> eventId = eventWeak.lock(); |
| 92 | if (eventId != nullptr) { |
| 93 | this->m_events.erase(eventId); // remove the event from the set after it is executed |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 94 | } |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 95 | }; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 96 | |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 97 | ns3::EventId id = ns3::Simulator::Schedule(ns3::NanoSeconds(after.count()), |
| 98 | &std::function<void()>::operator(), eventWithCleanup); |
| 99 | *eventId = std::move(id); |
| 100 | m_events.insert(eventId); |
| 101 | |
| 102 | return eventId; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 103 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 105 | void |
| 106 | Scheduler::cancelEvent(const EventId& eventId) |
| 107 | { |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 108 | if (eventId != nullptr) { |
| 109 | ns3::Simulator::Remove(*eventId); |
| 110 | const_cast<EventId&>(eventId).reset(); |
| 111 | m_events.erase(eventId); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 112 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 116 | Scheduler::cancelAllEvents() |
| 117 | { |
Alexander Afanasyev | 51af49f | 2016-10-18 15:35:44 -0700 | [diff] [blame] | 118 | for (auto i = m_events.begin(); i != m_events.end(); ) { |
| 119 | auto next = i; |
| 120 | ++next; // ns3::Simulator::Remove can call cancelEvent |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 121 | if ((*i) != nullptr) { |
| 122 | ns3::Simulator::Remove((**i)); |
| 123 | const_cast<EventId&>(*i).reset(); |
| 124 | } |
Alexander Afanasyev | 51af49f | 2016-10-18 15:35:44 -0700 | [diff] [blame] | 125 | i = next; |
Spyridon Mastorakis | 2d84099 | 2015-08-13 18:27:49 -0700 | [diff] [blame] | 126 | } |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 127 | m_events.clear(); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 130 | } // namespace scheduler |
| 131 | } // namespace util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 132 | } // namespace ndn |