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