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 |
| 47 | StrategyChoice::hasStrategy(const Name& strategyName) const |
| 48 | { |
| 49 | return m_strategyInstances.count(strategyName) > 0; |
| 50 | } |
| 51 | |
| 52 | bool |
| 53 | StrategyChoice::install(shared_ptr<Strategy> strategy) |
| 54 | { |
| 55 | BOOST_ASSERT(static_cast<bool>(strategy)); |
| 56 | const Name& strategyName = strategy->getName(); |
| 57 | |
| 58 | if (this->hasStrategy(strategyName)) { |
| 59 | NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName"); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | m_strategyInstances[strategyName] = strategy; |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | bool |
| 68 | StrategyChoice::insert(const Name& prefix, const Name& strategyName) |
| 69 | { |
| 70 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix); |
| 71 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 72 | shared_ptr<Strategy> oldStrategy; |
| 73 | |
| 74 | if (static_cast<bool>(entry)) { |
| 75 | if (entry->getStrategy().getName() == strategyName) { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 76 | NFD_LOG_TRACE("insert(" << prefix << "," << strategyName << ") not changing"); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | oldStrategy = entry->getStrategy().shared_from_this(); |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 80 | NFD_LOG_TRACE("insert(" << prefix << "," << strategyName << ") " |
| 81 | "changing from " << oldStrategy->getName()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | shared_ptr<Strategy> strategy = this->getStrategy(strategyName); |
| 85 | if (!static_cast<bool>(strategy)) { |
| 86 | NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed"); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if (!static_cast<bool>(entry)) { |
| 91 | oldStrategy = this->findEffectiveStrategy(prefix).shared_from_this(); |
| 92 | entry = make_shared<Entry>(prefix); |
| 93 | nameTreeEntry->setStrategyChoiceEntry(entry); |
| 94 | ++m_nItems; |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 95 | NFD_LOG_TRACE("insert(" << prefix << "," << strategyName << ") new entry"); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | this->changeStrategy(entry, oldStrategy, strategy); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | StrategyChoice::erase(const Name& prefix) |
| 104 | { |
| 105 | BOOST_ASSERT(prefix.size() > 0); |
| 106 | |
| 107 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 108 | if (!static_cast<bool>(nameTreeEntry)) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 113 | if (!static_cast<bool>(entry)) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | Strategy& oldStrategy = entry->getStrategy(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 118 | |
| 119 | Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1)); |
| 120 | this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this()); |
| 121 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 122 | nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>()); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame^] | 123 | m_nameTree.eraseEntryIfEmpty(nameTreeEntry); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 124 | --m_nItems; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | shared_ptr<const Name> |
| 128 | StrategyChoice::get(const Name& prefix) const |
| 129 | { |
| 130 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 131 | if (!static_cast<bool>(nameTreeEntry)) { |
| 132 | return shared_ptr<const Name>(); |
| 133 | } |
| 134 | |
| 135 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 136 | if (!static_cast<bool>(entry)) { |
| 137 | return shared_ptr<const Name>(); |
| 138 | } |
| 139 | |
| 140 | return entry->getStrategy().getName().shared_from_this(); |
| 141 | } |
| 142 | |
| 143 | static inline bool |
| 144 | predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry) |
| 145 | { |
| 146 | return static_cast<bool>(entry.getStrategyChoiceEntry()); |
| 147 | } |
| 148 | |
| 149 | Strategy& |
| 150 | StrategyChoice::findEffectiveStrategy(const Name& prefix) const |
| 151 | { |
| 152 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 153 | m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 154 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 155 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 156 | } |
| 157 | |
| 158 | Strategy& |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 159 | StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const |
| 160 | { |
| 161 | shared_ptr<strategy_choice::Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 162 | if (static_cast<bool>(entry)) |
| 163 | return entry->getStrategy(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 164 | nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry, |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 165 | &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 166 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 167 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 168 | } |
| 169 | |
| 170 | Strategy& |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 171 | StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const |
| 172 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 173 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 174 | |
| 175 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 176 | |
| 177 | return findEffectiveStrategy(nameTreeEntry); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 180 | Strategy& |
| 181 | StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const |
| 182 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 183 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry); |
| 184 | |
| 185 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 186 | |
| 187 | return findEffectiveStrategy(nameTreeEntry); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 190 | shared_ptr<fw::Strategy> |
| 191 | StrategyChoice::getStrategy(const Name& strategyName) |
| 192 | { |
| 193 | StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName); |
| 194 | return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>(); |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy) |
| 199 | { |
| 200 | this->install(strategy); |
| 201 | |
| 202 | // don't use .insert here, because it will invoke findEffectiveStrategy |
| 203 | // which expects an existing root entry |
| 204 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name()); |
| 205 | shared_ptr<Entry> entry = make_shared<Entry>(Name()); |
| 206 | nameTreeEntry->setStrategyChoiceEntry(entry); |
| 207 | ++m_nItems; |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 208 | NFD_LOG_INFO("Set default strategy " << strategy->getName()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 209 | |
| 210 | entry->setStrategy(strategy); |
| 211 | } |
| 212 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 213 | /** \brief a predicate that decides whether StrategyInfo should be reset |
| 214 | * |
| 215 | * StrategyInfo on a NameTree entry needs to be reset, |
| 216 | * if its effective strategy is covered by the changing StrategyChoice entry. |
| 217 | */ |
| 218 | static inline std::pair<bool,bool> |
| 219 | predicate_nameTreeEntry_needResetStrategyChoice(const name_tree::Entry& nameTreeEntry, |
| 220 | const name_tree::Entry& rootEntry) |
| 221 | { |
| 222 | if (&nameTreeEntry == &rootEntry) { |
| 223 | return std::make_pair(true, true); |
| 224 | } |
| 225 | if (static_cast<bool>(nameTreeEntry.getStrategyChoiceEntry())) { |
| 226 | return std::make_pair(false, false); |
| 227 | } |
| 228 | return std::make_pair(true, true); |
| 229 | } |
| 230 | |
| 231 | static inline void |
| 232 | clearStrategyInfo_pitFaceRecord(const pit::FaceRecord& pitFaceRecord) |
| 233 | { |
| 234 | const_cast<pit::FaceRecord&>(pitFaceRecord).clearStrategyInfo(); |
| 235 | } |
| 236 | |
| 237 | static inline void |
| 238 | clearStrategyInfo_pitEntry(shared_ptr<pit::Entry> pitEntry) |
| 239 | { |
| 240 | pitEntry->clearStrategyInfo(); |
| 241 | std::for_each(pitEntry->getInRecords().begin(), pitEntry->getInRecords().end(), |
| 242 | &clearStrategyInfo_pitFaceRecord); |
| 243 | std::for_each(pitEntry->getOutRecords().begin(), pitEntry->getOutRecords().end(), |
| 244 | &clearStrategyInfo_pitFaceRecord); |
| 245 | } |
| 246 | |
| 247 | static inline void |
| 248 | clearStrategyInfo(const name_tree::Entry& nameTreeEntry) |
| 249 | { |
| 250 | NFD_LOG_TRACE("clearStrategyInfo " << nameTreeEntry.getPrefix()); |
| 251 | |
| 252 | std::for_each(nameTreeEntry.getPitEntries().begin(), nameTreeEntry.getPitEntries().end(), |
| 253 | &clearStrategyInfo_pitEntry); |
| 254 | if (static_cast<bool>(nameTreeEntry.getMeasurementsEntry())) { |
| 255 | nameTreeEntry.getMeasurementsEntry()->clearStrategyInfo(); |
| 256 | } |
| 257 | } |
| 258 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 259 | void |
| 260 | StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry, |
| 261 | shared_ptr<fw::Strategy> oldStrategy, |
| 262 | shared_ptr<fw::Strategy> newStrategy) |
| 263 | { |
| 264 | entry->setStrategy(newStrategy); |
| 265 | if (oldStrategy == newStrategy) { |
| 266 | return; |
| 267 | } |
| 268 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 269 | std::for_each(m_nameTree.partialEnumerate(entry->getPrefix(), |
| 270 | bind(&predicate_nameTreeEntry_needResetStrategyChoice, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 271 | _1, cref(*m_nameTree.get(*entry)))), |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 272 | m_nameTree.end(), |
| 273 | &clearStrategyInfo); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 276 | StrategyChoice::const_iterator |
| 277 | StrategyChoice::begin() const |
| 278 | { |
| 279 | return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasStrategyChoiceEntry)); |
| 280 | } |
| 281 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 282 | } // namespace nfd |