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 | 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 { |
| 33 | |
| 34 | using strategy_choice::Entry; |
| 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 { |
| 53 | return static_cast<bool>(this->getStrategy(strategyName)); |
| 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 | { |
| 60 | BOOST_ASSERT(static_cast<bool>(strategy)); |
| 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); |
| 99 | shared_ptr<Entry> entry = nte->getStrategyChoiceEntry(); |
| 100 | Strategy* oldStrategy = nullptr; |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 101 | if (static_cast<bool>(entry)) { |
| 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 | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 111 | if (!static_cast<bool>(entry)) { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 112 | oldStrategy = &this->findEffectiveStrategy(prefix); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 113 | entry = make_shared<Entry>(prefix); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 114 | nte->setStrategyChoiceEntry(entry); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 115 | ++m_nItems; |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 116 | NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 119 | this->changeStrategy(*entry, *oldStrategy, *strategy); |
| 120 | entry->setStrategy(*strategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | StrategyChoice::erase(const Name& prefix) |
| 126 | { |
| 127 | BOOST_ASSERT(prefix.size() > 0); |
| 128 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 129 | shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix); |
| 130 | if (!static_cast<bool>(nte)) { |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 134 | shared_ptr<Entry> entry = nte->getStrategyChoiceEntry(); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 135 | if (!static_cast<bool>(entry)) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | Strategy& oldStrategy = entry->getStrategy(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 140 | |
| 141 | Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1)); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 142 | this->changeStrategy(*entry, oldStrategy, parentStrategy); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 143 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 144 | nte->setStrategyChoiceEntry(shared_ptr<Entry>()); |
| 145 | m_nameTree.eraseEntryIfEmpty(nte); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 146 | --m_nItems; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 149 | std::pair<bool, Name> |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 150 | StrategyChoice::get(const Name& prefix) const |
| 151 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 152 | shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix); |
| 153 | if (!static_cast<bool>(nte)) { |
| 154 | return { false, Name() }; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 157 | shared_ptr<Entry> entry = nte->getStrategyChoiceEntry(); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 158 | if (!static_cast<bool>(entry)) { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 159 | return { false, Name() }; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 162 | return { true, entry->getStrategy().getName() }; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | Strategy& |
| 166 | StrategyChoice::findEffectiveStrategy(const Name& prefix) const |
| 167 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 168 | shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(prefix, |
| 169 | [] (const name_tree::Entry& entry) { |
| 170 | return static_cast<bool>(entry.getStrategyChoiceEntry()); |
| 171 | }); |
| 172 | |
| 173 | BOOST_ASSERT(static_cast<bool>(nte)); |
| 174 | return nte->getStrategyChoiceEntry()->getStrategy(); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | Strategy& |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 178 | StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nte) const |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 179 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 180 | shared_ptr<strategy_choice::Entry> entry = nte->getStrategyChoiceEntry(); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 181 | if (static_cast<bool>(entry)) |
| 182 | return entry->getStrategy(); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 183 | |
| 184 | nte = m_nameTree.findLongestPrefixMatch(nte, |
| 185 | [] (const name_tree::Entry& entry) { |
| 186 | return static_cast<bool>(entry.getStrategyChoiceEntry()); |
| 187 | }); |
| 188 | |
| 189 | BOOST_ASSERT(static_cast<bool>(nte)); |
| 190 | return nte->getStrategyChoiceEntry()->getStrategy(); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | Strategy& |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 194 | StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const |
| 195 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 196 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry); |
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 | BOOST_ASSERT(static_cast<bool>(nte)); |
| 199 | return this->findEffectiveStrategy(nte); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 202 | Strategy& |
| 203 | StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const |
| 204 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 205 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(measurementsEntry); |
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 | BOOST_ASSERT(static_cast<bool>(nte)); |
| 208 | return this->findEffectiveStrategy(nte); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 211 | void |
| 212 | StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy) |
| 213 | { |
| 214 | this->install(strategy); |
| 215 | |
| 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 | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 219 | shared_ptr<Entry> entry = make_shared<Entry>(Name()); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 220 | nte->setStrategyChoiceEntry(entry); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 221 | ++m_nItems; |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 222 | NFD_LOG_INFO("setDefaultStrategy " << strategy->getName()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 223 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 224 | entry->setStrategy(*strategy); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 227 | static inline void |
| 228 | clearStrategyInfo(const name_tree::Entry& nte) |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 229 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 230 | NFD_LOG_TRACE("clearStrategyInfo " << nte.getPrefix()); |
| 231 | |
| 232 | for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) { |
| 233 | pitEntry->clearStrategyInfo(); |
| 234 | for (const pit::InRecord& inRecord : pitEntry->getInRecords()) { |
| 235 | const_cast<pit::InRecord&>(inRecord).clearStrategyInfo(); |
| 236 | } |
| 237 | for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) { |
| 238 | const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo(); |
| 239 | } |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 240 | } |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 241 | if (static_cast<bool>(nte.getMeasurementsEntry())) { |
| 242 | nte.getMeasurementsEntry()->clearStrategyInfo(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 246 | void |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 247 | StrategyChoice::changeStrategy(strategy_choice::Entry& entry, |
| 248 | fw::Strategy& oldStrategy, |
| 249 | fw::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 |
| 261 | const name_tree::Entry* rootNte = m_nameTree.get(entry).get(); |
| 262 | auto ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(), |
| 263 | [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> { |
| 264 | if (&nte == rootNte) { |
| 265 | return { true, true }; |
| 266 | } |
| 267 | if (static_cast<bool>(nte.getStrategyChoiceEntry())) { |
| 268 | return { false, false }; |
| 269 | } |
| 270 | return { true, true }; |
| 271 | }); |
| 272 | std::for_each(ntChanged, m_nameTree.end(), &clearStrategyInfo); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 275 | StrategyChoice::const_iterator |
| 276 | StrategyChoice::begin() const |
| 277 | { |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 278 | return const_iterator(m_nameTree.fullEnumerate( |
| 279 | [] (const name_tree::Entry& entry) { |
| 280 | return static_cast<bool>(entry.getStrategyChoiceEntry()); |
| 281 | })); |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 284 | } // namespace nfd |