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