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