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