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 | 80f9fcd | 2016-07-23 02:48:36 +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 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_MEASUREMENTS_HPP |
| 27 | #define NFD_DAEMON_TABLE_MEASUREMENTS_HPP |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 28 | |
| 29 | #include "measurements-entry.hpp" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 30 | #include "name-tree.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 34 | namespace fib { |
| 35 | class Entry; |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 36 | } // namespace fib |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 37 | |
| 38 | namespace pit { |
| 39 | class Entry; |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 40 | } // namespace pit |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 41 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 42 | namespace measurements { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 43 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 44 | /** \brief a predicate that accepts or rejects an entry |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 45 | */ |
| 46 | typedef std::function<bool(const Entry&)> EntryPredicate; |
| 47 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 48 | /** \brief an \p EntryPredicate that accepts any entry |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 49 | */ |
| 50 | class AnyEntry |
| 51 | { |
| 52 | public: |
| 53 | bool |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 54 | operator()(const Entry& entry) const |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 55 | { |
| 56 | return true; |
| 57 | } |
| 58 | }; |
| 59 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 60 | /** \brief an \p EntryPredicate that accepts an entry if it has StrategyInfo of type T |
| 61 | */ |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 62 | template<typename T> |
| 63 | class EntryWithStrategyInfo |
| 64 | { |
| 65 | public: |
| 66 | bool |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 67 | operator()(const Entry& entry) const |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 68 | { |
| 69 | return entry.getStrategyInfo<T>() != nullptr; |
| 70 | } |
| 71 | }; |
| 72 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 73 | /** \brief represents the Measurements table |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 74 | */ |
| 75 | class Measurements : noncopyable |
| 76 | { |
| 77 | public: |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 78 | explicit |
| 79 | Measurements(NameTree& nametree); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 80 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 81 | /** \brief find or insert a Measurements entry for \p name |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 82 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 83 | Entry& |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 84 | get(const Name& name); |
| 85 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 86 | /** \brief find or insert a Measurements entry for \p fibEntry.getPrefix() |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 87 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 88 | Entry& |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 89 | get(const fib::Entry& fibEntry); |
| 90 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 91 | /** \brief find or insert a Measurements entry for \p pitEntry.getName() |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 92 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 93 | Entry& |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 94 | get(const pit::Entry& pitEntry); |
| 95 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 96 | /** \brief find or insert a Measurements entry for child's parent |
| 97 | * \retval nullptr if child is the root entry |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 98 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 99 | Entry* |
| 100 | getParent(const Entry& child); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 101 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 102 | /** \brief perform a longest prefix match for \p name |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 103 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 104 | Entry* |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 105 | findLongestPrefixMatch(const Name& name, |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 106 | const EntryPredicate& pred = AnyEntry()) const; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 108 | /** \brief perform a longest prefix match for \p pitEntry.getName() |
| 109 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 110 | Entry* |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 111 | findLongestPrefixMatch(const pit::Entry& pitEntry, |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 112 | const EntryPredicate& pred = AnyEntry()) const; |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 114 | /** \brief perform an exact match |
| 115 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 116 | Entry* |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 117 | findExactMatch(const Name& name) const; |
| 118 | |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 119 | static time::nanoseconds |
| 120 | getInitialLifetime(); |
| 121 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 122 | /** \brief extend lifetime of an entry |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 123 | * |
| 124 | * The entry will be kept until at least now()+lifetime. |
| 125 | */ |
| 126 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 127 | extendLifetime(Entry& entry, const time::nanoseconds& lifetime); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 128 | |
| 129 | size_t |
| 130 | size() const; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 131 | |
| 132 | private: |
| 133 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 134 | cleanup(Entry& entry); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 135 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 136 | Entry& |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 137 | get(name_tree::Entry& nte); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 138 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 139 | /** \tparam K Name or shared_ptr<name_tree::Entry> |
| 140 | */ |
| 141 | template<typename K> |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 142 | Entry* |
| 143 | findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const; |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 144 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 145 | private: |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 146 | NameTree& m_nameTree; |
| 147 | size_t m_nItems; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 150 | inline time::nanoseconds |
| 151 | Measurements::getInitialLifetime() |
| 152 | { |
| 153 | return time::seconds(4); |
| 154 | } |
| 155 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 156 | inline size_t |
| 157 | Measurements::size() const |
| 158 | { |
| 159 | return m_nItems; |
| 160 | } |
| 161 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 162 | } // namespace measurements |
| 163 | |
| 164 | using measurements::Measurements; |
| 165 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 166 | } // namespace nfd |
| 167 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 168 | #endif // NFD_DAEMON_TABLE_MEASUREMENTS_HPP |