strategy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "strategy.hpp"
27 #include "forwarder.hpp"
28 #include "core/logger.hpp"
29 #include "core/random.hpp"
30 #include <boost/range/adaptor/map.hpp>
31 #include <boost/range/algorithm/copy.hpp>
32 
33 namespace nfd {
34 namespace fw {
35 
36 NFD_LOG_INIT("Strategy");
37 
38 Strategy::Registry&
39 Strategy::getRegistry()
40 {
41  static Registry registry;
42  return registry;
43 }
44 
45 Strategy::Registry::const_iterator
46 Strategy::find(const Name& instanceName)
47 {
48  const Registry& registry = getRegistry();
49  ParsedInstanceName parsed = parseInstanceName(instanceName);
50 
51  if (parsed.version) {
52  // specified version: find exact or next higher version
53 
54  auto found = registry.lower_bound(parsed.strategyName);
55  if (found != registry.end()) {
56  if (parsed.strategyName.getPrefix(-1).isPrefixOf(found->first)) {
57  NFD_LOG_TRACE("find " << instanceName << " versioned found=" << found->first);
58  return found;
59  }
60  }
61 
62  NFD_LOG_TRACE("find " << instanceName << " versioned not-found");
63  return registry.end();
64  }
65 
66  // no version specified: find highest version
67 
68  if (!parsed.strategyName.empty()) { // Name().getSuccessor() would be invalid
69  auto found = registry.lower_bound(parsed.strategyName.getSuccessor());
70  if (found != registry.begin()) {
71  --found;
72  if (parsed.strategyName.isPrefixOf(found->first)) {
73  NFD_LOG_TRACE("find " << instanceName << " unversioned found=" << found->first);
74  return found;
75  }
76  }
77  }
78 
79  NFD_LOG_TRACE("find " << instanceName << " unversioned not-found");
80  return registry.end();
81 }
82 
83 bool
84 Strategy::canCreate(const Name& instanceName)
85 {
86  return Strategy::find(instanceName) != getRegistry().end();
87 }
88 
89 unique_ptr<Strategy>
90 Strategy::create(const Name& instanceName, Forwarder& forwarder)
91 {
92  auto found = Strategy::find(instanceName);
93  if (found == getRegistry().end()) {
94  NFD_LOG_DEBUG("create " << instanceName << " not-found");
95  return nullptr;
96  }
97 
98  unique_ptr<Strategy> instance = found->second(forwarder, instanceName);
99  NFD_LOG_DEBUG("create " << instanceName << " found=" << found->first <<
100  " created=" << instance->getInstanceName());
101  BOOST_ASSERT(!instance->getInstanceName().empty());
102  return instance;
103 }
104 
105 bool
106 Strategy::areSameType(const Name& instanceNameA, const Name& instanceNameB)
107 {
108  return Strategy::find(instanceNameA) == Strategy::find(instanceNameB);
109 }
110 
111 std::set<Name>
113 {
114  std::set<Name> strategyNames;
115  boost::copy(getRegistry() | boost::adaptors::map_keys,
116  std::inserter(strategyNames, strategyNames.end()));
117  return strategyNames;
118 }
119 
121 Strategy::parseInstanceName(const Name& input)
122 {
123  for (ssize_t i = input.size() - 1; i > 0; --i) {
124  if (input[i].isVersion()) {
125  return {input.getPrefix(i + 1), input[i].toVersion(), input.getSubName(i + 1)};
126  }
127  }
128  return {input, ndn::nullopt, PartialName()};
129 }
130 
131 Name
132 Strategy::makeInstanceName(const Name& input, const Name& strategyName)
133 {
134  BOOST_ASSERT(strategyName.at(-1).isVersion());
135 
136  bool hasVersion = std::any_of(input.rbegin(), input.rend(),
137  [] (const name::Component& comp) { return comp.isVersion(); });
138  return hasVersion ? input : Name(input).append(strategyName.at(-1));
139 }
140 
142  : afterAddFace(forwarder.getFaceTable().afterAdd)
143  , beforeRemoveFace(forwarder.getFaceTable().beforeRemove)
144  , m_forwarder(forwarder)
145  , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
146 {
147 }
148 
149 Strategy::~Strategy() = default;
150 
151 void
152 Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
153  const Face& inFace, const Data& data)
154 {
155  NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName() <<
156  " inFace=" << inFace.getId() << " data=" << data.getName());
157 }
158 
159 void
160 Strategy::beforeExpirePendingInterest(const shared_ptr<pit::Entry>& pitEntry)
161 {
162  NFD_LOG_DEBUG("beforeExpirePendingInterest pitEntry=" << pitEntry->getName());
163 }
164 
165 void
166 Strategy::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
167  const shared_ptr<pit::Entry>& pitEntry)
168 {
169  NFD_LOG_DEBUG("afterReceiveNack inFace=" << inFace.getId() <<
170  " pitEntry=" << pitEntry->getName());
171 }
172 
173 void
174 Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
175  std::initializer_list<const Face*> exceptFaces)
176 {
177  // populate downstreams with all downstreams faces
178  std::unordered_set<const Face*> downstreams;
179  std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()),
180  [] (const pit::InRecord& inR) { return &inR.getFace(); });
181 
182  // delete excluded faces
183  // .erase in a loop is more efficient than std::set_difference because that requires sorted range
184  for (const Face* exceptFace : exceptFaces) {
185  downstreams.erase(exceptFace);
186  }
187 
188  // send Nacks
189  for (const Face* downstream : downstreams) {
190  this->sendNack(pitEntry, *downstream, header);
191  }
192  // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
193 }
194 
195 const fib::Entry&
196 Strategy::lookupFib(const pit::Entry& pitEntry) const
197 {
198  const Fib& fib = m_forwarder.getFib();
199  const NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable();
200 
201  const Interest& interest = pitEntry.getInterest();
202  // has Link object?
203  if (!interest.hasLink()) {
204  // FIB lookup with Interest name
205  const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
206  NFD_LOG_TRACE("lookupFib noLinkObject found=" << fibEntry.getPrefix());
207  return fibEntry;
208  }
209 
210  const Link& link = interest.getLink();
211 
212  // in producer region?
213  if (nrt.isInProducerRegion(link)) {
214  // FIB lookup with Interest name
215  const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
216  NFD_LOG_TRACE("lookupFib inProducerRegion found=" << fibEntry.getPrefix());
217  return fibEntry;
218  }
219 
220  // has SelectedDelegation?
221  if (interest.hasSelectedDelegation()) {
222  // FIB lookup with SelectedDelegation
223  Name selectedDelegation = interest.getSelectedDelegation();
224  const fib::Entry& fibEntry = fib.findLongestPrefixMatch(selectedDelegation);
225  NFD_LOG_TRACE("lookupFib hasSelectedDelegation=" << selectedDelegation << " found=" << fibEntry.getPrefix());
226  return fibEntry;
227  }
228 
229  // FIB lookup with first delegation Name
230  const fib::Entry& fibEntry0 = fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
231  // in default-free zone?
232  bool isDefaultFreeZone = !(fibEntry0.getPrefix().size() == 0 && fibEntry0.hasNextHops());
233  if (!isDefaultFreeZone) {
234  NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry0.getPrefix());
235  return fibEntry0;
236  }
237 
238  // choose and set SelectedDelegation
239  for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
240  const Name& delegationName = delegation.second;
241  const fib::Entry& fibEntry = fib.findLongestPrefixMatch(delegationName);
242  if (fibEntry.hasNextHops()) {
245  std::for_each(pitEntry.in_begin(), pitEntry.in_end(),
246  [&delegationName] (const pit::InRecord& inR) {
247  const_cast<Interest&>(inR.getInterest()).setSelectedDelegation(delegationName);
248  });
249  NFD_LOG_TRACE("lookupFib enterDefaultFreeZone setSelectedDelegation=" << delegationName);
250  return fibEntry;
251  }
252  }
253  BOOST_ASSERT(false);
254  return fibEntry0;
255 }
256 
257 } // namespace fw
258 } // namespace nfd
main class of NFD
Definition: forwarder.hpp:52
Strategy(Forwarder &forwarder)
construct a strategy instance
Definition: strategy.cpp:141
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:161
void sendNacks(const shared_ptr< pit::Entry > &pitEntry, const lp::NackHeader &header, std::initializer_list< const Face * > exceptFaces=std::initializer_list< const Face * >())
send Nack to every face that has an in-record, except those in exceptFaces
Definition: strategy.cpp:174
bool isInProducerRegion(const Link &link) const
determines whether an Interest has reached a producer region
Fib & getFib()
Definition: forwarder.hpp:135
represents a FIB entry
Definition: fib-entry.hpp:51
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:217
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
trigger before PIT entry is satisfied
Definition: strategy.cpp:152
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:132
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:84
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:106
an Interest table entry
Definition: pit-entry.hpp:57
const Name & getPrefix() const
Definition: fib-entry.hpp:58
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Face & getFace() const
contains information about an Interest from an incoming face
virtual void afterReceiveNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
trigger after Nack is received
Definition: strategy.cpp:166
stores a collection of producer region names
InRecordCollection::iterator in_end()
Definition: pit-entry.hpp:122
const Interest & getInterest() const
Definition: pit-entry.hpp:69
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:90
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:121
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:160
static std::set< Name > listRegistered()
Definition: strategy.cpp:112
virtual void beforeExpirePendingInterest(const shared_ptr< pit::Entry > &pitEntry)
trigger before PIT entry expires
Definition: strategy.cpp:160
virtual ~Strategy()
NetworkRegionTable & getNetworkRegionTable()
Definition: forwarder.hpp:171
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:196
bool hasNextHops() const
Definition: fib-entry.hpp:72
InRecordCollection::iterator in_begin()
Definition: pit-entry.hpp:110