blob: ed6cffdff04d6b7413a5982495342ee9907e3820 [file] [log] [blame]
Alexander Afanasyev920af2f2014-01-25 22:56:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi27533da2014-12-15 10:56:20 -07003 * Copyright (c) 2014, 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"
Alexander Afanasyev1de5da62014-11-14 17:11:41 -080027
28namespace ns3 {
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} // namespace ns3
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080041
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080042namespace nfd {
Junxiao Shic041ca32014-02-25 20:01:15 -070043namespace scheduler {
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080044
Junxiao Shi98e29f42014-03-31 10:27:26 -070045EventId
Alexander Afanasyev1de5da62014-11-14 17:11:41 -080046schedule(const time::nanoseconds& after, const std::function<void()>& event)
Junxiao Shi98e29f42014-03-31 10:27:26 -070047{
Alexander Afanasyev1de5da62014-11-14 17:11:41 -080048 ns3::EventId id = ns3::Simulator::Schedule(ns3::NanoSeconds(after.count()),
49 &std::function<void()>::operator(), event);
50 return std::make_shared<ns3::EventId>(id);
Junxiao Shi98e29f42014-03-31 10:27:26 -070051}
52
53void
54cancel(const EventId& eventId)
55{
Alexander Afanasyev1de5da62014-11-14 17:11:41 -080056 if (eventId != nullptr) {
57 ns3::Simulator::Remove(*eventId);
58 const_cast<EventId&>(eventId).reset();
59 }
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080060}
61
Junxiao Shi27533da2014-12-15 10:56:20 -070062ScopedEventId::ScopedEventId()
63{
64}
65
66ScopedEventId::ScopedEventId(const EventId& event)
67 : m_event(event)
68{
69}
70
71ScopedEventId::ScopedEventId(ScopedEventId&& other)
72 : m_event(other.m_event)
73{
74 other.release();
75}
76
77ScopedEventId&
78ScopedEventId::operator=(const EventId& event)
79{
80 if (m_event != event) {
81 scheduler::cancel(m_event);
82 m_event = event;
83 }
84 return *this;
85}
86
87ScopedEventId::~ScopedEventId()
88{
89 scheduler::cancel(m_event);
90}
91
92void
93ScopedEventId::cancel()
94{
95 scheduler::cancel(m_event);
96}
97
98void
99ScopedEventId::release()
100{
101 m_event.reset();
102}
103
Junxiao Shic041ca32014-02-25 20:01:15 -0700104} // namespace scheduler
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800105} // namespace nfd