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 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 32 | #include <boost/range/adaptor/transformed.hpp> |
| 33 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 34 | namespace nfd { |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 35 | namespace strategy_choice { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 37 | /** \brief represents the Strategy Choice table |
| 38 | * |
| 39 | * The Strategy Choice table maintains available Strategy types, |
| 40 | * and associates Name prefixes with Strategy types. |
| 41 | * |
| 42 | * Each strategy is identified by a strategyName. |
| 43 | * It's recommended to include a version number as the last component of strategyName. |
| 44 | * |
| 45 | * A Name prefix is owned by a strategy if a longest prefix match on the |
| 46 | * Strategy Choice table returns that strategy. |
| 47 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 48 | class StrategyChoice : noncopyable |
| 49 | { |
| 50 | public: |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 51 | StrategyChoice(NameTree& nameTree, unique_ptr<fw::Strategy> defaultStrategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 53 | size_t |
| 54 | size() const |
| 55 | { |
| 56 | return m_nItems; |
| 57 | } |
| 58 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 59 | public: // available Strategy types |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 60 | /** \brief determines if a strategy is installed |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 61 | * \param strategyName name of the strategy |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 62 | * \param isExact true to require exact match, false to permit unversioned strategyName |
| 63 | * \return true if strategy is installed |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 64 | */ |
| 65 | bool |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 66 | hasStrategy(const Name& strategyName, bool isExact = false) const; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 67 | |
| 68 | /** \brief install a strategy |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 69 | * \return if installed, true, and a pointer to the strategy instance; |
| 70 | * if not installed due to duplicate strategyName, false, |
| 71 | * and a pointer to the existing strategy instance |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 72 | */ |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 73 | std::pair<bool, fw::Strategy*> |
| 74 | install(unique_ptr<fw::Strategy> strategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 75 | |
| 76 | public: // Strategy Choice table |
| 77 | /** \brief set strategy of prefix to be strategyName |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 78 | * \param prefix the name prefix for which \p strategyName should be used |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 79 | * \param strategyName the strategy to be used |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 80 | * \return true on success |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 81 | * |
| 82 | * This method set a strategy onto a Name prefix. |
| 83 | * The strategy must have been installed. |
| 84 | * The strategyName can either be exact (contains version component), |
| 85 | * or omit the version component to pick the latest version. |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 86 | */ |
| 87 | bool |
| 88 | insert(const Name& prefix, const Name& strategyName); |
| 89 | |
| 90 | /** \brief make prefix to inherit strategy from its parent |
| 91 | * |
| 92 | * not allowed for root prefix (ndn:/) |
| 93 | */ |
| 94 | void |
| 95 | erase(const Name& prefix); |
| 96 | |
| 97 | /** \brief get strategy Name of prefix |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 98 | * \return true and strategyName at exact match, or false |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 99 | */ |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 100 | std::pair<bool, Name> |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 101 | get(const Name& prefix) const; |
| 102 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 103 | public: // effective strategy |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 104 | /** \brief get effective strategy for prefix |
| 105 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 106 | fw::Strategy& |
| 107 | findEffectiveStrategy(const Name& prefix) const; |
| 108 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 109 | /** \brief get effective strategy for pitEntry |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 110 | * |
| 111 | * This is equivalent to .findEffectiveStrategy(pitEntry.getName()) |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 112 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 113 | fw::Strategy& |
| 114 | findEffectiveStrategy(const pit::Entry& pitEntry) const; |
| 115 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 116 | /** \brief get effective strategy for measurementsEntry |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 117 | * |
| 118 | * This is equivalent to .findEffectiveStrategy(measurementsEntry.getName()) |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 119 | */ |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 120 | fw::Strategy& |
| 121 | findEffectiveStrategy(const measurements::Entry& measurementsEntry) const; |
| 122 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 123 | public: // enumeration |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 124 | typedef boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range> Range; |
| 125 | typedef boost::range_iterator<Range>::type const_iterator; |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 126 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 127 | /** \return an iterator to the beginning |
| 128 | * \note Iteration order is implementation-defined. |
| 129 | * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry |
| 130 | * is inserted or erased during enumeration. |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 131 | */ |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 132 | const_iterator |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 133 | begin() const |
| 134 | { |
| 135 | return this->getRange().begin(); |
| 136 | } |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 138 | /** \return an iterator to the end |
| 139 | * \sa begin() |
| 140 | */ |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 141 | const_iterator |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 142 | end() const |
| 143 | { |
| 144 | return this->getRange().end(); |
| 145 | } |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 146 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 147 | private: |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 148 | /** \brief get Strategy instance by strategyName |
| 149 | * \param strategyName a versioned or unversioned strategyName |
| 150 | */ |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 151 | fw::Strategy* |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 152 | getStrategy(const Name& strategyName) const; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 153 | |
| 154 | void |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 155 | setDefaultStrategy(unique_ptr<fw::Strategy> strategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 156 | |
| 157 | void |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 158 | changeStrategy(Entry& entry, |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 159 | fw::Strategy& oldStrategy, |
| 160 | fw::Strategy& newStrategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 162 | /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch |
| 163 | */ |
| 164 | template<typename K> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 165 | fw::Strategy& |
Junxiao Shi | dd8f661 | 2016-08-12 15:42:52 +0000 | [diff] [blame] | 166 | findEffectiveStrategyImpl(const K& key) const; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 167 | |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame^] | 168 | Range |
| 169 | getRange() const; |
| 170 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 171 | private: |
| 172 | NameTree& m_nameTree; |
| 173 | size_t m_nItems; |
| 174 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 175 | typedef std::map<Name, unique_ptr<fw::Strategy>> StrategyInstanceTable; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 176 | StrategyInstanceTable m_strategyInstances; |
| 177 | }; |
| 178 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 179 | } // namespace strategy_choice |
| 180 | |
| 181 | using strategy_choice::StrategyChoice; |
| 182 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 183 | } // namespace nfd |
| 184 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 185 | #endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP |