Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "strategy-choice.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 8 | #include "core/logger.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 9 | #include "fw/strategy.hpp" |
| 10 | #include "pit-entry.hpp" |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 11 | #include "measurements-entry.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 12 | |
| 13 | namespace nfd { |
| 14 | |
| 15 | using strategy_choice::Entry; |
| 16 | using fw::Strategy; |
| 17 | |
| 18 | NFD_LOG_INIT("StrategyChoice"); |
| 19 | |
| 20 | StrategyChoice::StrategyChoice(NameTree& nameTree, shared_ptr<Strategy> defaultStrategy) |
| 21 | : m_nameTree(nameTree) |
| 22 | { |
| 23 | this->setDefaultStrategy(defaultStrategy); |
| 24 | } |
| 25 | |
| 26 | bool |
| 27 | StrategyChoice::hasStrategy(const Name& strategyName) const |
| 28 | { |
| 29 | return m_strategyInstances.count(strategyName) > 0; |
| 30 | } |
| 31 | |
| 32 | bool |
| 33 | StrategyChoice::install(shared_ptr<Strategy> strategy) |
| 34 | { |
| 35 | BOOST_ASSERT(static_cast<bool>(strategy)); |
| 36 | const Name& strategyName = strategy->getName(); |
| 37 | |
| 38 | if (this->hasStrategy(strategyName)) { |
| 39 | NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName"); |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | m_strategyInstances[strategyName] = strategy; |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | bool |
| 48 | StrategyChoice::insert(const Name& prefix, const Name& strategyName) |
| 49 | { |
| 50 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix); |
| 51 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 52 | shared_ptr<Strategy> oldStrategy; |
| 53 | |
| 54 | if (static_cast<bool>(entry)) { |
| 55 | if (entry->getStrategy().getName() == strategyName) { |
| 56 | NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") not changing"); |
| 57 | return true; |
| 58 | } |
| 59 | oldStrategy = entry->getStrategy().shared_from_this(); |
| 60 | NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") " |
| 61 | "changing from " << oldStrategy->getName()); |
| 62 | } |
| 63 | |
| 64 | shared_ptr<Strategy> strategy = this->getStrategy(strategyName); |
| 65 | if (!static_cast<bool>(strategy)) { |
| 66 | NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed"); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if (!static_cast<bool>(entry)) { |
| 71 | oldStrategy = this->findEffectiveStrategy(prefix).shared_from_this(); |
| 72 | entry = make_shared<Entry>(prefix); |
| 73 | nameTreeEntry->setStrategyChoiceEntry(entry); |
| 74 | ++m_nItems; |
| 75 | NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") new entry"); |
| 76 | } |
| 77 | |
| 78 | this->changeStrategy(entry, oldStrategy, strategy); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | StrategyChoice::erase(const Name& prefix) |
| 84 | { |
| 85 | BOOST_ASSERT(prefix.size() > 0); |
| 86 | |
| 87 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 88 | if (!static_cast<bool>(nameTreeEntry)) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 93 | if (!static_cast<bool>(entry)) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | Strategy& oldStrategy = entry->getStrategy(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 98 | |
| 99 | Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1)); |
| 100 | this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this()); |
| 101 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 102 | nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>()); |
| 103 | --m_nItems; |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | shared_ptr<const Name> |
| 107 | StrategyChoice::get(const Name& prefix) const |
| 108 | { |
| 109 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 110 | if (!static_cast<bool>(nameTreeEntry)) { |
| 111 | return shared_ptr<const Name>(); |
| 112 | } |
| 113 | |
| 114 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 115 | if (!static_cast<bool>(entry)) { |
| 116 | return shared_ptr<const Name>(); |
| 117 | } |
| 118 | |
| 119 | return entry->getStrategy().getName().shared_from_this(); |
| 120 | } |
| 121 | |
| 122 | static inline bool |
| 123 | predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry) |
| 124 | { |
| 125 | return static_cast<bool>(entry.getStrategyChoiceEntry()); |
| 126 | } |
| 127 | |
| 128 | Strategy& |
| 129 | StrategyChoice::findEffectiveStrategy(const Name& prefix) const |
| 130 | { |
| 131 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 132 | m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 133 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 134 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 135 | } |
| 136 | |
| 137 | Strategy& |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 138 | StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const |
| 139 | { |
| 140 | shared_ptr<strategy_choice::Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 141 | if (static_cast<bool>(entry)) |
| 142 | return entry->getStrategy(); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 143 | nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry, |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 144 | &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 145 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 146 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 147 | } |
| 148 | |
| 149 | Strategy& |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 150 | StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const |
| 151 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 152 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 153 | |
| 154 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 155 | |
| 156 | return findEffectiveStrategy(nameTreeEntry); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 159 | Strategy& |
| 160 | StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const |
| 161 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 162 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry); |
| 163 | |
| 164 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 165 | |
| 166 | return findEffectiveStrategy(nameTreeEntry); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 169 | shared_ptr<fw::Strategy> |
| 170 | StrategyChoice::getStrategy(const Name& strategyName) |
| 171 | { |
| 172 | StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName); |
| 173 | return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>(); |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy) |
| 178 | { |
| 179 | this->install(strategy); |
| 180 | |
| 181 | // don't use .insert here, because it will invoke findEffectiveStrategy |
| 182 | // which expects an existing root entry |
| 183 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name()); |
| 184 | shared_ptr<Entry> entry = make_shared<Entry>(Name()); |
| 185 | nameTreeEntry->setStrategyChoiceEntry(entry); |
| 186 | ++m_nItems; |
| 187 | NFD_LOG_INFO("setDefaultStrategy(" << strategy->getName() << ") new entry"); |
| 188 | |
| 189 | entry->setStrategy(strategy); |
| 190 | } |
| 191 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 192 | /** \brief a predicate that decides whether StrategyInfo should be reset |
| 193 | * |
| 194 | * StrategyInfo on a NameTree entry needs to be reset, |
| 195 | * if its effective strategy is covered by the changing StrategyChoice entry. |
| 196 | */ |
| 197 | static inline std::pair<bool,bool> |
| 198 | predicate_nameTreeEntry_needResetStrategyChoice(const name_tree::Entry& nameTreeEntry, |
| 199 | const name_tree::Entry& rootEntry) |
| 200 | { |
| 201 | if (&nameTreeEntry == &rootEntry) { |
| 202 | return std::make_pair(true, true); |
| 203 | } |
| 204 | if (static_cast<bool>(nameTreeEntry.getStrategyChoiceEntry())) { |
| 205 | return std::make_pair(false, false); |
| 206 | } |
| 207 | return std::make_pair(true, true); |
| 208 | } |
| 209 | |
| 210 | static inline void |
| 211 | clearStrategyInfo_pitFaceRecord(const pit::FaceRecord& pitFaceRecord) |
| 212 | { |
| 213 | const_cast<pit::FaceRecord&>(pitFaceRecord).clearStrategyInfo(); |
| 214 | } |
| 215 | |
| 216 | static inline void |
| 217 | clearStrategyInfo_pitEntry(shared_ptr<pit::Entry> pitEntry) |
| 218 | { |
| 219 | pitEntry->clearStrategyInfo(); |
| 220 | std::for_each(pitEntry->getInRecords().begin(), pitEntry->getInRecords().end(), |
| 221 | &clearStrategyInfo_pitFaceRecord); |
| 222 | std::for_each(pitEntry->getOutRecords().begin(), pitEntry->getOutRecords().end(), |
| 223 | &clearStrategyInfo_pitFaceRecord); |
| 224 | } |
| 225 | |
| 226 | static inline void |
| 227 | clearStrategyInfo(const name_tree::Entry& nameTreeEntry) |
| 228 | { |
| 229 | NFD_LOG_TRACE("clearStrategyInfo " << nameTreeEntry.getPrefix()); |
| 230 | |
| 231 | std::for_each(nameTreeEntry.getPitEntries().begin(), nameTreeEntry.getPitEntries().end(), |
| 232 | &clearStrategyInfo_pitEntry); |
| 233 | if (static_cast<bool>(nameTreeEntry.getMeasurementsEntry())) { |
| 234 | nameTreeEntry.getMeasurementsEntry()->clearStrategyInfo(); |
| 235 | } |
| 236 | } |
| 237 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 238 | void |
| 239 | StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry, |
| 240 | shared_ptr<fw::Strategy> oldStrategy, |
| 241 | shared_ptr<fw::Strategy> newStrategy) |
| 242 | { |
| 243 | entry->setStrategy(newStrategy); |
| 244 | if (oldStrategy == newStrategy) { |
| 245 | return; |
| 246 | } |
| 247 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 248 | std::for_each(m_nameTree.partialEnumerate(entry->getPrefix(), |
| 249 | bind(&predicate_nameTreeEntry_needResetStrategyChoice, |
| 250 | _1, boost::cref(*m_nameTree.get(*entry)))), |
| 251 | m_nameTree.end(), |
| 252 | &clearStrategyInfo); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | } // namespace nfd |