Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 24 | |
| 25 | #include "measurements.hpp" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 26 | #include "name-tree.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 27 | #include "pit-entry.hpp" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 28 | #include "fib-entry.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 32 | const time::nanoseconds Measurements::s_defaultLifetime = time::seconds(4); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 33 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 34 | Measurements::Measurements(NameTree& nameTree) |
| 35 | : m_nameTree(nameTree) |
| 36 | , m_nItems(0) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
| 40 | Measurements::~Measurements() |
| 41 | { |
| 42 | } |
| 43 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 44 | static inline bool |
| 45 | predicate_NameTreeEntry_hasMeasurementsEntry(const name_tree::Entry& entry) |
| 46 | { |
| 47 | return static_cast<bool>(entry.getMeasurementsEntry()); |
| 48 | } |
| 49 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 50 | shared_ptr<measurements::Entry> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 51 | Measurements::get(shared_ptr<name_tree::Entry> nameTreeEntry) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 52 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 53 | shared_ptr<measurements::Entry> entry = nameTreeEntry->getMeasurementsEntry(); |
| 54 | if (static_cast<bool>(entry)) |
| 55 | return entry; |
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); |
| 58 | m_nItems++; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 59 | return entry; |
| 60 | } |
| 61 | |
| 62 | shared_ptr<measurements::Entry> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 63 | Measurements::get(const Name& name) |
| 64 | { |
| 65 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name); |
| 66 | return get(nameTreeEntry); |
| 67 | } |
| 68 | |
| 69 | shared_ptr<measurements::Entry> |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 70 | Measurements::get(const fib::Entry& fibEntry) |
| 71 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 72 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(fibEntry); |
| 73 | |
| 74 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 75 | |
| 76 | return get(nameTreeEntry); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | shared_ptr<measurements::Entry> |
| 80 | Measurements::get(const pit::Entry& pitEntry) |
| 81 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 82 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 83 | |
| 84 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 85 | |
| 86 | return get(nameTreeEntry); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | shared_ptr<measurements::Entry> |
| 90 | Measurements::getParent(shared_ptr<measurements::Entry> child) |
| 91 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 92 | BOOST_ASSERT(child); |
| 93 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 94 | if (child->getName().size() == 0) { |
| 95 | return shared_ptr<measurements::Entry>(); |
| 96 | } |
| 97 | |
| 98 | return this->get(child->getName().getPrefix(-1)); |
| 99 | } |
| 100 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 101 | shared_ptr<measurements::Entry> |
| 102 | Measurements::findLongestPrefixMatch(const Name& name) const |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 103 | { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 104 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 105 | m_nameTree.findLongestPrefixMatch(name, &predicate_NameTreeEntry_hasMeasurementsEntry); |
| 106 | if (static_cast<bool>(nameTreeEntry)) { |
| 107 | return nameTreeEntry->getMeasurementsEntry(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 108 | } |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 109 | return shared_ptr<measurements::Entry>(); |
| 110 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 111 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 112 | shared_ptr<measurements::Entry> |
| 113 | Measurements::findExactMatch(const Name& name) const |
| 114 | { |
| 115 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name); |
| 116 | if (static_cast<bool>(nameTreeEntry)) |
| 117 | return nameTreeEntry->getMeasurementsEntry(); |
| 118 | return shared_ptr<measurements::Entry>(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 122 | Measurements::extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 123 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 124 | shared_ptr<measurements::Entry> ret = this->findExactMatch(entry.getName()); |
| 125 | if (static_cast<bool>(ret)) |
| 126 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 127 | time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime; |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 128 | if (ret->m_expiry >= expiry) // has longer lifetime, not extending |
| 129 | return; |
| 130 | scheduler::cancel(entry.m_cleanup); |
| 131 | entry.m_expiry = expiry; |
| 132 | entry.m_cleanup = scheduler::schedule(lifetime, |
| 133 | bind(&Measurements::cleanup, this, ret)); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | Measurements::cleanup(shared_ptr<measurements::Entry> entry) |
| 139 | { |
| 140 | BOOST_ASSERT(entry); |
| 141 | |
| 142 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry->getName()); |
| 143 | if (static_cast<bool>(nameTreeEntry)) |
| 144 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 145 | nameTreeEntry->setMeasurementsEntry(shared_ptr<measurements::Entry>()); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 146 | m_nItems--; |
| 147 | } |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 148 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | } // namespace nfd |