Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 3 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 25 | |
| 26 | #include "scheduler.hpp" |
Alexander Afanasyev | 1de5da6 | 2014-11-14 17:11:41 -0800 | [diff] [blame^] | 27 | |
| 28 | namespace ns3 { |
| 29 | |
| 30 | template<> |
| 31 | struct 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 Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 42 | namespace nfd { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 43 | namespace scheduler { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 44 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 45 | EventId |
Alexander Afanasyev | 1de5da6 | 2014-11-14 17:11:41 -0800 | [diff] [blame^] | 46 | schedule(const time::nanoseconds& after, const std::function<void()>& event) |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 47 | { |
Alexander Afanasyev | 1de5da6 | 2014-11-14 17:11:41 -0800 | [diff] [blame^] | 48 | 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 Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void |
| 54 | cancel(const EventId& eventId) |
| 55 | { |
Alexander Afanasyev | 1de5da6 | 2014-11-14 17:11:41 -0800 | [diff] [blame^] | 56 | if (eventId != nullptr) { |
| 57 | ns3::Simulator::Remove(*eventId); |
| 58 | const_cast<EventId&>(eventId).reset(); |
| 59 | } |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 62 | ScopedEventId::ScopedEventId() |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | ScopedEventId::ScopedEventId(const EventId& event) |
| 67 | : m_event(event) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | ScopedEventId::ScopedEventId(ScopedEventId&& other) |
| 72 | : m_event(other.m_event) |
| 73 | { |
| 74 | other.release(); |
| 75 | } |
| 76 | |
| 77 | ScopedEventId& |
| 78 | ScopedEventId::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 | |
| 87 | ScopedEventId::~ScopedEventId() |
| 88 | { |
| 89 | scheduler::cancel(m_event); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | ScopedEventId::cancel() |
| 94 | { |
| 95 | scheduler::cancel(m_event); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | ScopedEventId::release() |
| 100 | { |
| 101 | m_event.reset(); |
| 102 | } |
| 103 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 104 | } // namespace scheduler |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 105 | } // namespace nfd |