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