33 #include <ndn-cxx/util/concepts.hpp>
50 : m_forwarder(forwarder)
51 , m_nameTree(m_forwarder.getNameTree())
58 auto entry = make_unique<Entry>(Name());
60 NFD_LOG_INFO(
"setDefaultStrategy " << entry->getStrategyInstanceName());
73 return InsertResult::DEPTH_EXCEEDED;
76 unique_ptr<Strategy> strategy;
80 catch (
const std::invalid_argument& e) {
81 NFD_LOG_ERROR(
"insert(" << prefix <<
"," << strategyName <<
") cannot create strategy: " << e.what());
85 if (strategy ==
nullptr) {
86 NFD_LOG_ERROR(
"insert(" << prefix <<
"," << strategyName <<
") strategy not registered");
87 return InsertResult::NOT_REGISTERED;
93 if (entry !=
nullptr) {
95 NFD_LOG_TRACE(
"insert(" << prefix <<
") not changing " << strategy->getInstanceName());
96 return InsertResult::OK;
100 " to " << strategy->getInstanceName());
104 auto newEntry = make_unique<Entry>(prefix);
105 entry = newEntry.get();
108 NFD_LOG_TRACE(
"insert(" << prefix <<
") new entry " << strategy->getInstanceName());
111 this->changeStrategy(*entry, *oldStrategy, *strategy);
113 return InsertResult::OK;
116 StrategyChoice::InsertResult::InsertResult(Status status,
const std::string& exceptionMessage)
118 , m_exceptionMessage(exceptionMessage)
125 switch (res.m_status) {
126 case StrategyChoice::InsertResult::OK:
128 case StrategyChoice::InsertResult::NOT_REGISTERED:
129 return os <<
"Strategy not registered";
130 case StrategyChoice::InsertResult::EXCEPTION:
131 return os <<
"Error instantiating strategy: " << res.m_exceptionMessage;
132 case StrategyChoice::InsertResult::DEPTH_EXCEEDED:
133 return os <<
"Prefix has too many components (limit is "
142 BOOST_ASSERT(prefix.size() > 0);
145 if (nte ==
nullptr) {
150 if (entry ==
nullptr) {
157 this->changeStrategy(*entry, oldStrategy, parentStrategy);
164 std::pair<bool, Name>
168 if (nte ==
nullptr) {
173 if (entry ==
nullptr) {
182 StrategyChoice::findEffectiveStrategyImpl(
const K& key)
const
185 BOOST_ASSERT(nte !=
nullptr);
192 return this->findEffectiveStrategyImpl(prefix);
198 return this->findEffectiveStrategyImpl(pitEntry);
204 return this->findEffectiveStrategyImpl(measurementsEntry);
213 pitEntry->clearStrategyInfo();
214 for (
const auto& inRecord : pitEntry->getInRecords()) {
217 for (
const auto& outRecord : pitEntry->getOutRecords()) {
227 StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
229 const Name& oldInstanceName = oldStrategy.getInstanceName();
230 const Name& newInstanceName = newStrategy.getInstanceName();
233 NFD_LOG_INFO(
"changeStrategy(" << entry.getPrefix() <<
") "
234 << oldInstanceName <<
" -> " << newInstanceName <<
" same-type");
238 NFD_LOG_INFO(
"changeStrategy(" << entry.getPrefix() <<
") "
239 << oldInstanceName <<
" -> " << newInstanceName);
243 const name_tree::Entry* rootNte = m_nameTree.
getEntry(entry);
244 BOOST_ASSERT(rootNte !=
nullptr);
246 [&rootNte] (
const name_tree::Entry& nte) -> std::pair<bool, bool> {
247 if (&nte == rootNte) {
250 if (nte.getStrategyChoiceEntry() !=
nullptr) {
251 return {false, false};
255 for (
const auto& nte : ntChanged) {
261 StrategyChoice::getRange()
const
264 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
Main class of NFD's forwarding engine.
void clearStrategyInfo()
Clear all StrategyInfo items.
Base class of all forwarding strategies.
const Name & getInstanceName() const noexcept
Returns the strategy's instance name.
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Returns a strategy instance created from instanceName.
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Returns whether two names will instantiate the same strategy type.
Represents an entry in the Measurements table.
An entry in the name tree.
measurements::Entry * getMeasurementsEntry() const
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
const Name & getName() const noexcept
strategy_choice::Entry * getStrategyChoiceEntry() const
Range partialEnumerate(const Name &prefix, const EntrySubTreeSelector &entrySubTreeSelector=AnyEntrySubTree()) const
Enumerate all entries under a prefix.
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
Delete the entry if it is empty.
Range fullEnumerate(const EntrySelector &entrySelector=AnyEntry()) const
Enumerate all entries.
static constexpr size_t getMaxDepth()
Maximum depth of the name tree.
Entry & lookup(const Name &name, size_t prefixLen)
Find or insert an entry by name.
Entry * getEntry(const EntryT &tableEntry) const
Entry * findExactMatch(const Name &name, size_t prefixLen=std::numeric_limits< size_t >::max()) const
Exact match lookup.
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
Longest prefix matching.
Represents an entry in the Interest table (PIT).
Contains information about an Interest from an incoming face.
Contains information about an Interest toward an outgoing face.
Represents an entry in the Strategy Choice table.
const Name & getStrategyInstanceName() const
fw::Strategy & getStrategy() const
void setStrategy(unique_ptr< fw::Strategy > strategy)
Represents the Strategy Choice table.
void erase(const Name &prefix)
Make prefix to inherit strategy from its parent.
boost::range_iterator< Range >::type const_iterator
StrategyChoice(Forwarder &forwarder)
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
InsertResult insert(const Name &prefix, const Name &strategyName)
Set strategy of prefix to be strategyName.
void setDefaultStrategy(const Name &strategyName)
Set the default strategy.
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
Get effective strategy for prefix.
std::pair< bool, Name > get(const Name &prefix) const
Get strategy Name of prefix.
#define NFD_LOG_INIT(name)
static void clearStrategyInfo(const name_tree::Entry &nte)
static bool nteHasStrategyChoiceEntry(const name_tree::Entry &nte)
std::ostream & operator<<(std::ostream &os, const StrategyChoice::InsertResult &res)
NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator)