strategy.hpp
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 #ifndef NFD_DAEMON_FW_STRATEGY_HPP
27 #define NFD_DAEMON_FW_STRATEGY_HPP
28 
29 #include "forwarder.hpp"
31 
32 namespace nfd {
33 namespace fw {
34 
37 class Strategy : noncopyable
38 {
39 public: // registry
46  template<typename S>
47  static void
48  registerType(const Name& strategyName = S::getStrategyName())
49  {
50  BOOST_ASSERT(strategyName.size() > 1);
51  BOOST_ASSERT(strategyName.at(-1).isVersion());
52  Registry& registry = getRegistry();
53  BOOST_ASSERT(registry.count(strategyName) == 0);
54  registry[strategyName] = [] (auto&&... args) {
55  return make_unique<S>(std::forward<decltype(args)>(args)...);
56  };
57  }
58 
64  static bool
65  canCreate(const Name& instanceName);
66 
72  static unique_ptr<Strategy>
73  create(const Name& instanceName, Forwarder& forwarder);
74 
77  static bool
78  areSameType(const Name& instanceNameA, const Name& instanceNameB);
79 
82  static std::set<Name>
84 
85 public: // constructor, destructor, strategy name
90  explicit
91  Strategy(Forwarder& forwarder);
92 
93  virtual
94  ~Strategy();
95 
96 #ifdef DOXYGEN
97 
102  static const Name&
103  getStrategyName();
104 #endif
105 
111  const Name&
113  {
114  return m_name;
115  }
116 
117 public: // triggers
142  virtual void
143  afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
144  const shared_ptr<pit::Entry>& pitEntry) = 0;
145 
165  virtual void
166  beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
167  const FaceEndpoint& ingress, const Data& data);
168 
173  virtual void
174  afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
175  const FaceEndpoint& ingress, const Data& data);
176 
200  virtual void
201  afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
202  const FaceEndpoint& ingress, const Data& data);
203 
225  virtual void
226  afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack,
227  const shared_ptr<pit::Entry>& pitEntry);
228 
233  virtual void
234  onDroppedInterest(const FaceEndpoint& egress, const Interest& interest);
235 
236 protected: // actions
242  VIRTUAL_WITH_TESTS void
243  sendInterest(const shared_ptr<pit::Entry>& pitEntry,
244  const FaceEndpoint& egress, const Interest& interest)
245  {
246  m_forwarder.onOutgoingInterest(pitEntry, egress, interest);
247  }
248 
254  VIRTUAL_WITH_TESTS void
255  sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, const FaceEndpoint& egress);
256 
265  VIRTUAL_WITH_TESTS void
266  sendDataToAll(const shared_ptr<pit::Entry>& pitEntry,
267  const FaceEndpoint& ingress, const Data& data);
268 
275  VIRTUAL_WITH_TESTS void
276  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
277  {
278  this->setExpiryTimer(pitEntry, 0_ms);
279  }
280 
288  VIRTUAL_WITH_TESTS void
289  sendNack(const shared_ptr<pit::Entry>& pitEntry,
290  const FaceEndpoint& egress, const lp::NackHeader& header)
291  {
292  m_forwarder.onOutgoingNack(pitEntry, egress, header);
293  }
294 
301  void
302  sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
303  std::initializer_list<FaceEndpoint> exceptFaceEndpoints = {});
304 
307  void
308  setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
309  {
310  m_forwarder.setExpiryTimer(pitEntry, duration);
311  }
312 
313 protected: // accessors
316  const fib::Entry&
317  lookupFib(const pit::Entry& pitEntry) const;
318 
319  MeasurementsAccessor&
321  {
322  return m_measurements;
323  }
324 
325  Face*
326  getFace(FaceId id) const
327  {
328  return m_forwarder.getFace(id);
329  }
330 
331  const FaceTable&
332  getFaceTable() const
333  {
334  return m_forwarder.getFaceTable();
335  }
336 
337 protected: // instance name
339  {
341  optional<uint64_t> version;
342  PartialName parameters;
343  };
344 
349  static ParsedInstanceName
350  parseInstanceName(const Name& input);
351 
362  static Name
363  makeInstanceName(const Name& input, const Name& strategyName);
364 
368  void
369  setInstanceName(const Name& name)
370  {
371  m_name = name;
372  }
373 
374 private: // registry
375  typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc;
376  typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name
377 
378  static Registry&
379  getRegistry();
380 
381  static Registry::const_iterator
382  find(const Name& instanceName);
383 
384 protected: // accessors
385  signal::Signal<FaceTable, Face&>& afterAddFace;
386  signal::Signal<FaceTable, Face&>& beforeRemoveFace;
387 
388 private: // instance fields
389  Name m_name;
390 
395  Forwarder& m_forwarder;
396 
397  MeasurementsAccessor m_measurements;
398 };
399 
400 } // namespace fw
401 } // namespace nfd
402 
407 #define NFD_REGISTER_STRATEGY(S) \
408 static class NfdAuto ## S ## StrategyRegistrationClass \
409 { \
410 public: \
411  NfdAuto ## S ## StrategyRegistrationClass() \
412  { \
413  ::nfd::fw::Strategy::registerType<S>(); \
414  } \
415 } g_nfdAuto ## S ## StrategyRegistrationVariable
416 
417 #endif // NFD_DAEMON_FW_STRATEGY_HPP
Main class of NFD forwarding engine.
Definition: forwarder.hpp:51
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:369
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:141
const Name & getInstanceName() const
Definition: strategy.hpp:112
const FaceTable & getFaceTable() const
Definition: strategy.hpp:332
virtual void afterReceiveInterest(const FaceEndpoint &ingress, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry)=0
trigger after Interest is received
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &egress, const lp::NackHeader &header)
send Nack to egress
Definition: strategy.hpp:289
represents a FIB entry
Definition: fib-entry.hpp:51
void sendDataToAll(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
send data to all matched and qualified face-endpoint pairs
Definition: strategy.cpp:208
container of all faces
Definition: face-table.hpp:37
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
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:320
virtual void afterReceiveNack(const FaceEndpoint &ingress, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
trigger after Nack is received
Definition: strategy.cpp:182
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &egress, const Interest &interest)
send Interest to egress
Definition: strategy.hpp:243
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:106
static void registerType(const Name &strategyName=S::getStrategyName())
register a strategy type
Definition: strategy.hpp:48
An Interest table entry.
Definition: pit-entry.hpp:58
Represents a face-endpoint pair in the forwarder.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
signal::Signal< FaceTable, Face & > & afterAddFace
Definition: strategy.hpp:385
static const Name & getStrategyName()
void setExpiryTimer(const shared_ptr< pit::Entry > &pitEntry, time::milliseconds duration)
Schedule the PIT entry to be erased after duration.
Definition: strategy.hpp:308
PartialName parameters
parameter components
Definition: strategy.hpp:342
FaceTable & getFaceTable()
Definition: forwarder.hpp:67
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:90
virtual void afterContentStoreHit(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger after a Data is matched in CS
Definition: strategy.cpp:160
represents a forwarding strategy
Definition: strategy.hpp:37
void sendData(const shared_ptr< pit::Entry > &pitEntry, const Data &data, const FaceEndpoint &egress)
send data to egress
Definition: strategy.cpp:195
#define VIRTUAL_WITH_TESTS
Definition: common.hpp:39
signal::Signal< FaceTable, Face & > & beforeRemoveFace
Definition: strategy.hpp:386
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger after Data is received
Definition: strategy.cpp:170
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:121
static std::set< Name > listRegistered()
Definition: strategy.cpp:112
uint64_t FaceId
identifies a face
Definition: face.hpp:39
void sendNacks(const shared_ptr< pit::Entry > &pitEntry, const lp::NackHeader &header, std::initializer_list< FaceEndpoint > exceptFaceEndpoints={})
send Nack to every face-endpoint pair that has an in-record, except those in exceptFaceEndpoints ...
Definition: strategy.cpp:232
virtual ~Strategy()
virtual void onDroppedInterest(const FaceEndpoint &egress, const Interest &interest)
trigger after Interest dropped for exceeding allowed retransmissions
Definition: strategy.cpp:189
Face * getFace(FaceId id) const
Definition: strategy.hpp:326
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger before PIT entry is satisfied
Definition: strategy.cpp:152
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
Definition: strategy.hpp:276
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:255
Name strategyName
strategy name without parameters
Definition: strategy.hpp:340
Face * getFace(FaceId id) const
get existing Face
Definition: forwarder.hpp:77
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:341