32 #include <ndn-cxx/util/concepts.hpp> 35 namespace strategy_choice {
50 : m_forwarder(forwarder)
51 , m_nameTree(m_forwarder.getNameTree())
59 auto entry = make_unique<Entry>(Name());
60 entry->setStrategy(Strategy::create(strategyName, m_forwarder));
61 NFD_LOG_INFO(
"setDefaultStrategy " << entry->getStrategyInstanceName());
66 nte.setStrategyChoiceEntry(std::move(entry));
73 if (prefix.size() > NameTree::getMaxDepth()) {
74 return InsertResult::DEPTH_EXCEEDED;
77 unique_ptr<Strategy> strategy;
79 strategy = Strategy::create(strategyName, m_forwarder);
81 catch (
const std::invalid_argument& e) {
82 NFD_LOG_ERROR(
"insert(" << prefix <<
"," << strategyName <<
") cannot create strategy: " << e.what());
86 if (strategy ==
nullptr) {
87 NFD_LOG_ERROR(
"insert(" << prefix <<
"," << strategyName <<
") strategy not registered");
88 return InsertResult::NOT_REGISTERED;
93 Strategy* oldStrategy =
nullptr;
94 if (entry !=
nullptr) {
96 NFD_LOG_TRACE(
"insert(" << prefix <<
") not changing " << strategy->getInstanceName());
97 return InsertResult::OK;
100 NFD_LOG_TRACE(
"insert(" << prefix <<
") changing from " << oldStrategy->getInstanceName() <<
101 " to " << strategy->getInstanceName());
105 auto newEntry = make_unique<Entry>(prefix);
106 entry = newEntry.get();
109 NFD_LOG_TRACE(
"insert(" << prefix <<
") new entry " << strategy->getInstanceName());
112 this->changeStrategy(*entry, *oldStrategy, *strategy);
114 return InsertResult::OK;
117 StrategyChoice::InsertResult::InsertResult(Status status,
const std::string& exceptionMessage)
119 , m_exceptionMessage(exceptionMessage)
126 switch (res.m_status) {
127 case StrategyChoice::InsertResult::OK:
129 case StrategyChoice::InsertResult::NOT_REGISTERED:
130 return os <<
"Strategy not registered";
131 case StrategyChoice::InsertResult::EXCEPTION:
132 return os <<
"Error instantiating strategy: " << res.m_exceptionMessage;
133 case StrategyChoice::InsertResult::DEPTH_EXCEEDED:
134 return os <<
"Prefix has too many components (limit is " 135 << to_string(NameTree::getMaxDepth()) <<
")";
143 BOOST_ASSERT(prefix.size() > 0);
146 if (nte ==
nullptr) {
150 Entry* entry = nte->getStrategyChoiceEntry();
151 if (entry ==
nullptr) {
158 this->changeStrategy(*entry, oldStrategy, parentStrategy);
160 nte->setStrategyChoiceEntry(
nullptr);
161 m_nameTree.eraseIfEmpty(nte);
165 std::pair<bool, Name>
169 if (nte ==
nullptr) {
170 return {
false, Name()};
174 if (entry ==
nullptr) {
175 return {
false, Name()};
183 StrategyChoice::findEffectiveStrategyImpl(
const K& key)
const 186 BOOST_ASSERT(nte !=
nullptr);
193 return this->findEffectiveStrategyImpl(prefix);
199 return this->findEffectiveStrategyImpl(pitEntry);
205 return this->findEffectiveStrategyImpl(measurementsEntry);
213 for (
const shared_ptr<pit::Entry>& pitEntry : nte.
getPitEntries()) {
214 pitEntry->clearStrategyInfo();
215 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
218 for (
const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
228 StrategyChoice::changeStrategy(
Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
230 const Name& oldInstanceName = oldStrategy.getInstanceName();
231 const Name& newInstanceName = newStrategy.getInstanceName();
232 if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
235 << oldInstanceName <<
" -> " << newInstanceName
241 << oldInstanceName <<
" -> " << newInstanceName);
246 BOOST_ASSERT(rootNte !=
nullptr);
247 auto&& ntChanged = m_nameTree.partialEnumerate(entry.
getPrefix(),
249 if (&nte == rootNte) {
252 if (nte.getStrategyChoiceEntry() !=
nullptr) {
253 return {
false,
false};
263 StrategyChoice::getRange()
const 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
NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator)
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
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)
const Name & getPrefix() const
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
get effective strategy for prefix
const Name & getName() const
a functor to get a table entry from a name tree entry
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