Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 4e837f8 | 2017-10-02 22:14:43 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 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 | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP |
| 27 | #define NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 28 | |
| 29 | #include "strategy-choice-entry.hpp" |
| 30 | #include "name-tree.hpp" |
| 31 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 32 | #include <boost/range/adaptor/transformed.hpp> |
| 33 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 34 | namespace nfd { |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 35 | |
| 36 | class Forwarder; |
| 37 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 38 | namespace strategy_choice { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 39 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 40 | /** \brief Represents the Strategy Choice table |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 41 | * |
| 42 | * The Strategy Choice table maintains available Strategy types, |
| 43 | * and associates Name prefixes with Strategy types. |
| 44 | * |
| 45 | * Each strategy is identified by a strategyName. |
| 46 | * It's recommended to include a version number as the last component of strategyName. |
| 47 | * |
| 48 | * A Name prefix is owned by a strategy if a longest prefix match on the |
| 49 | * Strategy Choice table returns that strategy. |
| 50 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 51 | class StrategyChoice : noncopyable |
| 52 | { |
| 53 | public: |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 54 | explicit |
| 55 | StrategyChoice(Forwarder& forwarder); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 56 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 57 | size_t |
| 58 | size() const |
| 59 | { |
| 60 | return m_nItems; |
| 61 | } |
| 62 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 63 | /** \brief Set the default strategy |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 64 | * |
| 65 | * This must be called by forwarder constructor. |
| 66 | */ |
| 67 | void |
| 68 | setDefaultStrategy(const Name& strategyName); |
| 69 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 70 | public: // Strategy Choice table |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 71 | class InsertResult |
| 72 | { |
| 73 | public: |
| 74 | explicit |
| 75 | operator bool() const |
| 76 | { |
| 77 | return m_status == OK; |
| 78 | } |
| 79 | |
| 80 | bool |
| 81 | isRegistered() const |
| 82 | { |
Junxiao Shi | 4e837f8 | 2017-10-02 22:14:43 +0000 | [diff] [blame] | 83 | return m_status == OK || m_status == EXCEPTION; |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 86 | /** \brief Get a status code for use in management command response |
Junxiao Shi | 332783b | 2018-02-04 23:52:46 +0000 | [diff] [blame] | 87 | */ |
| 88 | int |
| 89 | getStatusCode() const |
| 90 | { |
| 91 | return static_cast<int>(m_status); |
| 92 | } |
| 93 | |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 94 | private: |
| 95 | enum Status { |
Junxiao Shi | 332783b | 2018-02-04 23:52:46 +0000 | [diff] [blame] | 96 | OK = 200, |
| 97 | NOT_REGISTERED = 404, |
| 98 | EXCEPTION = 409, |
| 99 | DEPTH_EXCEEDED = 414, |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | // implicitly constructible from Status |
| 103 | InsertResult(Status status, const std::string& exceptionMessage = ""); |
| 104 | |
| 105 | private: |
| 106 | Status m_status; |
| 107 | std::string m_exceptionMessage; |
| 108 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 109 | friend StrategyChoice; |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 110 | friend std::ostream& operator<<(std::ostream&, const InsertResult&); |
| 111 | }; |
| 112 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 113 | /** \brief Set strategy of \p prefix to be \p strategyName |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 114 | * \param prefix the name prefix to change strategy |
| 115 | * \param strategyName strategy instance name, may contain version and parameters; |
| 116 | * strategy must have been registered |
| 117 | * \return convertible to true on success; convertible to false on failure |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 118 | */ |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 119 | InsertResult |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 120 | insert(const Name& prefix, const Name& strategyName); |
| 121 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 122 | /** \brief Make prefix to inherit strategy from its parent |
| 123 | * \note Not allowed for root prefix (ndn:/) |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 124 | */ |
| 125 | void |
| 126 | erase(const Name& prefix); |
| 127 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 128 | /** \brief Get strategy Name of prefix |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 129 | * \return true and strategyName at exact match, or false |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 130 | */ |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 131 | std::pair<bool, Name> |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 132 | get(const Name& prefix) const; |
| 133 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 134 | public: // effective strategy |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 135 | /** \brief Get effective strategy for \p prefix |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 136 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 137 | fw::Strategy& |
| 138 | findEffectiveStrategy(const Name& prefix) const; |
| 139 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 140 | /** \brief Get effective strategy for \p pitEntry |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 141 | * |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 142 | * This is equivalent to `findEffectiveStrategy(pitEntry.getName())` |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 143 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 144 | fw::Strategy& |
| 145 | findEffectiveStrategy(const pit::Entry& pitEntry) const; |
| 146 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 147 | /** \brief Get effective strategy for \p measurementsEntry |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 148 | * |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 149 | * This is equivalent to `findEffectiveStrategy(measurementsEntry.getName())` |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 150 | */ |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 151 | fw::Strategy& |
| 152 | findEffectiveStrategy(const measurements::Entry& measurementsEntry) const; |
| 153 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 154 | public: // enumeration |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 155 | using Range = boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range>; |
| 156 | using const_iterator = boost::range_iterator<Range>::type; |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 158 | /** \return an iterator to the beginning |
| 159 | * \note Iteration order is implementation-defined. |
| 160 | * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry |
| 161 | * is inserted or erased during enumeration. |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 162 | */ |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 163 | const_iterator |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 164 | begin() const |
| 165 | { |
| 166 | return this->getRange().begin(); |
| 167 | } |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 168 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 169 | /** \return an iterator to the end |
| 170 | * \sa begin() |
| 171 | */ |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 172 | const_iterator |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 173 | end() const |
| 174 | { |
| 175 | return this->getRange().end(); |
| 176 | } |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 177 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 178 | private: |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 179 | void |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 180 | changeStrategy(Entry& entry, |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 181 | fw::Strategy& oldStrategy, |
| 182 | fw::Strategy& newStrategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 183 | |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 184 | /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch |
| 185 | */ |
| 186 | template<typename K> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 187 | fw::Strategy& |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 188 | findEffectiveStrategyImpl(const K& key) const; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 189 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 190 | Range |
| 191 | getRange() const; |
| 192 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 193 | private: |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 194 | Forwarder& m_forwarder; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 195 | NameTree& m_nameTree; |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 196 | size_t m_nItems = 0; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
Junxiao Shi | 530cf00 | 2017-01-03 14:43:16 +0000 | [diff] [blame] | 199 | std::ostream& |
| 200 | operator<<(std::ostream& os, const StrategyChoice::InsertResult& res); |
| 201 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 202 | } // namespace strategy_choice |
| 203 | |
| 204 | using strategy_choice::StrategyChoice; |
| 205 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 206 | } // namespace nfd |
| 207 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 208 | #endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP |