| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2013-2018 Regents of the University of California. |
| * |
| * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| * |
| * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| * terms of the GNU Lesser General Public License as published by the Free Software |
| * Foundation, either version 3 of the License, or (at your option) any later version. |
| * |
| * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| * |
| * You should have received copies of the GNU General Public License and GNU Lesser |
| * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| * <http://www.gnu.org/licenses/>. |
| * |
| * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| */ |
| |
| #include "scheduler.hpp" |
| #include "detail/steady-timer.hpp" |
| |
| #include <boost/scope_exit.hpp> |
| |
| namespace ns3 { |
| |
| /// @cond include_hidden |
| |
| template<> |
| struct EventMemberImplObjTraits<std::function<void()>> { |
| typedef std::function<void()> T; |
| static T& |
| GetReference(T& p) |
| { |
| return p; |
| } |
| }; |
| |
| /// @endcond |
| |
| } // namespace ns3 |
| |
| namespace ndn { |
| namespace util { |
| namespace scheduler { |
| |
| Scheduler::Scheduler(boost::asio::io_service& ioService) |
| : m_scheduledEvent(m_events.end()) |
| { |
| } |
| |
| Scheduler::~Scheduler() |
| { |
| cancelAllEvents(); |
| } |
| |
| EventId |
| Scheduler::scheduleEvent(const time::nanoseconds& after, const Event& event) |
| { |
| EventId eventId = std::make_shared<ns3::EventId>(); |
| weak_ptr<ns3::EventId> eventWeak = eventId; |
| std::function<void()> eventWithCleanup = [this, event, eventWeak] () { |
| event(); |
| shared_ptr<ns3::EventId> eventId = eventWeak.lock(); |
| if (eventId != nullptr) { |
| this->m_events.erase(eventId); // remove the event from the set after it is executed |
| } |
| }; |
| |
| ns3::EventId id = ns3::Simulator::Schedule(ns3::NanoSeconds(after.count()), |
| &std::function<void()>::operator(), eventWithCleanup); |
| *eventId = std::move(id); |
| m_events.insert(eventId); |
| |
| return eventId; |
| } |
| |
| void |
| Scheduler::cancelEvent(const EventId& eventId) |
| { |
| if (eventId != nullptr) { |
| ns3::Simulator::Remove(*eventId); |
| m_events.erase(eventId); |
| const_cast<EventId&>(eventId).reset(); |
| } |
| } |
| |
| void |
| Scheduler::cancelAllEvents() |
| { |
| for (auto i = m_events.begin(); i != m_events.end(); ) { |
| auto next = i; |
| ++next; // ns3::Simulator::Remove can call cancelEvent |
| if ((*i) != nullptr) { |
| ns3::Simulator::Remove((**i)); |
| const_cast<EventId&>(*i).reset(); |
| } |
| i = next; |
| } |
| m_events.clear(); |
| } |
| |
| } // namespace scheduler |
| } // namespace util |
| } // namespace ndn |