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