NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
strategy-choice.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "strategy-choice.hpp"
27 #include "measurements-entry.hpp"
28 #include "pit-entry.hpp"
29 
30 #include "common/logger.hpp"
31 #include "fw/strategy.hpp"
32 
34 
35 namespace nfd {
36 namespace strategy_choice {
37 
39 
41 
42 using fw::Strategy;
43 
44 static inline bool
46 {
47  return nte.getStrategyChoiceEntry() != nullptr;
48 }
49 
51  : m_forwarder(forwarder)
52  , m_nameTree(m_forwarder.getNameTree())
53 {
54 }
55 
56 void
58 {
59  auto entry = make_unique<Entry>(Name());
60  entry->setStrategy(Strategy::create(strategyName, m_forwarder));
61  NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyInstanceName());
62 
63  // don't use .insert here, because it will invoke findEffectiveStrategy
64  // which expects an existing root entry
65  name_tree::Entry& nte = m_nameTree.lookup(Name());
67  ++m_nItems;
68 }
69 
71 StrategyChoice::insert(const Name& prefix, const Name& strategyName)
72 {
73  if (prefix.size() > NameTree::getMaxDepth()) {
74  return InsertResult::DEPTH_EXCEEDED;
75  }
76 
77  unique_ptr<Strategy> strategy;
78  try {
79  strategy = Strategy::create(strategyName, m_forwarder);
80  }
81  catch (const std::invalid_argument& e) {
82  NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
83  return InsertResult(InsertResult::EXCEPTION, e.what());
84  }
85 
86  if (strategy == nullptr) {
87  NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
88  return InsertResult::NOT_REGISTERED;
89  }
90 
91  name_tree::Entry& nte = m_nameTree.lookup(prefix);
92  Entry* entry = nte.getStrategyChoiceEntry();
93  Strategy* oldStrategy = nullptr;
94  if (entry != nullptr) {
95  if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
96  NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
97  return InsertResult::OK;
98  }
99  oldStrategy = &entry->getStrategy();
100  NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
101  " to " << strategy->getInstanceName());
102  }
103  else {
104  oldStrategy = &this->findEffectiveStrategy(prefix);
105  auto newEntry = make_unique<Entry>(prefix);
106  entry = newEntry.get();
107  nte.setStrategyChoiceEntry(std::move(newEntry));
108  ++m_nItems;
109  NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getInstanceName());
110  }
111 
112  this->changeStrategy(*entry, *oldStrategy, *strategy);
113  entry->setStrategy(std::move(strategy));
114  return InsertResult::OK;
115 }
116 
117 StrategyChoice::InsertResult::InsertResult(Status status, const std::string& exceptionMessage)
118  : m_status(status)
119  , m_exceptionMessage(exceptionMessage)
120 {
121 }
122 
123 std::ostream&
124 operator<<(std::ostream& os, const StrategyChoice::InsertResult& res)
125 {
126  switch (res.m_status) {
127  case StrategyChoice::InsertResult::OK:
128  return os << "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()) << ")";
136  }
137  return os;
138 }
139 
140 void
142 {
143  BOOST_ASSERT(prefix.size() > 0);
144 
145  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
146  if (nte == nullptr) {
147  return;
148  }
149 
150  Entry* entry = nte->getStrategyChoiceEntry();
151  if (entry == nullptr) {
152  return;
153  }
154 
155  Strategy& oldStrategy = entry->getStrategy();
156 
157  Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
158  this->changeStrategy(*entry, oldStrategy, parentStrategy);
159 
160  nte->setStrategyChoiceEntry(nullptr);
161  m_nameTree.eraseIfEmpty(nte);
162  --m_nItems;
163 }
164 
165 std::pair<bool, Name>
166 StrategyChoice::get(const Name& prefix) const
167 {
168  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
169  if (nte == nullptr) {
170  return {false, {}};
171  }
172 
173  Entry* entry = nte->getStrategyChoiceEntry();
174  if (entry == nullptr) {
175  return {false, {}};
176  }
177 
178  return {true, entry->getStrategyInstanceName()};
179 }
180 
181 template<typename K>
182 Strategy&
183 StrategyChoice::findEffectiveStrategyImpl(const K& key) const
184 {
186  BOOST_ASSERT(nte != nullptr);
187  return nte->getStrategyChoiceEntry()->getStrategy();
188 }
189 
190 Strategy&
192 {
193  return this->findEffectiveStrategyImpl(prefix);
194 }
195 
196 Strategy&
198 {
199  return this->findEffectiveStrategyImpl(pitEntry);
200 }
201 
202 Strategy&
204 {
205  return this->findEffectiveStrategyImpl(measurementsEntry);
206 }
207 
208 static inline void
210 {
211  NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
212 
213  for (const auto& pitEntry : nte.getPitEntries()) {
214  pitEntry->clearStrategyInfo();
215  for (const auto& inRecord : pitEntry->getInRecords()) {
216  const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
217  }
218  for (const auto& outRecord : pitEntry->getOutRecords()) {
219  const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
220  }
221  }
222  if (nte.getMeasurementsEntry() != nullptr) {
224  }
225 }
226 
227 void
228 StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
229 {
230  const Name& oldInstanceName = oldStrategy.getInstanceName();
231  const Name& newInstanceName = newStrategy.getInstanceName();
232  if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
233  // same Strategy subclass type: no need to clear StrategyInfo
234  NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
235  << oldInstanceName << " -> " << newInstanceName << " same-type");
236  return;
237  }
238 
239  NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
240  << oldInstanceName << " -> " << newInstanceName);
241 
242  // reset StrategyInfo on a portion of NameTree,
243  // where entry's effective strategy is covered by the changing StrategyChoice entry
244  const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
245  BOOST_ASSERT(rootNte != nullptr);
246  const auto& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
247  [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
248  if (&nte == rootNte) {
249  return {true, true};
250  }
251  if (nte.getStrategyChoiceEntry() != nullptr) {
252  return {false, false};
253  }
254  return {true, true};
255  });
256  for (const auto& nte : ntChanged) {
257  clearStrategyInfo(nte);
258  }
259 }
260 
262 StrategyChoice::getRange() const
263 {
264  return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
265  boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
266  &name_tree::Entry::getStrategyChoiceEntry));
267 }
268 
269 } // namespace strategy_choice
270 } // namespace nfd
NFD_LOG_TRACE
#define NFD_LOG_TRACE
Definition: logger.hpp:37
nfd::name_tree::Entry
An entry in the name tree.
Definition: name-tree-entry.hpp:42
NFD_LOG_INFO
#define NFD_LOG_INFO
Definition: logger.hpp:39
nfd::name_tree::NameTree::getEntry
Entry * getEntry(const EntryT &tableEntry) const
Definition: name-tree.hpp:77
nfd::strategy_choice::NDN_CXX_ASSERT_FORWARD_ITERATOR
NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator)
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
measurements-entry.hpp
nfd::fw::Strategy
Strategy
Definition: strategy.cpp:38
nfd::pit::OutRecord
Contains information about an Interest toward an outgoing face.
Definition: pit-out-record.hpp:37
nfd::strategy_choice::StrategyChoice::InsertResult
Definition: strategy-choice.hpp:72
ndn::Name::size
size_t size() const
Returns the number of components.
Definition: name.hpp:153
nfd::name_tree::Entry::getPitEntries
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
Definition: name-tree-entry.hpp:125
nfd::name_tree::Range
boost::iterator_range< Iterator > Range
a Forward Range of name tree entries
Definition: name-tree-iterator.hpp:212
nfd::name_tree::NameTree::eraseIfEmpty
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
Delete the entry if it is empty.
Definition: name-tree.cpp:123
nfd::name_tree::NameTree::lookup
Entry & lookup(const Name &name, size_t prefixLen)
Find or insert an entry by name.
Definition: name-tree.cpp:44
concepts.hpp
nfd::fw::Strategy::create
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:92
nfd::fw::Strategy::areSameType
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:108
nfd::name_tree::NameTree::findExactMatch
Entry * findExactMatch(const Name &name, size_t prefixLen=std::numeric_limits< size_t >::max()) const
Exact match lookup.
Definition: name-tree.cpp:150
nfd::fw::Strategy::getInstanceName
const Name & getInstanceName() const
Definition: strategy.hpp:112
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
nfd::name_tree::Entry::getName
const Name & getName() const
Definition: name-tree-entry.hpp:47
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
nfd::StrategyInfoHost::clearStrategyInfo
void clearStrategyInfo()
Clear all StrategyInfo items.
Definition: strategy-info-host.hpp:93
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
pit-entry.hpp
nfd::strategy_choice::Entry::getStrategyInstanceName
const Name & getStrategyInstanceName() const
Definition: strategy-choice-entry.cpp:40
nfd::strategy_choice::StrategyChoice::findEffectiveStrategy
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
Get effective strategy for prefix.
Definition: strategy-choice.cpp:191
nfd::strategy_choice::StrategyChoice::const_iterator
boost::range_iterator< Range >::type const_iterator
Definition: strategy-choice.hpp:156
ndn::Name::getPrefix
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:211
nfd::strategy_choice::operator<<
std::ostream & operator<<(std::ostream &os, const StrategyChoice::InsertResult &res)
Definition: strategy-choice.cpp:124
nfd::name_tree::Entry::getStrategyChoiceEntry
strategy_choice::Entry * getStrategyChoiceEntry() const
Definition: name-tree-entry.hpp:146
nfd::pit::Entry
An Interest table entry.
Definition: pit-entry.hpp:59
nfd::pit::InRecord
Contains information about an Interest from an incoming face.
Definition: pit-in-record.hpp:37
nfd::strategy_choice::StrategyChoice::setDefaultStrategy
void setDefaultStrategy(const Name &strategyName)
Set the default strategy.
Definition: strategy-choice.cpp:57
nfd::name_tree::Entry::getMeasurementsEntry
measurements::Entry * getMeasurementsEntry() const
Definition: name-tree-entry.hpp:137
nfd::strategy_choice::StrategyChoice::erase
void erase(const Name &prefix)
Make prefix to inherit strategy from its parent.
Definition: strategy-choice.cpp:141
nfd::strategy_choice::Entry
Represents a Strategy Choice entry.
Definition: strategy-choice-entry.hpp:46
nfd::fw::Strategy
represents a forwarding strategy
Definition: strategy.hpp:38
nfd::Forwarder
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:52
nfd::strategy_choice::StrategyChoice::StrategyChoice
StrategyChoice(Forwarder &forwarder)
Definition: strategy-choice.cpp:50
nfd::name_tree::NameTree::partialEnumerate
Range partialEnumerate(const Name &prefix, const EntrySubTreeSelector &entrySubTreeSelector=AnyEntrySubTree()) const
Enumerate all entries under a prefix.
Definition: name-tree.cpp:232
nfd::name_tree::NameTree::getMaxDepth
static constexpr size_t getMaxDepth()
Maximum depth of the name tree.
Definition: name-tree.hpp:51
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
nfd::measurements::Entry
Represents a Measurements entry.
Definition: measurements-entry.hpp:42
nfd::strategy_choice::Entry::getStrategy
fw::Strategy & getStrategy() const
Definition: strategy-choice-entry.hpp:69
strategy.hpp
nfd::strategy_choice::nteHasStrategyChoiceEntry
static bool nteHasStrategyChoiceEntry(const name_tree::Entry &nte)
Definition: strategy-choice.cpp:45
nfd::strategy_choice::StrategyChoice
Represents the Strategy Choice table.
Definition: strategy-choice.hpp:52
nfd::strategy_choice::Entry::setStrategy
void setStrategy(unique_ptr< fw::Strategy > strategy)
Definition: strategy-choice-entry.cpp:46
nfd::name_tree::NameTree::findLongestPrefixMatch
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
Longest prefix matching.
Definition: name-tree.cpp:162
NFD_LOG_ERROR
#define NFD_LOG_ERROR
Definition: logger.hpp:41
strategy-choice.hpp
nfd::strategy_choice::StrategyChoice::get
std::pair< bool, Name > get(const Name &prefix) const
Get strategy Name of prefix.
Definition: strategy-choice.cpp:166
nfd::name_tree::Entry::setStrategyChoiceEntry
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
Definition: name-tree-entry.cpp:128
nfd::strategy_choice::StrategyChoice::insert
InsertResult insert(const Name &prefix, const Name &strategyName)
Set strategy of prefix to be strategyName.
Definition: strategy-choice.cpp:71
nfd::strategy_choice::clearStrategyInfo
static void clearStrategyInfo(const name_tree::Entry &nte)
Definition: strategy-choice.cpp:209
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
logger.hpp