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 | |
| 63 | if (this->hasStrategy(strategyName)) { |
| 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 | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 72 | shared_ptr<fw::Strategy> |
| 73 | StrategyChoice::getStrategy(const Name& strategyName) const |
| 74 | { |
| 75 | shared_ptr<fw::Strategy> candidate; |
| 76 | for (StrategyInstanceTable::const_iterator it = m_strategyInstances.lower_bound(strategyName); |
| 77 | it != m_strategyInstances.end() && strategyName.isPrefixOf(it->first); ++it) { |
| 78 | switch (it->first.size() - strategyName.size()) { |
| 79 | case 0: // exact match |
| 80 | return it->second; |
| 81 | case 1: // unversioned strategyName matches versioned strategy |
| 82 | candidate = it->second; |
| 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 | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 92 | shared_ptr<Strategy> strategy = this->getStrategy(strategyName); |
| 93 | if (!static_cast<bool>(strategy)) { |
| 94 | NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed"); |
| 95 | return false; |
| 96 | } |
| 97 | |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 98 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix); |
| 99 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 100 | shared_ptr<Strategy> oldStrategy; |
| 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 | } |
| 106 | oldStrategy = entry->getStrategy().shared_from_this(); |
| 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)) { |
| 112 | oldStrategy = this->findEffectiveStrategy(prefix).shared_from_this(); |
| 113 | entry = make_shared<Entry>(prefix); |
| 114 | nameTreeEntry->setStrategyChoiceEntry(entry); |
| 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 | |
| 119 | this->changeStrategy(entry, oldStrategy, strategy); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | StrategyChoice::erase(const Name& prefix) |
| 125 | { |
| 126 | BOOST_ASSERT(prefix.size() > 0); |
| 127 | |
| 128 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 129 | if (!static_cast<bool>(nameTreeEntry)) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 134 | if (!static_cast<bool>(entry)) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | Strategy& oldStrategy = entry->getStrategy(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 139 | |
| 140 | Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1)); |
| 141 | this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this()); |
| 142 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 143 | nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>()); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 144 | m_nameTree.eraseEntryIfEmpty(nameTreeEntry); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 145 | --m_nItems; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | shared_ptr<const Name> |
| 149 | StrategyChoice::get(const Name& prefix) const |
| 150 | { |
| 151 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 152 | if (!static_cast<bool>(nameTreeEntry)) { |
| 153 | return shared_ptr<const Name>(); |
| 154 | } |
| 155 | |
| 156 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 157 | if (!static_cast<bool>(entry)) { |
| 158 | return shared_ptr<const Name>(); |
| 159 | } |
| 160 | |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 161 | return make_shared<const Name>(entry->getStrategy().getName()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static inline bool |
| 165 | predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry) |
| 166 | { |
| 167 | return static_cast<bool>(entry.getStrategyChoiceEntry()); |
| 168 | } |
| 169 | |
| 170 | Strategy& |
| 171 | StrategyChoice::findEffectiveStrategy(const Name& prefix) const |
| 172 | { |
| 173 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 174 | m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 175 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 176 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 177 | } |
| 178 | |
| 179 | Strategy& |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 180 | StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const |
| 181 | { |
| 182 | shared_ptr<strategy_choice::Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 183 | if (static_cast<bool>(entry)) |
| 184 | return entry->getStrategy(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 185 | nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry, |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 186 | &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 187 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 188 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 189 | } |
| 190 | |
| 191 | Strategy& |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 192 | StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const |
| 193 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 194 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 195 | |
| 196 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 197 | |
| 198 | return findEffectiveStrategy(nameTreeEntry); |
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 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 204 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry); |
| 205 | |
| 206 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 207 | |
| 208 | return findEffectiveStrategy(nameTreeEntry); |
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 |
| 218 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name()); |
| 219 | shared_ptr<Entry> entry = make_shared<Entry>(Name()); |
| 220 | nameTreeEntry->setStrategyChoiceEntry(entry); |
| 221 | ++m_nItems; |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 222 | NFD_LOG_INFO("Set default strategy " << strategy->getName()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 223 | |
| 224 | entry->setStrategy(strategy); |
| 225 | } |
| 226 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 227 | /** \brief a predicate that decides whether StrategyInfo should be reset |
| 228 | * |
| 229 | * StrategyInfo on a NameTree entry needs to be reset, |
| 230 | * if its effective strategy is covered by the changing StrategyChoice entry. |
| 231 | */ |
| 232 | static inline std::pair<bool,bool> |
| 233 | predicate_nameTreeEntry_needResetStrategyChoice(const name_tree::Entry& nameTreeEntry, |
| 234 | const name_tree::Entry& rootEntry) |
| 235 | { |
| 236 | if (&nameTreeEntry == &rootEntry) { |
| 237 | return std::make_pair(true, true); |
| 238 | } |
| 239 | if (static_cast<bool>(nameTreeEntry.getStrategyChoiceEntry())) { |
| 240 | return std::make_pair(false, false); |
| 241 | } |
| 242 | return std::make_pair(true, true); |
| 243 | } |
| 244 | |
| 245 | static inline void |
| 246 | clearStrategyInfo_pitFaceRecord(const pit::FaceRecord& pitFaceRecord) |
| 247 | { |
| 248 | const_cast<pit::FaceRecord&>(pitFaceRecord).clearStrategyInfo(); |
| 249 | } |
| 250 | |
| 251 | static inline void |
| 252 | clearStrategyInfo_pitEntry(shared_ptr<pit::Entry> pitEntry) |
| 253 | { |
| 254 | pitEntry->clearStrategyInfo(); |
| 255 | std::for_each(pitEntry->getInRecords().begin(), pitEntry->getInRecords().end(), |
| 256 | &clearStrategyInfo_pitFaceRecord); |
| 257 | std::for_each(pitEntry->getOutRecords().begin(), pitEntry->getOutRecords().end(), |
| 258 | &clearStrategyInfo_pitFaceRecord); |
| 259 | } |
| 260 | |
| 261 | static inline void |
| 262 | clearStrategyInfo(const name_tree::Entry& nameTreeEntry) |
| 263 | { |
| 264 | NFD_LOG_TRACE("clearStrategyInfo " << nameTreeEntry.getPrefix()); |
| 265 | |
| 266 | std::for_each(nameTreeEntry.getPitEntries().begin(), nameTreeEntry.getPitEntries().end(), |
| 267 | &clearStrategyInfo_pitEntry); |
| 268 | if (static_cast<bool>(nameTreeEntry.getMeasurementsEntry())) { |
| 269 | nameTreeEntry.getMeasurementsEntry()->clearStrategyInfo(); |
| 270 | } |
| 271 | } |
| 272 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 273 | void |
| 274 | StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry, |
| 275 | shared_ptr<fw::Strategy> oldStrategy, |
| 276 | shared_ptr<fw::Strategy> newStrategy) |
| 277 | { |
| 278 | entry->setStrategy(newStrategy); |
| 279 | if (oldStrategy == newStrategy) { |
| 280 | return; |
| 281 | } |
| 282 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 283 | std::for_each(m_nameTree.partialEnumerate(entry->getPrefix(), |
| 284 | bind(&predicate_nameTreeEntry_needResetStrategyChoice, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 285 | _1, cref(*m_nameTree.get(*entry)))), |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 286 | m_nameTree.end(), |
| 287 | &clearStrategyInfo); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 290 | StrategyChoice::const_iterator |
| 291 | StrategyChoice::begin() const |
| 292 | { |
| 293 | return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasStrategyChoiceEntry)); |
| 294 | } |
| 295 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 296 | } // namespace nfd |