Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 22 | #include "scheduler.hpp" |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 23 | #include "detail/steady-timer.hpp" |
Davide Pesavento | bdcedf4 | 2017-10-15 14:56:28 -0400 | [diff] [blame] | 24 | |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 25 | #include <boost/scope_exit.hpp> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 26 | |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 27 | namespace ns3 { |
| 28 | |
| 29 | /// @cond include_hidden |
| 30 | |
| 31 | template<> |
| 32 | struct EventMemberImplObjTraits<std::function<void()>> { |
| 33 | typedef std::function<void()> T; |
| 34 | static T& |
| 35 | GetReference(T& p) |
| 36 | { |
| 37 | return p; |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | /// @endcond |
| 42 | |
| 43 | } // namespace ns3 |
| 44 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 45 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 46 | namespace util { |
| 47 | namespace scheduler { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 49 | Scheduler::Scheduler(boost::asio::io_service& ioService) |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 50 | : m_scheduledEvent(m_events.end()) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 54 | Scheduler::~Scheduler() |
| 55 | { |
| 56 | cancelAllEvents(); |
| 57 | } |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 58 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 59 | EventId |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 60 | Scheduler::scheduleEvent(const time::nanoseconds& after, const Event& event) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 61 | { |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 62 | EventId eventId = std::make_shared<ns3::EventId>(); |
| 63 | weak_ptr<ns3::EventId> eventWeak = eventId; |
| 64 | std::function<void()> eventWithCleanup = [this, event, eventWeak] () { |
| 65 | event(); |
| 66 | shared_ptr<ns3::EventId> eventId = eventWeak.lock(); |
| 67 | if (eventId != nullptr) { |
| 68 | this->m_events.erase(eventId); // remove the event from the set after it is executed |
| 69 | } |
| 70 | }; |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 71 | |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 72 | ns3::EventId id = ns3::Simulator::Schedule(ns3::NanoSeconds(after.count()), |
| 73 | &std::function<void()>::operator(), eventWithCleanup); |
| 74 | *eventId = std::move(id); |
| 75 | m_events.insert(eventId); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 76 | |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 77 | return eventId; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 78 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 79 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 80 | void |
| 81 | Scheduler::cancelEvent(const EventId& eventId) |
| 82 | { |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 83 | if (eventId != nullptr) { |
| 84 | ns3::Simulator::Remove(*eventId); |
| 85 | m_events.erase(eventId); |
| 86 | const_cast<EventId&>(eventId).reset(); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 87 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 91 | Scheduler::cancelAllEvents() |
| 92 | { |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 93 | for (auto i = m_events.begin(); i != m_events.end(); ) { |
| 94 | auto next = i; |
| 95 | ++next; // ns3::Simulator::Remove can call cancelEvent |
| 96 | if ((*i) != nullptr) { |
| 97 | ns3::Simulator::Remove((**i)); |
| 98 | const_cast<EventId&>(*i).reset(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 99 | } |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 100 | i = next; |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 101 | } |
Spyridon Mastorakis | 52d7877 | 2015-08-13 18:27:49 -0700 | [diff] [blame^] | 102 | m_events.clear(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 105 | } // namespace scheduler |
| 106 | } // namespace util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 107 | } // namespace ndn |