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 | |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 30 | /// @cond include_hidden |
| 31 | |
Alexander Afanasyev | 1de5da6 | 2014-11-14 17:11:41 -0800 | [diff] [blame] | 32 | template<> |
| 33 | struct EventMemberImplObjTraits<std::function<void()>> { |
| 34 | typedef std::function<void()> T; |
| 35 | static T& |
| 36 | GetReference(T& p) |
| 37 | { |
| 38 | return p; |
| 39 | } |
| 40 | }; |
| 41 | |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 42 | /// @endcond |
| 43 | |
Alexander Afanasyev | 1de5da6 | 2014-11-14 17:11:41 -0800 | [diff] [blame] | 44 | } // namespace ns3 |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 46 | namespace nfd { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 47 | namespace scheduler { |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 48 | |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 49 | EventId |
Alexander Afanasyev | 1de5da6 | 2014-11-14 17:11:41 -0800 | [diff] [blame] | 50 | schedule(const time::nanoseconds& after, const std::function<void()>& event) |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 51 | { |
Alexander Afanasyev | 1de5da6 | 2014-11-14 17:11:41 -0800 | [diff] [blame] | 52 | 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 Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void |
| 58 | cancel(const EventId& eventId) |
| 59 | { |
Alexander Afanasyev | 1de5da6 | 2014-11-14 17:11:41 -0800 | [diff] [blame] | 60 | if (eventId != nullptr) { |
| 61 | ns3::Simulator::Remove(*eventId); |
| 62 | const_cast<EventId&>(eventId).reset(); |
| 63 | } |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Junxiao Shi | 27533da | 2014-12-15 10:56:20 -0700 | [diff] [blame] | 66 | ScopedEventId::ScopedEventId() |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | ScopedEventId::ScopedEventId(const EventId& event) |
| 71 | : m_event(event) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | ScopedEventId::ScopedEventId(ScopedEventId&& other) |
| 76 | : m_event(other.m_event) |
| 77 | { |
| 78 | other.release(); |
| 79 | } |
| 80 | |
| 81 | ScopedEventId& |
| 82 | ScopedEventId::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 | |
| 91 | ScopedEventId::~ScopedEventId() |
| 92 | { |
| 93 | scheduler::cancel(m_event); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | ScopedEventId::cancel() |
| 98 | { |
| 99 | scheduler::cancel(m_event); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | ScopedEventId::release() |
| 104 | { |
| 105 | m_event.reset(); |
| 106 | } |
| 107 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 108 | } // namespace scheduler |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 109 | } // namespace nfd |