33 namespace strategy_choice {
46 : m_nameTree(nameTree)
49 this->setDefaultStrategy(std::move(defaultStrategy));
56 return m_strategyInstances.count(strategyName) > 0;
59 return this->getStrategy(strategyName) !=
nullptr;
63 std::pair<bool, Strategy*>
66 BOOST_ASSERT(strategy !=
nullptr);
67 Name strategyName = strategy->getName();
70 bool isInserted =
false;
72 std::tie(it, isInserted) = m_strategyInstances.emplace(strategyName, std::move(strategy));
75 NFD_LOG_ERROR(
"install(" << strategyName <<
") duplicate strategyName");
77 return std::make_pair(isInserted, it->second.get());
81 StrategyChoice::getStrategy(
const Name& strategyName)
const
83 Strategy* candidate =
nullptr;
84 for (
auto it = m_strategyInstances.lower_bound(strategyName);
85 it != m_strategyInstances.end() && strategyName.isPrefixOf(it->first); ++it) {
86 switch (it->first.size() - strategyName.size()) {
88 return it->second.get();
90 candidate = it->second.get();
100 Strategy* strategy = this->getStrategy(strategyName);
101 if (strategy ==
nullptr) {
102 NFD_LOG_ERROR(
"insert(" << prefix <<
"," << strategyName <<
") strategy not installed");
108 Strategy* oldStrategy =
nullptr;
109 if (entry !=
nullptr) {
111 NFD_LOG_TRACE(
"insert(" << prefix <<
") not changing " << strategy->getName());
115 NFD_LOG_TRACE(
"insert(" << prefix <<
") changing from " << oldStrategy->getName() <<
116 " to " << strategy->getName());
119 if (entry ==
nullptr) {
121 auto newEntry = make_unique<Entry>(prefix);
122 entry = newEntry.get();
125 NFD_LOG_TRACE(
"insert(" << prefix <<
") new entry " << strategy->getName());
128 this->changeStrategy(*entry, *oldStrategy, *strategy);
136 BOOST_ASSERT(prefix.size() > 0);
139 if (nte ==
nullptr) {
143 Entry* entry = nte->getStrategyChoiceEntry();
144 if (entry ==
nullptr) {
151 this->changeStrategy(*entry, oldStrategy, parentStrategy);
153 nte->setStrategyChoiceEntry(
nullptr);
154 m_nameTree.eraseIfEmpty(nte);
158 std::pair<bool, Name>
162 if (nte ==
nullptr) {
163 return {
false, Name()};
167 if (entry ==
nullptr) {
168 return {
false, Name()};
176 StrategyChoice::findEffectiveStrategyImpl(
const K& key)
const
179 BOOST_ASSERT(nte !=
nullptr);
186 return this->findEffectiveStrategyImpl(prefix);
192 return this->findEffectiveStrategyImpl(pitEntry);
198 return this->findEffectiveStrategyImpl(measurementsEntry);
202 StrategyChoice::setDefaultStrategy(unique_ptr<Strategy> strategy)
204 bool isInstalled =
false;
205 Strategy* instance =
nullptr;
206 std::tie(isInstalled, instance) = this->
install(std::move(strategy));
207 BOOST_ASSERT(isInstalled);
209 auto entry = make_unique<Entry>(Name());
210 entry->setStrategy(*instance);
217 NFD_LOG_INFO(
"setDefaultStrategy " << instance->getName());
225 for (
const shared_ptr<pit::Entry>& pitEntry : nte.
getPitEntries()) {
226 pitEntry->clearStrategyInfo();
227 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
230 for (
const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
240 StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
242 if (&oldStrategy == &newStrategy) {
246 NFD_LOG_INFO(
"changeStrategy(" << entry.getPrefix() <<
")"
247 <<
" from " << oldStrategy.getName()
248 <<
" to " << newStrategy.getName());
252 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
253 BOOST_ASSERT(rootNte !=
nullptr);
254 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
255 [&rootNte] (
const name_tree::Entry& nte) -> std::pair<bool, bool> {
256 if (&nte == rootNte) {
260 return {
false,
false};
264 for (
const name_tree::Entry& nte : ntChanged) {
270 StrategyChoice::getRange()
const
273 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
fw::Strategy & getStrategy() const
std::pair< bool, Name > get(const Name &prefix) const
get strategy Name of prefix
static void clearStrategyInfo(const name_tree::Entry &nte)
represents a Measurements entry
contains information about an Interest toward an outgoing face
void setStrategy(fw::Strategy &strategy)
strategy_choice::Entry * getStrategyChoiceEntry() const
#define NFD_LOG_ERROR(expression)
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
void clearStrategyInfo()
clear all StrategyInfo items
Table::const_iterator iterator
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
StrategyChoice(NameTree &nameTree, unique_ptr< fw::Strategy > defaultStrategy)
bool insert(const Name &prefix, const Name &strategyName)
set strategy of prefix to be strategyName
#define NFD_LOG_INFO(expression)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
contains information about an Interest from an incoming face
std::pair< bool, fw::Strategy * > install(unique_ptr< fw::Strategy > strategy)
install a strategy
const Name & getName() const
an entry in the name tree
represents a Strategy Choice entry
measurements::Entry * getMeasurementsEntry() const
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
get effective strategy for prefix
bool hasStrategy(const Name &strategyName, bool isExact=false) const
determines if a strategy is installed
const Name & getName() const
static bool nteHasStrategyChoiceEntry(const name_tree::Entry &nte)
void erase(const Name &prefix)
make prefix to inherit strategy from its parent