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 | |
| 39 | Measurements::~Measurements() |
| 40 | { |
| 41 | } |
| 42 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 43 | static inline bool |
| 44 | predicate_NameTreeEntry_hasMeasurementsEntry(const name_tree::Entry& entry) |
| 45 | { |
| 46 | return static_cast<bool>(entry.getMeasurementsEntry()); |
| 47 | } |
| 48 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 49 | shared_ptr<measurements::Entry> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 50 | Measurements::get(shared_ptr<name_tree::Entry> nameTreeEntry) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 51 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 52 | shared_ptr<measurements::Entry> entry = nameTreeEntry->getMeasurementsEntry(); |
| 53 | if (static_cast<bool>(entry)) |
| 54 | return entry; |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 55 | |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 56 | entry = make_shared<measurements::Entry>(nameTreeEntry->getPrefix()); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 57 | nameTreeEntry->setMeasurementsEntry(entry); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 58 | ++m_nItems; |
| 59 | |
| 60 | entry->m_expiry = time::steady_clock::now() + getInitialLifetime(); |
| 61 | entry->m_cleanup = scheduler::schedule(getInitialLifetime(), |
| 62 | bind(&Measurements::cleanup, this, entry)); |
| 63 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 64 | return entry; |
| 65 | } |
| 66 | |
| 67 | shared_ptr<measurements::Entry> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 68 | Measurements::get(const Name& name) |
| 69 | { |
| 70 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name); |
| 71 | return get(nameTreeEntry); |
| 72 | } |
| 73 | |
| 74 | shared_ptr<measurements::Entry> |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 75 | Measurements::get(const fib::Entry& fibEntry) |
| 76 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 77 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(fibEntry); |
| 78 | |
| 79 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 80 | |
| 81 | return get(nameTreeEntry); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | shared_ptr<measurements::Entry> |
| 85 | Measurements::get(const pit::Entry& pitEntry) |
| 86 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 87 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 88 | |
| 89 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 90 | |
| 91 | return get(nameTreeEntry); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | shared_ptr<measurements::Entry> |
| 95 | Measurements::getParent(shared_ptr<measurements::Entry> child) |
| 96 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 97 | BOOST_ASSERT(child); |
| 98 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 99 | if (child->getName().size() == 0) { |
| 100 | return shared_ptr<measurements::Entry>(); |
| 101 | } |
| 102 | |
| 103 | return this->get(child->getName().getPrefix(-1)); |
| 104 | } |
| 105 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 106 | shared_ptr<measurements::Entry> |
| 107 | Measurements::findLongestPrefixMatch(const Name& name) const |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 108 | { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 109 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 110 | m_nameTree.findLongestPrefixMatch(name, &predicate_NameTreeEntry_hasMeasurementsEntry); |
| 111 | if (static_cast<bool>(nameTreeEntry)) { |
| 112 | return nameTreeEntry->getMeasurementsEntry(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 113 | } |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 114 | return shared_ptr<measurements::Entry>(); |
| 115 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 116 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 117 | shared_ptr<measurements::Entry> |
| 118 | Measurements::findExactMatch(const Name& name) const |
| 119 | { |
| 120 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name); |
| 121 | if (static_cast<bool>(nameTreeEntry)) |
| 122 | return nameTreeEntry->getMeasurementsEntry(); |
| 123 | return shared_ptr<measurements::Entry>(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 127 | Measurements::extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 128 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 129 | shared_ptr<measurements::Entry> ret = this->findExactMatch(entry.getName()); |
| 130 | if (static_cast<bool>(ret)) |
| 131 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 132 | time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime; |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 133 | if (ret->m_expiry >= expiry) // has longer lifetime, not extending |
| 134 | return; |
| 135 | scheduler::cancel(entry.m_cleanup); |
| 136 | entry.m_expiry = expiry; |
| 137 | entry.m_cleanup = scheduler::schedule(lifetime, |
| 138 | bind(&Measurements::cleanup, this, ret)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | Measurements::cleanup(shared_ptr<measurements::Entry> entry) |
| 144 | { |
| 145 | BOOST_ASSERT(entry); |
| 146 | |
| 147 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry->getName()); |
| 148 | if (static_cast<bool>(nameTreeEntry)) |
| 149 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 150 | nameTreeEntry->setMeasurementsEntry(shared_ptr<measurements::Entry>()); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 151 | m_nItems--; |
| 152 | } |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 153 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace nfd |