blob: 513348f0825ede733675276978ebbdcb2d5af700 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5bcab562017-07-16 17:19:14 +00002/*
Davide Pesavento5afbb0b2018-01-01 17:24:18 -05003 * Copyright (c) 2013-2018 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
22#include "scheduler.hpp"
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050023#include "detail/steady-timer.hpp"
Davide Pesaventobdcedf42017-10-15 14:56:28 -040024
Junxiao Shi86dfa532016-08-10 03:00:11 +000025#include <boost/scope_exit.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080026
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070027namespace ns3 {
28
29/// @cond include_hidden
30
31template<>
32struct 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 Afanasyevf6468892014-01-29 01:04:14 -080045namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080046namespace util {
47namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080048
Alexander Afanasyevf6468892014-01-29 01:04:14 -080049Scheduler::Scheduler(boost::asio::io_service& ioService)
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070050 : m_scheduledEvent(m_events.end())
Alexander Afanasyevf6468892014-01-29 01:04:14 -080051{
52}
53
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070054Scheduler::~Scheduler()
55{
56 cancelAllEvents();
57}
Junxiao Shi5bcab562017-07-16 17:19:14 +000058
Alexander Afanasyevf6468892014-01-29 01:04:14 -080059EventId
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070060Scheduler::scheduleEvent(const time::nanoseconds& after, const Event& event)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080061{
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070062 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 Afanasyevf73f0632014-05-12 18:02:37 -070071
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070072 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 Yuf2a82092014-02-03 16:49:15 -080076
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070077 return eventId;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080078}
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070079
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080void
81Scheduler::cancelEvent(const EventId& eventId)
82{
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070083 if (eventId != nullptr) {
84 ns3::Simulator::Remove(*eventId);
85 m_events.erase(eventId);
86 const_cast<EventId&>(eventId).reset();
Junxiao Shid50f2b42016-08-10 02:59:59 +000087 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -080088}
89
90void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -070091Scheduler::cancelAllEvents()
92{
Spyridon Mastorakis52d78772015-08-13 18:27:49 -070093 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 Afanasyevf6468892014-01-29 01:04:14 -080099 }
Spyridon Mastorakis52d78772015-08-13 18:27:49 -0700100 i = next;
Junxiao Shid50f2b42016-08-10 02:59:59 +0000101 }
Spyridon Mastorakis52d78772015-08-13 18:27:49 -0700102 m_events.clear();
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800103}
104
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800105} // namespace scheduler
106} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800107} // namespace ndn