33 #include <ndn-cxx/util/concepts.hpp> 36 namespace strategy_choice {
51 : m_forwarder(forwarder)
52 , m_nameTree(m_forwarder.getNameTree())
60 auto entry = make_unique<Entry>(Name());
61 entry->setStrategy(Strategy::create(strategyName, m_forwarder));
62 NFD_LOG_INFO(
"setDefaultStrategy " << entry->getStrategyInstanceName());
67 nte.setStrategyChoiceEntry(std::move(entry));
74 if (prefix.size() > NameTree::getMaxDepth()) {
75 return InsertResult::DEPTH_EXCEEDED;
78 unique_ptr<Strategy> strategy;
80 strategy = Strategy::create(strategyName, m_forwarder);
82 catch (
const std::invalid_argument& e) {
83 NFD_LOG_ERROR(
"insert(" << prefix <<
"," << strategyName <<
") cannot create strategy: " << e.what());
87 if (strategy ==
nullptr) {
88 NFD_LOG_ERROR(
"insert(" << prefix <<
"," << strategyName <<
") strategy not registered");
89 return InsertResult::NOT_REGISTERED;
94 Strategy* oldStrategy =
nullptr;
95 if (entry !=
nullptr) {
97 NFD_LOG_TRACE(
"insert(" << prefix <<
") not changing " << strategy->getInstanceName());
98 return InsertResult::OK;
101 NFD_LOG_TRACE(
"insert(" << prefix <<
") changing from " << oldStrategy->getInstanceName() <<
102 " to " << strategy->getInstanceName());
106 auto newEntry = make_unique<Entry>(prefix);
107 entry = newEntry.get();
110 NFD_LOG_TRACE(
"insert(" << prefix <<
") new entry " << strategy->getInstanceName());
113 this->changeStrategy(*entry, *oldStrategy, *strategy);
115 return InsertResult::OK;
118 StrategyChoice::InsertResult::InsertResult(Status status,
const std::string& exceptionMessage)
120 , m_exceptionMessage(exceptionMessage)
127 switch (res.m_status) {
128 case StrategyChoice::InsertResult::OK:
130 case StrategyChoice::InsertResult::NOT_REGISTERED:
131 return os <<
"Strategy not registered";
132 case StrategyChoice::InsertResult::EXCEPTION:
133 return os <<
"Error instantiating strategy: " << res.m_exceptionMessage;
134 case StrategyChoice::InsertResult::DEPTH_EXCEEDED:
135 return os <<
"Prefix has too many components (limit is " 136 << to_string(NameTree::getMaxDepth()) <<
")";
144 BOOST_ASSERT(prefix.size() > 0);
147 if (nte ==
nullptr) {
151 Entry* entry = nte->getStrategyChoiceEntry();
152 if (entry ==
nullptr) {
159 this->changeStrategy(*entry, oldStrategy, parentStrategy);
161 nte->setStrategyChoiceEntry(
nullptr);
162 m_nameTree.eraseIfEmpty(nte);
166 std::pair<bool, Name>
170 if (nte ==
nullptr) {
171 return {
false, Name()};
175 if (entry ==
nullptr) {
176 return {
false, Name()};
184 StrategyChoice::findEffectiveStrategyImpl(
const K& key)
const 187 BOOST_ASSERT(nte !=
nullptr);
194 return this->findEffectiveStrategyImpl(prefix);
200 return this->findEffectiveStrategyImpl(pitEntry);
206 return this->findEffectiveStrategyImpl(measurementsEntry);
214 for (
const shared_ptr<pit::Entry>& pitEntry : nte.
getPitEntries()) {
215 pitEntry->clearStrategyInfo();
216 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
219 for (
const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
229 StrategyChoice::changeStrategy(
Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
231 const Name& oldInstanceName = oldStrategy.getInstanceName();
232 const Name& newInstanceName = newStrategy.getInstanceName();
233 if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
236 << oldInstanceName <<
" -> " << newInstanceName
242 << oldInstanceName <<
" -> " << newInstanceName);
247 BOOST_ASSERT(rootNte !=
nullptr);
248 auto&& ntChanged = m_nameTree.partialEnumerate(entry.
getPrefix(),
250 if (&nte == rootNte) {
253 if (nte.getStrategyChoiceEntry() !=
nullptr) {
254 return {
false,
false};
264 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
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)
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)
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