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(); |
| 97 | nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>()); |
| 98 | --m_nItems; |
| 99 | |
| 100 | Strategy& parentStrategy = this->findEffectiveStrategy(prefix); |
| 101 | this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this()); |
| 102 | } |
| 103 | |
| 104 | shared_ptr<const Name> |
| 105 | StrategyChoice::get(const Name& prefix) const |
| 106 | { |
| 107 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 108 | if (!static_cast<bool>(nameTreeEntry)) { |
| 109 | return shared_ptr<const Name>(); |
| 110 | } |
| 111 | |
| 112 | shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 113 | if (!static_cast<bool>(entry)) { |
| 114 | return shared_ptr<const Name>(); |
| 115 | } |
| 116 | |
| 117 | return entry->getStrategy().getName().shared_from_this(); |
| 118 | } |
| 119 | |
| 120 | static inline bool |
| 121 | predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry) |
| 122 | { |
| 123 | return static_cast<bool>(entry.getStrategyChoiceEntry()); |
| 124 | } |
| 125 | |
| 126 | Strategy& |
| 127 | StrategyChoice::findEffectiveStrategy(const Name& prefix) const |
| 128 | { |
| 129 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 130 | m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 131 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 132 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 133 | } |
| 134 | |
| 135 | Strategy& |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 136 | StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const |
| 137 | { |
| 138 | shared_ptr<strategy_choice::Entry> entry = nameTreeEntry->getStrategyChoiceEntry(); |
| 139 | if (static_cast<bool>(entry)) |
| 140 | return entry->getStrategy(); |
| 141 | nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry, |
| 142 | &predicate_NameTreeEntry_hasStrategyChoiceEntry); |
| 143 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 144 | return nameTreeEntry->getStrategyChoiceEntry()->getStrategy(); |
| 145 | } |
| 146 | |
| 147 | Strategy& |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 148 | StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const |
| 149 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 150 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 151 | |
| 152 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 153 | |
| 154 | return findEffectiveStrategy(nameTreeEntry); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 157 | Strategy& |
| 158 | StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const |
| 159 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 160 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry); |
| 161 | |
| 162 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 163 | |
| 164 | return findEffectiveStrategy(nameTreeEntry); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 167 | shared_ptr<fw::Strategy> |
| 168 | StrategyChoice::getStrategy(const Name& strategyName) |
| 169 | { |
| 170 | StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName); |
| 171 | return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>(); |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy) |
| 176 | { |
| 177 | this->install(strategy); |
| 178 | |
| 179 | // don't use .insert here, because it will invoke findEffectiveStrategy |
| 180 | // which expects an existing root entry |
| 181 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name()); |
| 182 | shared_ptr<Entry> entry = make_shared<Entry>(Name()); |
| 183 | nameTreeEntry->setStrategyChoiceEntry(entry); |
| 184 | ++m_nItems; |
| 185 | NFD_LOG_INFO("setDefaultStrategy(" << strategy->getName() << ") new entry"); |
| 186 | |
| 187 | entry->setStrategy(strategy); |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry, |
| 192 | shared_ptr<fw::Strategy> oldStrategy, |
| 193 | shared_ptr<fw::Strategy> newStrategy) |
| 194 | { |
| 195 | entry->setStrategy(newStrategy); |
| 196 | if (oldStrategy == newStrategy) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | // TODO delete incompatible StrategyInfo |
| 201 | NFD_LOG_WARN("changeStrategy(" << entry->getPrefix() << ") " << |
| 202 | "runtime strategy change not implemented"); |
| 203 | } |
| 204 | |
| 205 | } // namespace nfd |