Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | 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 | |
| 32 | namespace nfd { |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 33 | namespace strategy_choice { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 34 | |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 35 | /** \brief represents the Strategy Choice table |
| 36 | * |
| 37 | * The Strategy Choice table maintains available Strategy types, |
| 38 | * and associates Name prefixes with Strategy types. |
| 39 | * |
| 40 | * Each strategy is identified by a strategyName. |
| 41 | * It's recommended to include a version number as the last component of strategyName. |
| 42 | * |
| 43 | * A Name prefix is owned by a strategy if a longest prefix match on the |
| 44 | * Strategy Choice table returns that strategy. |
| 45 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 46 | class StrategyChoice : noncopyable |
| 47 | { |
| 48 | public: |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 49 | StrategyChoice(NameTree& nameTree, unique_ptr<fw::Strategy> defaultStrategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 50 | |
| 51 | public: // available Strategy types |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 52 | /** \brief determines if a strategy is installed |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 53 | * \param strategyName name of the strategy |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 54 | * \param isExact true to require exact match, false to permit unversioned strategyName |
| 55 | * \return true if strategy is installed |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 56 | */ |
| 57 | bool |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 58 | hasStrategy(const Name& strategyName, bool isExact = false) const; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 59 | |
| 60 | /** \brief install a strategy |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 61 | * \return if installed, true, and a pointer to the strategy instance; |
| 62 | * if not installed due to duplicate strategyName, false, |
| 63 | * and a pointer to the existing strategy instance |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 64 | */ |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 65 | std::pair<bool, fw::Strategy*> |
| 66 | install(unique_ptr<fw::Strategy> strategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 67 | |
| 68 | public: // Strategy Choice table |
| 69 | /** \brief set strategy of prefix to be strategyName |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 70 | * \param prefix the name prefix for which \p strategyName should be used |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 71 | * \param strategyName the strategy to be used |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 72 | * \return true on success |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 73 | * |
| 74 | * This method set a strategy onto a Name prefix. |
| 75 | * The strategy must have been installed. |
| 76 | * The strategyName can either be exact (contains version component), |
| 77 | * or omit the version component to pick the latest version. |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 78 | */ |
| 79 | bool |
| 80 | insert(const Name& prefix, const Name& strategyName); |
| 81 | |
| 82 | /** \brief make prefix to inherit strategy from its parent |
| 83 | * |
| 84 | * not allowed for root prefix (ndn:/) |
| 85 | */ |
| 86 | void |
| 87 | erase(const Name& prefix); |
| 88 | |
| 89 | /** \brief get strategy Name of prefix |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 90 | * \return true and strategyName at exact match, or false |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 91 | */ |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 92 | std::pair<bool, Name> |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 93 | get(const Name& prefix) const; |
| 94 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 95 | public: // effective strategy |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 96 | /** \brief get effective strategy for prefix |
| 97 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 98 | fw::Strategy& |
| 99 | findEffectiveStrategy(const Name& prefix) const; |
| 100 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 101 | /** \brief get effective strategy for pitEntry |
| 102 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 103 | fw::Strategy& |
| 104 | findEffectiveStrategy(const pit::Entry& pitEntry) const; |
| 105 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 106 | /** \brief get effective strategy for measurementsEntry |
| 107 | */ |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 108 | fw::Strategy& |
| 109 | findEffectiveStrategy(const measurements::Entry& measurementsEntry) const; |
| 110 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 111 | public: // enumeration |
| 112 | class const_iterator |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 113 | : public std::iterator<std::forward_iterator_tag, const Entry> |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 114 | { |
| 115 | public: |
| 116 | explicit |
| 117 | const_iterator(const NameTree::const_iterator& it); |
| 118 | |
| 119 | ~const_iterator(); |
| 120 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 121 | const Entry& |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 122 | operator*() const; |
| 123 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 124 | const Entry* |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 125 | operator->() const; |
| 126 | |
| 127 | const_iterator& |
| 128 | operator++(); |
| 129 | |
| 130 | const_iterator |
| 131 | operator++(int); |
| 132 | |
| 133 | bool |
| 134 | operator==(const const_iterator& other) const; |
| 135 | |
| 136 | bool |
| 137 | operator!=(const const_iterator& other) const; |
| 138 | |
| 139 | private: |
| 140 | NameTree::const_iterator m_nameTreeIterator; |
| 141 | }; |
| 142 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 143 | /** \return number of entries stored |
| 144 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 145 | size_t |
| 146 | size() const; |
| 147 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 148 | const_iterator |
| 149 | begin() const; |
| 150 | |
| 151 | const_iterator |
| 152 | end() const; |
| 153 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 154 | private: |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 155 | /** \brief get Strategy instance by strategyName |
| 156 | * \param strategyName a versioned or unversioned strategyName |
| 157 | */ |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 158 | fw::Strategy* |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 159 | getStrategy(const Name& strategyName) const; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 160 | |
| 161 | void |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 162 | setDefaultStrategy(unique_ptr<fw::Strategy> strategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 163 | |
| 164 | void |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 165 | changeStrategy(Entry& entry, |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 166 | fw::Strategy& oldStrategy, |
| 167 | fw::Strategy& newStrategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 168 | |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 169 | /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch |
| 170 | */ |
| 171 | template<typename K> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 172 | fw::Strategy& |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 173 | findEffectiveStrategyImpl(const K& key) const; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 174 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 175 | private: |
| 176 | NameTree& m_nameTree; |
| 177 | size_t m_nItems; |
| 178 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 179 | typedef std::map<Name, unique_ptr<fw::Strategy>> StrategyInstanceTable; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 180 | StrategyInstanceTable m_strategyInstances; |
| 181 | }; |
| 182 | |
| 183 | inline size_t |
| 184 | StrategyChoice::size() const |
| 185 | { |
| 186 | return m_nItems; |
| 187 | } |
| 188 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 189 | inline StrategyChoice::const_iterator |
| 190 | StrategyChoice::end() const |
| 191 | { |
| 192 | return const_iterator(m_nameTree.end()); |
| 193 | } |
| 194 | |
| 195 | inline |
| 196 | StrategyChoice::const_iterator::const_iterator(const NameTree::const_iterator& it) |
| 197 | : m_nameTreeIterator(it) |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | inline |
| 202 | StrategyChoice::const_iterator::~const_iterator() |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | inline |
| 207 | StrategyChoice::const_iterator |
| 208 | StrategyChoice::const_iterator::operator++(int) |
| 209 | { |
| 210 | StrategyChoice::const_iterator temp(*this); |
| 211 | ++(*this); |
| 212 | return temp; |
| 213 | } |
| 214 | |
| 215 | inline StrategyChoice::const_iterator& |
| 216 | StrategyChoice::const_iterator::operator++() |
| 217 | { |
| 218 | ++m_nameTreeIterator; |
| 219 | return *this; |
| 220 | } |
| 221 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 222 | inline const Entry& |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 223 | StrategyChoice::const_iterator::operator*() const |
| 224 | { |
| 225 | return *(m_nameTreeIterator->getStrategyChoiceEntry()); |
| 226 | } |
| 227 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 228 | inline const Entry* |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 229 | StrategyChoice::const_iterator::operator->() const |
| 230 | { |
| 231 | return m_nameTreeIterator->getStrategyChoiceEntry(); |
| 232 | } |
| 233 | |
| 234 | inline bool |
| 235 | StrategyChoice::const_iterator::operator==(const StrategyChoice::const_iterator& other) const |
| 236 | { |
| 237 | return m_nameTreeIterator == other.m_nameTreeIterator; |
| 238 | } |
| 239 | |
| 240 | inline bool |
| 241 | StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& other) const |
| 242 | { |
| 243 | return m_nameTreeIterator != other.m_nameTreeIterator; |
| 244 | } |
| 245 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 246 | } // namespace strategy_choice |
| 247 | |
| 248 | using strategy_choice::StrategyChoice; |
| 249 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 250 | } // namespace nfd |
| 251 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 252 | #endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP |