34 namespace strategy_choice {
49 : m_forwarder(forwarder)
50 , m_nameTree(m_forwarder.getNameTree())
58 auto entry = make_unique<Entry>(Name());
59 entry->setStrategy(Strategy::create(strategyName, m_forwarder));
60 NFD_LOG_INFO(
"setDefaultStrategy " << entry->getStrategyInstanceName());
65 nte.setStrategyChoiceEntry(std::move(entry));
72 unique_ptr<Strategy> strategy;
74 strategy = Strategy::create(strategyName, m_forwarder);
76 catch (
const std::invalid_argument& e) {
77 NFD_LOG_ERROR(
"insert(" << prefix <<
"," << strategyName <<
") cannot create strategy: " << e.what());
81 if (strategy ==
nullptr) {
82 NFD_LOG_ERROR(
"insert(" << prefix <<
"," << strategyName <<
") strategy not registered");
83 return InsertResult::NOT_REGISTERED;
88 Strategy* oldStrategy =
nullptr;
89 if (entry !=
nullptr) {
91 NFD_LOG_TRACE(
"insert(" << prefix <<
") not changing " << strategy->getInstanceName());
92 return InsertResult::OK;
95 NFD_LOG_TRACE(
"insert(" << prefix <<
") changing from " << oldStrategy->getInstanceName() <<
96 " to " << strategy->getInstanceName());
100 auto newEntry = make_unique<Entry>(prefix);
101 entry = newEntry.get();
104 NFD_LOG_TRACE(
"insert(" << prefix <<
") new entry " << strategy->getInstanceName());
107 this->changeStrategy(*entry, *oldStrategy, *strategy);
109 return InsertResult::OK;
112 StrategyChoice::InsertResult::InsertResult(Status status,
const std::string& exceptionMessage)
114 , m_exceptionMessage(exceptionMessage)
121 switch (res.m_status) {
122 case StrategyChoice::InsertResult::OK:
124 case StrategyChoice::InsertResult::NOT_REGISTERED:
125 return os <<
"Strategy not registered";
126 case StrategyChoice::InsertResult::EXCEPTION:
127 return os <<
"Error instantiating strategy: " << res.m_exceptionMessage;
135 BOOST_ASSERT(prefix.size() > 0);
138 if (nte ==
nullptr) {
142 Entry* entry = nte->getStrategyChoiceEntry();
143 if (entry ==
nullptr) {
150 this->changeStrategy(*entry, oldStrategy, parentStrategy);
152 nte->setStrategyChoiceEntry(
nullptr);
153 m_nameTree.eraseIfEmpty(nte);
157 std::pair<bool, Name>
161 if (nte ==
nullptr) {
162 return {
false, Name()};
166 if (entry ==
nullptr) {
167 return {
false, Name()};
175 StrategyChoice::findEffectiveStrategyImpl(
const K& key)
const
178 BOOST_ASSERT(nte !=
nullptr);
185 return this->findEffectiveStrategyImpl(prefix);
191 return this->findEffectiveStrategyImpl(pitEntry);
197 return this->findEffectiveStrategyImpl(measurementsEntry);
205 for (
const shared_ptr<pit::Entry>& pitEntry : nte.
getPitEntries()) {
206 pitEntry->clearStrategyInfo();
207 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
210 for (
const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
220 StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
222 const Name& oldInstanceName = oldStrategy.getInstanceName();
223 const Name& newInstanceName = newStrategy.getInstanceName();
224 if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
226 NFD_LOG_INFO(
"changeStrategy(" << entry.getPrefix() <<
") "
227 << oldInstanceName <<
" -> " << newInstanceName
232 NFD_LOG_INFO(
"changeStrategy(" << entry.getPrefix() <<
") "
233 << oldInstanceName <<
" -> " << newInstanceName);
237 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
238 BOOST_ASSERT(rootNte !=
nullptr);
239 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
240 [&rootNte] (
const name_tree::Entry& nte) -> std::pair<bool, bool> {
241 if (&nte == rootNte) {
244 if (nte.getStrategyChoiceEntry() !=
nullptr) {
245 return {
false,
false};
249 for (
const name_tree::Entry& nte : ntChanged) {
255 StrategyChoice::getRange()
const
258 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
fw::Strategy & getStrategy() const
StrategyChoice(Forwarder &forwarder)
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
InsertResult insert(const Name &prefix, const Name &strategyName)
set strategy of prefix to be strategyName
contains information about an Interest toward an outgoing face
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
std::ostream & operator<<(std::ostream &os, const StrategyChoice::InsertResult &res)
boost::range_iterator< Range >::type const_iterator
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
#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
void setDefaultStrategy(const Name &strategyName)
set the default strategy
NFD_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator)
const Name & getStrategyInstanceName() 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
const Name & getName() const
static bool nteHasStrategyChoiceEntry(const name_tree::Entry &nte)
void setStrategy(unique_ptr< fw::Strategy > strategy)
void erase(const Name &prefix)
make prefix to inherit strategy from its parent