blob: 523d00537092b6ad48e6a15184f7c96f578e844c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -08003 * Copyright (c) 2013-2015 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
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080022#include "common.hpp"
23
Alexander Afanasyevf6468892014-01-29 01:04:14 -080024#include "scheduler.hpp"
25
Spyridon Mastorakis2d840992015-08-13 18:27:49 -070026namespace ns3 {
27
28/// @cond include_hidden
29
30template<>
31struct 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 Afanasyevf6468892014-01-29 01:04:14 -080044namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080045namespace util {
46namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080047
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048Scheduler::EventInfo::EventInfo(const time::nanoseconds& after,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070049 const Event& event)
50 : m_scheduledTime(time::steady_clock::now() + after)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080051 , m_event(event)
52{
53}
54
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070055Scheduler::EventInfo::EventInfo(const time::steady_clock::TimePoint& when,
56 const EventInfo& previousEvent)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080057 : m_scheduledTime(when)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080058 , m_event(previousEvent.m_event)
59 , m_eventId(previousEvent.m_eventId)
60{
61}
62
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063time::nanoseconds
Alexander Afanasyevf6468892014-01-29 01:04:14 -080064Scheduler::EventInfo::expiresFromNow() const
65{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066 time::steady_clock::TimePoint now = time::steady_clock::now();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080067 if (now > m_scheduledTime)
68 return time::seconds(0); // event should be scheduled ASAP
69 else
70 return m_scheduledTime - now;
71}
72
73
74Scheduler::Scheduler(boost::asio::io_service& ioService)
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070075 : m_scheduledEvent(m_events.end())
Alexander Afanasyevf6468892014-01-29 01:04:14 -080076{
77}
78
Spyridon Mastorakis2d840992015-08-13 18:27:49 -070079Scheduler::~Scheduler()
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080{
Spyridon Mastorakis2d840992015-08-13 18:27:49 -070081 cancelAllEvents();
82}
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070083
Spyridon Mastorakis2d840992015-08-13 18:27:49 -070084EventId
85Scheduler::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 Afanasyevf6468892014-01-29 01:04:14 -080094 }
Spyridon Mastorakis2d840992015-08-13 18:27:49 -070095 };
Alexander Afanasyevf6468892014-01-29 01:04:14 -080096
Spyridon Mastorakis2d840992015-08-13 18:27:49 -070097 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 Afanasyevf6468892014-01-29 01:04:14 -0800103}
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700104
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800105void
106Scheduler::cancelEvent(const EventId& eventId)
107{
Spyridon Mastorakis2d840992015-08-13 18:27:49 -0700108 if (eventId != nullptr) {
109 ns3::Simulator::Remove(*eventId);
110 const_cast<EventId&>(eventId).reset();
111 m_events.erase(eventId);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800112 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800113}
114
115void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700116Scheduler::cancelAllEvents()
117{
Alexander Afanasyev51af49f2016-10-18 15:35:44 -0700118 for (auto i = m_events.begin(); i != m_events.end(); ) {
119 auto next = i;
120 ++next; // ns3::Simulator::Remove can call cancelEvent
Spyridon Mastorakis2d840992015-08-13 18:27:49 -0700121 if ((*i) != nullptr) {
122 ns3::Simulator::Remove((**i));
123 const_cast<EventId&>(*i).reset();
124 }
Alexander Afanasyev51af49f2016-10-18 15:35:44 -0700125 i = next;
Spyridon Mastorakis2d840992015-08-13 18:27:49 -0700126 }
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700127 m_events.clear();
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700128}
129
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800130} // namespace scheduler
131} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800132} // namespace ndn