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 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame^] | 103 | shared_ptr<name_tree::Entry> nameTreeChild = m_nameTree.get(*child); |
| 104 | shared_ptr<name_tree::Entry> nameTreeEntry = nameTreeChild->getParent(); |
| 105 | if (static_cast<bool>(nameTreeEntry)) { |
| 106 | return this->get(nameTreeEntry); |
| 107 | } |
| 108 | else { |
| 109 | BOOST_ASSERT(nameTreeChild->getPrefix().size() == 0); // root entry has no parent |
| 110 | return shared_ptr<measurements::Entry>(); |
| 111 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 112 | } |
| 113 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 114 | shared_ptr<measurements::Entry> |
| 115 | Measurements::findLongestPrefixMatch(const Name& name) const |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 116 | { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 117 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 118 | m_nameTree.findLongestPrefixMatch(name, &predicate_NameTreeEntry_hasMeasurementsEntry); |
| 119 | if (static_cast<bool>(nameTreeEntry)) { |
| 120 | return nameTreeEntry->getMeasurementsEntry(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 121 | } |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 122 | return shared_ptr<measurements::Entry>(); |
| 123 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 124 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 125 | shared_ptr<measurements::Entry> |
| 126 | Measurements::findExactMatch(const Name& name) const |
| 127 | { |
| 128 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name); |
| 129 | if (static_cast<bool>(nameTreeEntry)) |
| 130 | return nameTreeEntry->getMeasurementsEntry(); |
| 131 | return shared_ptr<measurements::Entry>(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame^] | 135 | Measurements::extendLifetime(shared_ptr<measurements::Entry> entry, |
| 136 | const time::nanoseconds& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 137 | { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame^] | 138 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*entry); |
| 139 | if (!static_cast<bool>(nameTreeEntry) || |
| 140 | nameTreeEntry->getMeasurementsEntry().get() != entry.get()) { |
| 141 | // entry is already gone; it is a dangling reference |
| 142 | return; |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 143 | } |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame^] | 144 | |
| 145 | time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime; |
| 146 | if (entry->m_expiry >= expiry) { |
| 147 | // has longer lifetime, not extending |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | scheduler::cancel(entry->m_cleanup); |
| 152 | entry->m_expiry = expiry; |
| 153 | entry->m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, entry)); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
| 157 | Measurements::cleanup(shared_ptr<measurements::Entry> entry) |
| 158 | { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame^] | 159 | BOOST_ASSERT(static_cast<bool>(entry)); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 160 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame^] | 161 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*entry); |
| 162 | if (static_cast<bool>(nameTreeEntry)) { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 163 | nameTreeEntry->setMeasurementsEntry(shared_ptr<measurements::Entry>()); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame^] | 164 | m_nameTree.eraseEntryIfEmpty(nameTreeEntry); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 165 | m_nItems--; |
| 166 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } // namespace nfd |