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 
28 #include "measurements-entry.hpp"
29 #include "pit-entry.hpp"
30 #include "core/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  , m_nItems(0)
54 {
55 }
56 
57 void
59 {
60  auto entry = make_unique<Entry>(Name());
61  entry->setStrategy(Strategy::create(strategyName, m_forwarder));
62  NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyInstanceName());
63 
64  // don't use .insert here, because it will invoke findEffectiveStrategy
65  // which expects an existing root entry
66  name_tree::Entry& nte = m_nameTree.lookup(Name());
67  nte.setStrategyChoiceEntry(std::move(entry));
68  ++m_nItems;
69 }
70 
72 StrategyChoice::insert(const Name& prefix, const Name& strategyName)
73 {
74  if (prefix.size() > NameTree::getMaxDepth()) {
75  return InsertResult::DEPTH_EXCEEDED;
76  }
77 
78  unique_ptr<Strategy> strategy;
79  try {
80  strategy = Strategy::create(strategyName, m_forwarder);
81  }
82  catch (const std::invalid_argument& e) {
83  NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
84  return InsertResult(InsertResult::EXCEPTION, e.what());
85  }
86 
87  if (strategy == nullptr) {
88  NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
89  return InsertResult::NOT_REGISTERED;
90  }
91 
92  name_tree::Entry& nte = m_nameTree.lookup(prefix);
93  Entry* entry = nte.getStrategyChoiceEntry();
94  Strategy* oldStrategy = nullptr;
95  if (entry != nullptr) {
96  if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
97  NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
98  return InsertResult::OK;
99  }
100  oldStrategy = &entry->getStrategy();
101  NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
102  " to " << strategy->getInstanceName());
103  }
104  else {
105  oldStrategy = &this->findEffectiveStrategy(prefix);
106  auto newEntry = make_unique<Entry>(prefix);
107  entry = newEntry.get();
108  nte.setStrategyChoiceEntry(std::move(newEntry));
109  ++m_nItems;
110  NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getInstanceName());
111  }
112 
113  this->changeStrategy(*entry, *oldStrategy, *strategy);
114  entry->setStrategy(std::move(strategy));
115  return InsertResult::OK;
116 }
117 
118 StrategyChoice::InsertResult::InsertResult(Status status, const std::string& exceptionMessage)
119  : m_status(status)
120  , m_exceptionMessage(exceptionMessage)
121 {
122 }
123 
124 std::ostream&
125 operator<<(std::ostream& os, const StrategyChoice::InsertResult& res)
126 {
127  switch (res.m_status) {
128  case StrategyChoice::InsertResult::OK:
129  return os << "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()) << ")";
137  }
138  return os;
139 }
140 
141 void
143 {
144  BOOST_ASSERT(prefix.size() > 0);
145 
146  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
147  if (nte == nullptr) {
148  return;
149  }
150 
151  Entry* entry = nte->getStrategyChoiceEntry();
152  if (entry == nullptr) {
153  return;
154  }
155 
156  Strategy& oldStrategy = entry->getStrategy();
157 
158  Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
159  this->changeStrategy(*entry, oldStrategy, parentStrategy);
160 
161  nte->setStrategyChoiceEntry(nullptr);
162  m_nameTree.eraseIfEmpty(nte);
163  --m_nItems;
164 }
165 
166 std::pair<bool, Name>
167 StrategyChoice::get(const Name& prefix) const
168 {
169  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
170  if (nte == nullptr) {
171  return {false, Name()};
172  }
173 
174  Entry* entry = nte->getStrategyChoiceEntry();
175  if (entry == nullptr) {
176  return {false, Name()};
177  }
178 
179  return {true, entry->getStrategyInstanceName()};
180 }
181 
182 template<typename K>
183 Strategy&
184 StrategyChoice::findEffectiveStrategyImpl(const K& key) const
185 {
187  BOOST_ASSERT(nte != nullptr);
188  return nte->getStrategyChoiceEntry()->getStrategy();
189 }
190 
191 Strategy&
193 {
194  return this->findEffectiveStrategyImpl(prefix);
195 }
196 
197 Strategy&
199 {
200  return this->findEffectiveStrategyImpl(pitEntry);
201 }
202 
203 Strategy&
205 {
206  return this->findEffectiveStrategyImpl(measurementsEntry);
207 }
208 
209 static inline void
211 {
212  NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
213 
214  for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
215  pitEntry->clearStrategyInfo();
216  for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
217  const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
218  }
219  for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
220  const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
221  }
222  }
223  if (nte.getMeasurementsEntry() != nullptr) {
225  }
226 }
227 
228 void
229 StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
230 {
231  const Name& oldInstanceName = oldStrategy.getInstanceName();
232  const Name& newInstanceName = newStrategy.getInstanceName();
233  if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
234  // same Strategy subclass type: no need to clear StrategyInfo
235  NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
236  << oldInstanceName << " -> " << newInstanceName
237  << " same-type");
238  return;
239  }
240 
241  NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
242  << oldInstanceName << " -> " << newInstanceName);
243 
244  // reset StrategyInfo on a portion of NameTree,
245  // where entry's effective strategy is covered by the changing StrategyChoice entry
246  const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
247  BOOST_ASSERT(rootNte != nullptr);
248  auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
249  [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
250  if (&nte == rootNte) {
251  return {true, true};
252  }
253  if (nte.getStrategyChoiceEntry() != nullptr) {
254  return {false, false};
255  }
256  return {true, true};
257  });
258  for (const name_tree::Entry& nte : ntChanged) {
259  clearStrategyInfo(nte);
260  }
261 }
262 
264 StrategyChoice::getRange() const
265 {
266  return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
267  boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
269 }
270 
271 } // namespace strategy_choice
272 } // namespace nfd
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:203
boost::range_iterator< Range >::type const_iterator
static void clearStrategyInfo(const name_tree::Entry &nte)
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
#define NFD_LOG_ERROR
Definition: logger.hpp:41
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
Definition: logger.hpp:39
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
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
#define NFD_LOG_TRACE
Definition: logger.hpp:37
an Interest table entry
Definition: pit-entry.hpp:59
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:43
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:147
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:91
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
fw::Strategy & getStrategy() const
represents the Strategy Choice table
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:67
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:31
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:107
an entry in the name tree
static constexpr size_t getMaxDepth()
maximum depth of the name tree
Definition: name-tree.hpp:53