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-2018, 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 #include "core/logger.hpp"
30 #include "fw/strategy.hpp"
31 
32 #include <ndn-cxx/util/concepts.hpp>
33 
34 namespace nfd {
35 namespace strategy_choice {
36 
38 
39 NFD_LOG_INIT("StrategyChoice");
40 
41 using fw::Strategy;
42 
43 static inline bool
45 {
46  return nte.getStrategyChoiceEntry() != nullptr;
47 }
48 
50  : m_forwarder(forwarder)
51  , m_nameTree(m_forwarder.getNameTree())
52  , m_nItems(0)
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());
66  nte.setStrategyChoiceEntry(std::move(entry));
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, Name()};
171  }
172 
173  Entry* entry = nte->getStrategyChoiceEntry();
174  if (entry == nullptr) {
175  return {false, Name()};
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 shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
214  pitEntry->clearStrategyInfo();
215  for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
216  const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
217  }
218  for (const pit::OutRecord& 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
236  << " same-type");
237  return;
238  }
239 
240  NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
241  << oldInstanceName << " -> " << newInstanceName);
242 
243  // reset StrategyInfo on a portion of NameTree,
244  // where entry's effective strategy is covered by the changing StrategyChoice entry
245  const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
246  BOOST_ASSERT(rootNte != nullptr);
247  auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
248  [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
249  if (&nte == rootNte) {
250  return {true, true};
251  }
252  if (nte.getStrategyChoiceEntry() != nullptr) {
253  return {false, false};
254  }
255  return {true, true};
256  });
257  for (const name_tree::Entry& nte : ntChanged) {
258  clearStrategyInfo(nte);
259  }
260 }
261 
263 StrategyChoice::getRange() const
264 {
265  return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
266  boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
268 }
269 
270 } // namespace strategy_choice
271 } // namespace nfd
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:210
boost::range_iterator< Range >::type const_iterator
static void clearStrategyInfo(const name_tree::Entry &nte)
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator)
contains information about an Interest from an incoming face
void erase(const Name &prefix)
make prefix to inherit strategy from its parent
std::pair< bool, Name > get(const Name &prefix) const
get strategy Name of prefix
main class of NFD
Definition: forwarder.hpp:54
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
Entry & lookup(const Name &name, size_t prefixLen)
find or insert an entry by name
Definition: name-tree.cpp:44
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
longest prefix matching
Definition: name-tree.cpp:162
const Name & getStrategyInstanceName() const
InsertResult insert(const Name &prefix, const Name &strategyName)
set strategy of prefix to be strategyName
represents a Measurements entry
Range partialEnumerate(const Name &prefix, const EntrySubTreeSelector &entrySubTreeSelector=AnyEntrySubTree()) const
enumerate all entries under a prefix
Definition: name-tree.cpp:232
std::ostream & operator<<(std::ostream &os, const StrategyChoice::InsertResult &res)
measurements::Entry * getMeasurementsEntry() const
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
#define NFD_LOG_ERROR(expression)
Definition: logger.hpp:57
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
an Interest table entry
Definition: pit-entry.hpp:57
Entry * getEntry(const EntryT &tableEntry) const
Definition: name-tree.hpp:79
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
get effective strategy for prefix
void clearStrategyInfo()
clear all StrategyInfo items
Represents an absolute name.
Definition: name.hpp:42
void setDefaultStrategy(const Name &strategyName)
set the default strategy
represents a Strategy Choice entry
Range fullEnumerate(const EntrySelector &entrySelector=AnyEntry()) const
enumerate all entries
Definition: name-tree.cpp:226
size_t size() const
Get number of components.
Definition: name.hpp:154
represents a forwarding strategy
Definition: strategy.hpp:37
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
delete the entry if it is empty
Definition: name-tree.cpp:123
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:90
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
fw::Strategy & getStrategy() const
void setStrategy(unique_ptr< fw::Strategy > strategy)
const Name & getInstanceName() const
Definition: strategy.hpp:110
contains information about an Interest toward an outgoing face
std::string to_string(const V &v)
Definition: backports.hpp:107
strategy_choice::Entry * getStrategyChoiceEntry() const
Entry * findExactMatch(const Name &name, size_t prefixLen=std::numeric_limits< size_t >::max()) const
exact match lookup
Definition: name-tree.cpp:150
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
static bool nteHasStrategyChoiceEntry(const name_tree::Entry &nte)
const Name & getName() const
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:106
an entry in the name tree
static constexpr size_t getMaxDepth()
maximum depth of the name tree
Definition: name-tree.hpp:53