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 
33 #include <ndn-cxx/util/concepts.hpp>
34 
35 namespace nfd {
36 namespace strategy_choice {
37 
39 
40 NFD_LOG_INIT(StrategyChoice);
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
57 StrategyChoice::setDefaultStrategy(const Name& strategyName)
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
141 StrategyChoice::erase(const Name& prefix)
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 {
185  const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
186  BOOST_ASSERT(nte != nullptr);
187  return nte->getStrategyChoiceEntry()->getStrategy();
188 }
189 
190 Strategy&
191 StrategyChoice::findEffectiveStrategy(const Name& prefix) const
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>(
267 }
268 
269 } // namespace strategy_choice
270 } // namespace nfd
fw::Strategy & getStrategy() const
Main class of NFD&#39;s forwarding engine.
Definition: forwarder.hpp:51
std::pair< bool, Name > get(const Name &prefix) const
Get strategy Name of prefix.
static void clearStrategyInfo(const name_tree::Entry &nte)
Represents a Measurements entry.
InsertResult insert(const Name &prefix, const Name &strategyName)
Set strategy of prefix to be strategyName.
Contains information about an Interest toward an outgoing face.
#define NFD_LOG_ERROR
Definition: logger.hpp:41
NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator)
#define NFD_LOG_TRACE
Definition: logger.hpp:37
strategy_choice::Entry * getStrategyChoiceEntry() const
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
void clearStrategyInfo()
Clear all StrategyInfo items.
std::ostream & operator<<(std::ostream &os, const StrategyChoice::InsertResult &res)
boost::range_iterator< Range >::type const_iterator
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
An Interest table entry.
Definition: pit-entry.hpp:58
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
Contains information about an Interest from an incoming face.
void setDefaultStrategy(const Name &strategyName)
Set the default strategy.
const Name & getStrategyInstanceName() const
An entry in the name tree.
Represents a Strategy Choice entry.
#define NFD_LOG_INFO
Definition: logger.hpp:39
measurements::Entry * getMeasurementsEntry() const
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
const Name & getPrefix() const
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
Get effective strategy for prefix.
const Name & getName() const
a functor to get a table entry from a name tree entry
static bool nteHasStrategyChoiceEntry(const name_tree::Entry &nte)
void setStrategy(unique_ptr< fw::Strategy > strategy)
void erase(const Name &prefix)
Make prefix to inherit strategy from its parent.