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/>. |
| 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>()); |
| 123 | --m_nItems; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | shared_ptr<const Name> |
| 127 | StrategyChoice::get(const Name& prefix) const |
| 128 | { |
| 129 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 130 | if (!static_cast<bool>(nameTreeEntry)) { |
| 131 | return shared_ptr<const Name>(); |
| 132 | } |
| 133 | |
| 134 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 135 | if (!static_cast<bool>(entry)) { |
| 136 | return shared_ptr<const Name>(); |
| 137 | } |
| 138 | |
| 139 | return entry->getStrategy().getName().shared_from_this(); |
| 140 | } |
| 141 | |
| 142 | static inline bool |
| 143 | predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry) |
| 144 | { |
| 145 | return static_cast<bool>(entry.getStrategyChoiceEntry()); |
| 146 | } |
| 147 | |
| 148 | Strategy& |
| 149 | StrategyChoice::findEffectiveStrategy(const Name& prefix) const |
| 150 | { |
| 151 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 152 | m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 153 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 154 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 155 | } |
| 156 | |
| 157 | Strategy& |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 158 | StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const |
| 159 | { |
| 160 | shared_ptr<strategy_choice::Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 161 | if (static_cast<bool>(entry)) |
| 162 | return entry->getStrategy(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 163 | nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry, |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 164 | &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 165 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 166 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 167 | } |
| 168 | |
| 169 | Strategy& |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 170 | StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const |
| 171 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 172 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 173 | |
| 174 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 175 | |
| 176 | return findEffectiveStrategy(nameTreeEntry); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 179 | Strategy& |
| 180 | StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const |
| 181 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 182 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry); |
| 183 | |
| 184 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 185 | |
| 186 | return findEffectiveStrategy(nameTreeEntry); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 189 | shared_ptr<fw::Strategy> |
| 190 | StrategyChoice::getStrategy(const Name& strategyName) |
| 191 | { |
| 192 | StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName); |
| 193 | return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>(); |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy) |
| 198 | { |
| 199 | this->install(strategy); |
| 200 | |
| 201 | // don't use .insert here, because it will invoke findEffectiveStrategy |
| 202 | // which expects an existing root entry |
| 203 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name()); |
| 204 | shared_ptr<Entry> entry = make_shared<Entry>(Name()); |
| 205 | nameTreeEntry->setStrategyChoiceEntry(entry); |
| 206 | ++m_nItems; |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 207 | NFD_LOG_INFO("Set default strategy " << strategy->getName()); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 208 | |
| 209 | entry->setStrategy(strategy); |
| 210 | } |
| 211 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 212 | /** \brief a predicate that decides whether StrategyInfo should be reset |
| 213 | * |
| 214 | * StrategyInfo on a NameTree entry needs to be reset, |
| 215 | * if its effective strategy is covered by the changing StrategyChoice entry. |
| 216 | */ |
| 217 | static inline std::pair<bool,bool> |
| 218 | predicate_nameTreeEntry_needResetStrategyChoice(const name_tree::Entry& nameTreeEntry, |
| 219 | const name_tree::Entry& rootEntry) |
| 220 | { |
| 221 | if (&nameTreeEntry == &rootEntry) { |
| 222 | return std::make_pair(true, true); |
| 223 | } |
| 224 | if (static_cast<bool>(nameTreeEntry.getStrategyChoiceEntry())) { |
| 225 | return std::make_pair(false, false); |
| 226 | } |
| 227 | return std::make_pair(true, true); |
| 228 | } |
| 229 | |
| 230 | static inline void |
| 231 | clearStrategyInfo_pitFaceRecord(const pit::FaceRecord& pitFaceRecord) |
| 232 | { |
| 233 | const_cast<pit::FaceRecord&>(pitFaceRecord).clearStrategyInfo(); |
| 234 | } |
| 235 | |
| 236 | static inline void |
| 237 | clearStrategyInfo_pitEntry(shared_ptr<pit::Entry> pitEntry) |
| 238 | { |
| 239 | pitEntry->clearStrategyInfo(); |
| 240 | std::for_each(pitEntry->getInRecords().begin(), pitEntry->getInRecords().end(), |
| 241 | &clearStrategyInfo_pitFaceRecord); |
| 242 | std::for_each(pitEntry->getOutRecords().begin(), pitEntry->getOutRecords().end(), |
| 243 | &clearStrategyInfo_pitFaceRecord); |
| 244 | } |
| 245 | |
| 246 | static inline void |
| 247 | clearStrategyInfo(const name_tree::Entry& nameTreeEntry) |
| 248 | { |
| 249 | NFD_LOG_TRACE("clearStrategyInfo " << nameTreeEntry.getPrefix()); |
| 250 | |
| 251 | std::for_each(nameTreeEntry.getPitEntries().begin(), nameTreeEntry.getPitEntries().end(), |
| 252 | &clearStrategyInfo_pitEntry); |
| 253 | if (static_cast<bool>(nameTreeEntry.getMeasurementsEntry())) { |
| 254 | nameTreeEntry.getMeasurementsEntry()->clearStrategyInfo(); |
| 255 | } |
| 256 | } |
| 257 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 258 | void |
| 259 | StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry, |
| 260 | shared_ptr<fw::Strategy> oldStrategy, |
| 261 | shared_ptr<fw::Strategy> newStrategy) |
| 262 | { |
| 263 | entry->setStrategy(newStrategy); |
| 264 | if (oldStrategy == newStrategy) { |
| 265 | return; |
| 266 | } |
| 267 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 268 | std::for_each(m_nameTree.partialEnumerate(entry->getPrefix(), |
| 269 | bind(&predicate_nameTreeEntry_needResetStrategyChoice, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 270 | _1, cref(*m_nameTree.get(*entry)))), |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 271 | m_nameTree.end(), |
| 272 | &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 | { |
| 278 | return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasStrategyChoiceEntry)); |
| 279 | } |
| 280 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 281 | } // namespace nfd |