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