NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017, 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/asserts.hpp"
30 #include "core/logger.hpp"
31 #include "fw/strategy.hpp"
32 
33 namespace nfd {
34 namespace strategy_choice {
35 
36 using fw::Strategy;
37 
39 
40 NFD_LOG_INIT("StrategyChoice");
41 
42 static inline bool
44 {
45  return nte.getStrategyChoiceEntry() != nullptr;
46 }
47 
49  : m_forwarder(forwarder)
50  , m_nameTree(m_forwarder.getNameTree())
51  , m_nItems(0)
52 {
53 }
54 
55 void
57 {
58  auto entry = make_unique<Entry>(Name());
59  entry->setStrategy(Strategy::create(strategyName, m_forwarder));
60  NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyInstanceName());
61 
62  // don't use .insert here, because it will invoke findEffectiveStrategy
63  // which expects an existing root entry
64  name_tree::Entry& nte = m_nameTree.lookup(Name());
65  nte.setStrategyChoiceEntry(std::move(entry));
66  ++m_nItems;
67 }
68 
70 StrategyChoice::insert(const Name& prefix, const Name& strategyName)
71 {
72  if (prefix.size() > NameTree::getMaxDepth()) {
73  return InsertResult::DEPTH_EXCEEDED;
74  }
75 
76  unique_ptr<Strategy> strategy;
77  try {
78  strategy = Strategy::create(strategyName, m_forwarder);
79  }
80  catch (const std::invalid_argument& e) {
81  NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
82  return InsertResult(InsertResult::EXCEPTION, e.what());
83  }
84 
85  if (strategy == nullptr) {
86  NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
87  return InsertResult::NOT_REGISTERED;
88  }
89 
90  name_tree::Entry& nte = m_nameTree.lookup(prefix, true);
91  Entry* entry = nte.getStrategyChoiceEntry();
92  Strategy* oldStrategy = nullptr;
93  if (entry != nullptr) {
94  if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
95  NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
96  return InsertResult::OK;
97  }
98  oldStrategy = &entry->getStrategy();
99  NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
100  " to " << strategy->getInstanceName());
101  }
102  else {
103  oldStrategy = &this->findEffectiveStrategy(prefix);
104  auto newEntry = make_unique<Entry>(prefix);
105  entry = newEntry.get();
106  nte.setStrategyChoiceEntry(std::move(newEntry));
107  ++m_nItems;
108  NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getInstanceName());
109  }
110 
111  this->changeStrategy(*entry, *oldStrategy, *strategy);
112  entry->setStrategy(std::move(strategy));
113  return InsertResult::OK;
114 }
115 
116 StrategyChoice::InsertResult::InsertResult(Status status, const std::string& exceptionMessage)
117  : m_status(status)
118  , m_exceptionMessage(exceptionMessage)
119 {
120 }
121 
122 std::ostream&
123 operator<<(std::ostream& os, const StrategyChoice::InsertResult& res)
124 {
125  switch (res.m_status) {
126  case StrategyChoice::InsertResult::OK:
127  return os << "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 "
134  << to_string(NameTree::getMaxDepth()) << ")";
135  }
136  return os;
137 }
138 
139 void
141 {
142  BOOST_ASSERT(prefix.size() > 0);
143 
144  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
145  if (nte == nullptr) {
146  return;
147  }
148 
149  Entry* entry = nte->getStrategyChoiceEntry();
150  if (entry == nullptr) {
151  return;
152  }
153 
154  Strategy& oldStrategy = entry->getStrategy();
155 
156  Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
157  this->changeStrategy(*entry, oldStrategy, parentStrategy);
158 
159  nte->setStrategyChoiceEntry(nullptr);
160  m_nameTree.eraseIfEmpty(nte);
161  --m_nItems;
162 }
163 
164 std::pair<bool, Name>
165 StrategyChoice::get(const Name& prefix) const
166 {
167  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
168  if (nte == nullptr) {
169  return {false, Name()};
170  }
171 
172  Entry* entry = nte->getStrategyChoiceEntry();
173  if (entry == nullptr) {
174  return {false, Name()};
175  }
176 
177  return {true, entry->getStrategyInstanceName()};
178 }
179 
180 template<typename K>
181 Strategy&
182 StrategyChoice::findEffectiveStrategyImpl(const K& key) const
183 {
184  const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
185  BOOST_ASSERT(nte != nullptr);
186  return nte->getStrategyChoiceEntry()->getStrategy();
187 }
188 
189 Strategy&
191 {
192  return this->findEffectiveStrategyImpl(prefix);
193 }
194 
195 Strategy&
197 {
198  return this->findEffectiveStrategyImpl(pitEntry);
199 }
200 
201 Strategy&
203 {
204  return this->findEffectiveStrategyImpl(measurementsEntry);
205 }
206 
207 static inline void
209 {
210  NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
211 
212  for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
213  pitEntry->clearStrategyInfo();
214  for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
215  const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
216  }
217  for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
218  const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
219  }
220  }
221  if (nte.getMeasurementsEntry() != nullptr) {
223  }
224 }
225 
226 void
227 StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
228 {
229  const Name& oldInstanceName = oldStrategy.getInstanceName();
230  const Name& newInstanceName = newStrategy.getInstanceName();
231  if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
232  // same Strategy subclass type: no need to clear StrategyInfo
233  NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
234  << oldInstanceName << " -> " << newInstanceName
235  << " 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  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 name_tree::Entry& 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>(
267 }
268 
269 } // namespace strategy_choice
270 } // namespace nfd
boost::range_iterator< Range >::type const_iterator
static void clearStrategyInfo(const name_tree::Entry &nte)
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
get effective strategy for prefix
const Name & getStrategyInstanceName() const
contains information about an Interest from an incoming face
void erase(const Name &prefix)
make prefix to inherit strategy from its parent
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
const Name & getName() const
main class of NFD
Definition: forwarder.hpp:54
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
strategy_choice::Entry * getStrategyChoiceEntry() const
InsertResult insert(const Name &prefix, const Name &strategyName)
set strategy of prefix to be strategyName
represents a Measurements entry
std::ostream & operator<<(std::ostream &os, const StrategyChoice::InsertResult &res)
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
#define NFD_LOG_ERROR(expression)
Definition: logger.hpp:57
fw::Strategy & getStrategy() const
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
an Interest table entry
Definition: pit-entry.hpp:57
NFD_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator)
size_t size() const
Get number of components.
Definition: name.hpp:154
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
std::pair< bool, Name > get(const Name &prefix) const
get strategy Name of prefix
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
void setStrategy(unique_ptr< fw::Strategy > strategy)
contains information about an Interest toward an outgoing face
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:210
std::string to_string(const V &v)
Definition: backports.hpp:84
measurements::Entry * getMeasurementsEntry() const
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
a functor to get a table entry from a name tree entry
static bool nteHasStrategyChoiceEntry(const name_tree::Entry &nte)
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
an entry in the name tree