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 | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 6 | * University Pierre & Marie Curie, Sorbonne University, |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 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 { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 32 | namespace measurements { |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -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 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 40 | Entry& |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 41 | Measurements::get(name_tree::Entry& nte) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 42 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 43 | Entry* entry = nte.getMeasurementsEntry(); |
| 44 | if (entry != nullptr) { |
| 45 | return *entry; |
| 46 | } |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 48 | nte.setMeasurementsEntry(make_unique<Entry>(nte.getPrefix())); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 49 | ++m_nItems; |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 50 | entry = nte.getMeasurementsEntry(); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 51 | |
| 52 | entry->m_expiry = time::steady_clock::now() + getInitialLifetime(); |
| 53 | entry->m_cleanup = scheduler::schedule(getInitialLifetime(), |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 54 | bind(&Measurements::cleanup, this, ref(*entry))); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 56 | return *entry; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 59 | Entry& |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 60 | Measurements::get(const Name& name) |
| 61 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 62 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 63 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 64 | return this->get(*nte); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 65 | } |
| 66 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 67 | Entry& |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 68 | Measurements::get(const fib::Entry& fibEntry) |
| 69 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 70 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(fibEntry); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 71 | if (nte == nullptr) { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 72 | // must be Fib::s_emptyEntry that is unattached |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 73 | BOOST_ASSERT(fibEntry.getPrefix().empty()); |
| 74 | nte = m_nameTree.lookup(fibEntry.getPrefix()); |
| 75 | } |
| 76 | |
| 77 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 78 | return this->get(*nte); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 81 | Entry& |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 82 | Measurements::get(const pit::Entry& pitEntry) |
| 83 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 84 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame] | 85 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 86 | return this->get(*nte); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 89 | Entry* |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 90 | Measurements::getParent(const Entry& child) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 91 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 92 | if (child.getName().empty()) { // the root entry |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 93 | return nullptr; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 96 | shared_ptr<name_tree::Entry> nteChild = m_nameTree.lookup(child); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 97 | name_tree::Entry* nte = nteChild->getParent(); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 98 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 99 | return &this->get(*nte); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 102 | template<typename K> |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 103 | Entry* |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 104 | Measurements::findLongestPrefixMatchImpl(const K& key, |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 105 | const EntryPredicate& pred) const |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 106 | { |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 107 | shared_ptr<name_tree::Entry> match = m_nameTree.findLongestPrefixMatch(key, |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 108 | [&pred] (const name_tree::Entry& nte) -> bool { |
| 109 | Entry* entry = nte.getMeasurementsEntry(); |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 110 | return entry != nullptr && pred(*entry); |
| 111 | }); |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 112 | if (match != nullptr) { |
| 113 | return match->getMeasurementsEntry(); |
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 | return nullptr; |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 116 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 117 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 118 | Entry* |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 119 | Measurements::findLongestPrefixMatch(const Name& name, |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 120 | const EntryPredicate& pred) const |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 121 | { |
| 122 | return this->findLongestPrefixMatchImpl(name, pred); |
| 123 | } |
| 124 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 125 | Entry* |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 126 | Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry, |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 127 | const EntryPredicate& pred) const |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 128 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 129 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry); |
| 130 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 131 | return this->findLongestPrefixMatchImpl(nte, pred); |
| 132 | } |
| 133 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 134 | Entry* |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 135 | Measurements::findExactMatch(const Name& name) const |
| 136 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 137 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 138 | if (nte != nullptr) { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 139 | return nte->getMeasurementsEntry(); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 140 | } |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 141 | return nullptr; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 145 | Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 146 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 147 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 148 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 149 | |
| 150 | time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime; |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 151 | if (entry.m_expiry >= expiry) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 152 | // has longer lifetime, not extending |
| 153 | return; |
| 154 | } |
| 155 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 156 | scheduler::cancel(entry.m_cleanup); |
| 157 | entry.m_expiry = expiry; |
| 158 | entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry))); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 162 | Measurements::cleanup(Entry& entry) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 163 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 164 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 165 | BOOST_ASSERT(nte != nullptr); |
| 166 | |
| 167 | nte->setMeasurementsEntry(nullptr); |
| 168 | m_nameTree.eraseEntryIfEmpty(nte); |
| 169 | --m_nItems; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 172 | } // namespace measurements |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 173 | } // namespace nfd |