blob: 504a53cc73d85a8b3f6b5b4bf0c547b1e52d382f [file] [log] [blame]
Alexander Afanasyev920af2f2014-01-25 22:56:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev5d57f972015-02-11 21:04:29 -08003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi27533da2014-12-15 10:56:20 -070024 */
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080025
26#include "scheduler.hpp"
27
Alexander Afanasyevae0d0682014-11-14 17:11:41 -080028namespace ns3 {
29
30/// @cond include_hidden
31
32template<>
33struct EventMemberImplObjTraits<std::function<void()>> {
34 typedef std::function<void()> T;
35 static T&
36 GetReference(T& p)
37 {
38 return p;
39 }
40};
41
42/// @endcond
43
44} // namespace ns3
Alexander Afanasyev5d57f972015-02-11 21:04:29 -080045
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080046namespace nfd {
Junxiao Shic041ca32014-02-25 20:01:15 -070047namespace scheduler {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080048
Junxiao Shi98e29f42014-03-31 10:27:26 -070049EventId
Alexander Afanasyevae0d0682014-11-14 17:11:41 -080050schedule(const time::nanoseconds& after, const std::function<void()>& event)
Junxiao Shi98e29f42014-03-31 10:27:26 -070051{
Alexander Afanasyevae0d0682014-11-14 17:11:41 -080052 ns3::EventId id = ns3::Simulator::Schedule(ns3::NanoSeconds(after.count()),
53 &std::function<void()>::operator(), event);
54 return std::make_shared<ns3::EventId>(id);
Junxiao Shi98e29f42014-03-31 10:27:26 -070055}
56
57void
58cancel(const EventId& eventId)
59{
Alexander Afanasyevae0d0682014-11-14 17:11:41 -080060 if (eventId != nullptr) {
61 ns3::Simulator::Remove(*eventId);
62 const_cast<EventId&>(eventId).reset();
63 }
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080064}
65
Junxiao Shi27533da2014-12-15 10:56:20 -070066ScopedEventId::ScopedEventId()
67{
68}
69
70ScopedEventId::ScopedEventId(const EventId& event)
71 : m_event(event)
72{
73}
74
75ScopedEventId::ScopedEventId(ScopedEventId&& other)
76 : m_event(other.m_event)
77{
78 other.release();
79}
80
81ScopedEventId&
82ScopedEventId::operator=(const EventId& event)
83{
84 if (m_event != event) {
85 scheduler::cancel(m_event);
86 m_event = event;
87 }
88 return *this;
89}
90
91ScopedEventId::~ScopedEventId()
92{
93 scheduler::cancel(m_event);
94}
95
96void
97ScopedEventId::cancel()
98{
99 scheduler::cancel(m_event);
100}
101
102void
103ScopedEventId::release()
104{
105 m_event.reset();
106}
107
Junxiao Shic041ca32014-02-25 20:01:15 -0700108} // namespace scheduler
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800109} // namespace nfd