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 | b184e53 | 2016-05-26 18:09:57 +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 | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 25 | |
| 26 | #include "strategy-choice.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 28 | #include "fw/strategy.hpp" |
| 29 | #include "pit-entry.hpp" |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 30 | #include "measurements-entry.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 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 | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 35 | using fw::Strategy; |
| 36 | |
| 37 | NFD_LOG_INIT("StrategyChoice"); |
| 38 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 39 | StrategyChoice::StrategyChoice(NameTree& nameTree, unique_ptr<Strategy> defaultStrategy) |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 40 | : m_nameTree(nameTree) |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 41 | , m_nItems(0) |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 42 | { |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 43 | this->setDefaultStrategy(std::move(defaultStrategy)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | bool |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 47 | StrategyChoice::hasStrategy(const Name& strategyName, bool isExact) const |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 48 | { |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 49 | if (isExact) { |
| 50 | return m_strategyInstances.count(strategyName) > 0; |
| 51 | } |
| 52 | else { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 53 | return this->getStrategy(strategyName) != nullptr; |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 54 | } |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 57 | std::pair<bool, Strategy*> |
| 58 | StrategyChoice::install(unique_ptr<Strategy> strategy) |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 59 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 60 | BOOST_ASSERT(strategy != nullptr); |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 61 | Name strategyName = strategy->getName(); |
| 62 | // copying Name, so that strategyName remains available even if strategy is deallocated |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 63 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 64 | bool isInserted = false; |
| 65 | StrategyInstanceTable::iterator it; |
| 66 | std::tie(it, isInserted) = m_strategyInstances.emplace(strategyName, std::move(strategy)); |
| 67 | |
| 68 | if (!isInserted) { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 69 | NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName"); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 70 | } |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 71 | return std::make_pair(isInserted, it->second.get()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 74 | Strategy* |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 75 | StrategyChoice::getStrategy(const Name& strategyName) const |
| 76 | { |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 77 | Strategy* candidate = nullptr; |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 78 | for (auto it = m_strategyInstances.lower_bound(strategyName); |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 79 | it != m_strategyInstances.end() && strategyName.isPrefixOf(it->first); ++it) { |
| 80 | switch (it->first.size() - strategyName.size()) { |
| 81 | case 0: // exact match |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 82 | return it->second.get(); |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 83 | case 1: // unversioned strategyName matches versioned strategy |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 84 | candidate = it->second.get(); |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | } |
| 88 | return candidate; |
| 89 | } |
| 90 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 91 | bool |
| 92 | StrategyChoice::insert(const Name& prefix, const Name& strategyName) |
| 93 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 94 | Strategy* strategy = this->getStrategy(strategyName); |
| 95 | if (strategy == nullptr) { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 96 | NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed"); |
| 97 | return false; |
| 98 | } |
| 99 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 100 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(prefix); |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 101 | Entry* entry = nte->getStrategyChoiceEntry(); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 102 | Strategy* oldStrategy = nullptr; |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 103 | if (entry != nullptr) { |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 104 | if (entry->getStrategy().getName() == strategy->getName()) { |
| 105 | NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getName()); |
| 106 | return true; |
| 107 | } |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 108 | oldStrategy = &entry->getStrategy(); |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 109 | NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getName() << |
| 110 | " to " << strategy->getName()); |
| 111 | } |
| 112 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 113 | if (entry == nullptr) { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 114 | oldStrategy = &this->findEffectiveStrategy(prefix); |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 115 | auto newEntry = make_unique<Entry>(prefix); |
| 116 | entry = newEntry.get(); |
| 117 | nte->setStrategyChoiceEntry(std::move(newEntry)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 118 | ++m_nItems; |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 119 | NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 122 | this->changeStrategy(*entry, *oldStrategy, *strategy); |
| 123 | entry->setStrategy(*strategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | StrategyChoice::erase(const Name& prefix) |
| 129 | { |
| 130 | BOOST_ASSERT(prefix.size() > 0); |
| 131 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 132 | shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 133 | if (nte == nullptr) { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 137 | Entry* entry = nte->getStrategyChoiceEntry(); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 138 | if (entry == nullptr) { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
| 142 | Strategy& oldStrategy = entry->getStrategy(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 143 | |
| 144 | Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1)); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 145 | this->changeStrategy(*entry, oldStrategy, parentStrategy); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 146 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 147 | nte->setStrategyChoiceEntry(nullptr); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 148 | m_nameTree.eraseEntryIfEmpty(nte); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 149 | --m_nItems; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 152 | std::pair<bool, Name> |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 153 | StrategyChoice::get(const Name& prefix) const |
| 154 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 155 | shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 156 | if (nte == nullptr) { |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 157 | return {false, Name()}; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 160 | Entry* entry = nte->getStrategyChoiceEntry(); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 161 | if (entry == nullptr) { |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 162 | return {false, Name()}; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 165 | return {true, entry->getStrategy().getName()}; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | Strategy& |
| 169 | StrategyChoice::findEffectiveStrategy(const Name& prefix) const |
| 170 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 171 | shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(prefix, |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 172 | [] (const name_tree::Entry& entry) { return entry.getStrategyChoiceEntry() != nullptr; }); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 173 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 174 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 175 | return nte->getStrategyChoiceEntry()->getStrategy(); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | Strategy& |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 179 | StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nte) const |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 180 | { |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 181 | Entry* entry = nte->getStrategyChoiceEntry(); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 182 | if (entry != nullptr) |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 183 | return entry->getStrategy(); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 184 | |
| 185 | nte = m_nameTree.findLongestPrefixMatch(nte, |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 186 | [] (const name_tree::Entry& entry) { return entry.getStrategyChoiceEntry() != nullptr; }); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 187 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 188 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 189 | return nte->getStrategyChoiceEntry()->getStrategy(); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | Strategy& |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 193 | StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const |
| 194 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 195 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry); |
| 196 | BOOST_ASSERT(nte != nullptr); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 197 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 198 | return this->findEffectiveStrategy(nte); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 201 | Strategy& |
| 202 | StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const |
| 203 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 204 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(measurementsEntry); |
| 205 | BOOST_ASSERT(nte != nullptr); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 206 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 207 | return this->findEffectiveStrategy(nte); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 210 | void |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 211 | StrategyChoice::setDefaultStrategy(unique_ptr<Strategy> strategy) |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 212 | { |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 213 | bool isInstalled = false; |
| 214 | Strategy* instance = nullptr; |
| 215 | std::tie(isInstalled, instance) = this->install(std::move(strategy)); |
| 216 | BOOST_ASSERT(isInstalled); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 217 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 218 | auto entry = make_unique<Entry>(Name()); |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 219 | entry->setStrategy(*instance); |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 220 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 221 | // don't use .insert here, because it will invoke findEffectiveStrategy |
| 222 | // which expects an existing root entry |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 223 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(Name()); |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 224 | nte->setStrategyChoiceEntry(std::move(entry)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 225 | ++m_nItems; |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 226 | NFD_LOG_INFO("setDefaultStrategy " << instance->getName()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 229 | static inline void |
| 230 | clearStrategyInfo(const name_tree::Entry& nte) |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 231 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 232 | NFD_LOG_TRACE("clearStrategyInfo " << nte.getPrefix()); |
| 233 | |
| 234 | for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) { |
| 235 | pitEntry->clearStrategyInfo(); |
| 236 | for (const pit::InRecord& inRecord : pitEntry->getInRecords()) { |
| 237 | const_cast<pit::InRecord&>(inRecord).clearStrategyInfo(); |
| 238 | } |
| 239 | for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) { |
| 240 | const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo(); |
| 241 | } |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 242 | } |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 243 | if (nte.getMeasurementsEntry() != nullptr) { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 244 | nte.getMeasurementsEntry()->clearStrategyInfo(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 248 | void |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 249 | StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy) |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 250 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 251 | if (&oldStrategy == &newStrategy) { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 255 | NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ")" |
| 256 | << " from " << oldStrategy.getName() |
| 257 | << " to " << newStrategy.getName()); |
| 258 | |
| 259 | // reset StrategyInfo on a portion of NameTree, |
| 260 | // where entry's effective strategy is covered by the changing StrategyChoice entry |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 261 | const name_tree::Entry* rootNte = m_nameTree.lookup(entry).get(); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 262 | auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(), |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 263 | [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> { |
| 264 | if (&nte == rootNte) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 265 | return {true, true}; |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 266 | } |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 267 | if (nte.getStrategyChoiceEntry() != nullptr) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 268 | return {false, false}; |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 269 | } |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 270 | return {true, true}; |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 271 | }); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 272 | for (const name_tree::Entry& nte : ntChanged) { |
| 273 | clearStrategyInfo(nte); |
| 274 | } |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 277 | StrategyChoice::const_iterator |
| 278 | StrategyChoice::begin() const |
| 279 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 280 | auto&& enumerable = m_nameTree.fullEnumerate( |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 281 | [] (const name_tree::Entry& entry) { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 282 | return entry.getStrategyChoiceEntry() != nullptr; |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 283 | }); |
| 284 | return const_iterator(enumerable.begin()); |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 287 | } // namespace strategy_choice |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 288 | } // namespace nfd |