Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -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 | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 25 | |
| 26 | #include "measurements.hpp" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 27 | #include "name-tree.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 28 | #include "pit-entry.hpp" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 29 | #include "fib-entry.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 33 | Measurements::Measurements(NameTree& nameTree) |
| 34 | : m_nameTree(nameTree) |
| 35 | , m_nItems(0) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 39 | shared_ptr<measurements::Entry> |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 40 | Measurements::get(name_tree::Entry& nte) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 41 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 42 | shared_ptr<measurements::Entry> entry = nte.getMeasurementsEntry(); |
| 43 | if (entry != nullptr) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 44 | return entry; |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 45 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 46 | entry = make_shared<measurements::Entry>(nte.getPrefix()); |
| 47 | nte.setMeasurementsEntry(entry); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 48 | ++m_nItems; |
| 49 | |
| 50 | entry->m_expiry = time::steady_clock::now() + getInitialLifetime(); |
| 51 | entry->m_cleanup = scheduler::schedule(getInitialLifetime(), |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 52 | bind(&Measurements::cleanup, this, ref(*entry))); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 53 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 54 | return entry; |
| 55 | } |
| 56 | |
| 57 | shared_ptr<measurements::Entry> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 58 | Measurements::get(const Name& name) |
| 59 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 60 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name); |
| 61 | return this->get(*nte); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | shared_ptr<measurements::Entry> |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 65 | Measurements::get(const fib::Entry& fibEntry) |
| 66 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 67 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(fibEntry); |
| 68 | return this->get(*nte); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | shared_ptr<measurements::Entry> |
| 72 | Measurements::get(const pit::Entry& pitEntry) |
| 73 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 74 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry); |
| 75 | return this->get(*nte); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | shared_ptr<measurements::Entry> |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 79 | Measurements::getParent(const measurements::Entry& child) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 80 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 81 | if (child.getName().size() == 0) { // the root entry |
| 82 | return nullptr; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 85 | shared_ptr<name_tree::Entry> nteChild = m_nameTree.get(child); |
| 86 | shared_ptr<name_tree::Entry> nte = nteChild->getParent(); |
| 87 | BOOST_ASSERT(nte != nullptr); |
| 88 | return this->get(*nte); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 91 | shared_ptr<measurements::Entry> |
| 92 | Measurements::findLongestPrefixMatch(const Name& name) const |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 93 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 94 | shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(name, |
| 95 | [] (const name_tree::Entry& nte) { return nte.getMeasurementsEntry() != nullptr; }); |
| 96 | if (nte != nullptr) { |
| 97 | return nte->getMeasurementsEntry(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 98 | } |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 99 | return nullptr; |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 100 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 101 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 102 | shared_ptr<measurements::Entry> |
| 103 | Measurements::findExactMatch(const Name& name) const |
| 104 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 105 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name); |
| 106 | if (nte != nullptr) |
| 107 | return nte->getMeasurementsEntry(); |
| 108 | return nullptr; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 112 | Measurements::extendLifetime(measurements::Entry& entry, |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 113 | const time::nanoseconds& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 114 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 115 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry); |
| 116 | if (nte == nullptr || |
| 117 | nte->getMeasurementsEntry().get() != &entry) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 118 | // entry is already gone; it is a dangling reference |
| 119 | return; |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 120 | } |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 121 | |
| 122 | time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime; |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 123 | if (entry.m_expiry >= expiry) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 124 | // has longer lifetime, not extending |
| 125 | return; |
| 126 | } |
| 127 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 128 | scheduler::cancel(entry.m_cleanup); |
| 129 | entry.m_expiry = expiry; |
| 130 | entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry))); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 134 | Measurements::cleanup(measurements::Entry& entry) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 135 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 136 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry); |
| 137 | if (nte != nullptr) { |
| 138 | nte->setMeasurementsEntry(nullptr); |
| 139 | m_nameTree.eraseEntryIfEmpty(nte); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 140 | m_nItems--; |
| 141 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } // namespace nfd |