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 info
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 
119  bool
121  {
122  return m_wantNewNextHopTrigger;
123  }
124 
125 public: // triggers
150  virtual void
151  afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
152  const shared_ptr<pit::Entry>& pitEntry) = 0;
153 
173  virtual void
174  beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
175  const FaceEndpoint& ingress, const Data& data);
176 
181  virtual void
182  afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
183  const FaceEndpoint& ingress, const Data& data);
184 
208  virtual void
209  afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
210  const FaceEndpoint& ingress, const Data& data);
211 
233  virtual void
234  afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack,
235  const shared_ptr<pit::Entry>& pitEntry);
236 
241  virtual void
242  onDroppedInterest(const FaceEndpoint& egress, const Interest& interest);
243 
249  virtual void
250  afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry);
251 
252 protected: // actions
258  VIRTUAL_WITH_TESTS void
259  sendInterest(const shared_ptr<pit::Entry>& pitEntry,
260  const FaceEndpoint& egress, const Interest& interest);
261 
267  VIRTUAL_WITH_TESTS void
268  sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, const FaceEndpoint& egress);
269 
278  VIRTUAL_WITH_TESTS void
279  sendDataToAll(const shared_ptr<pit::Entry>& pitEntry,
280  const FaceEndpoint& ingress, const Data& data);
281 
288  VIRTUAL_WITH_TESTS void
289  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
290  {
291  this->setExpiryTimer(pitEntry, 0_ms);
292  }
293 
301  VIRTUAL_WITH_TESTS void
302  sendNack(const shared_ptr<pit::Entry>& pitEntry,
303  const FaceEndpoint& egress, const lp::NackHeader& header)
304  {
305  m_forwarder.onOutgoingNack(pitEntry, egress, header);
306  }
307 
314  void
315  sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
316  std::initializer_list<FaceEndpoint> exceptFaceEndpoints = {});
317 
320  void
321  setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
322  {
323  m_forwarder.setExpiryTimer(pitEntry, duration);
324  }
325 
326 protected: // accessors
329  const fib::Entry&
330  lookupFib(const pit::Entry& pitEntry) const;
331 
332  MeasurementsAccessor&
334  {
335  return m_measurements;
336  }
337 
338  Face*
339  getFace(FaceId id) const
340  {
341  return getFaceTable().get(id);
342  }
343 
344  const FaceTable&
345  getFaceTable() const
346  {
347  return m_forwarder.m_faceTable;
348  }
349 
350 protected: // instance name
352  {
354  optional<uint64_t> version;
355  PartialName parameters;
356  };
357 
362  static ParsedInstanceName
363  parseInstanceName(const Name& input);
364 
375  static Name
376  makeInstanceName(const Name& input, const Name& strategyName);
377 
381  void
382  setInstanceName(const Name& name)
383  {
384  m_name = name;
385  }
386 
390  void
392  {
393  m_wantNewNextHopTrigger = enabled;
394  }
395 
396 private: // registry
397  typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc;
398  typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name
399 
400  static Registry&
401  getRegistry();
402 
403  static Registry::const_iterator
404  find(const Name& instanceName);
405 
406 protected: // accessors
407  signal::Signal<FaceTable, Face>& afterAddFace;
408  signal::Signal<FaceTable, Face>& beforeRemoveFace;
409 
410 private: // instance fields
411  Name m_name;
412 
417  Forwarder& m_forwarder;
418 
419  MeasurementsAccessor m_measurements;
420 
421  bool m_wantNewNextHopTrigger = false;
422 };
423 
424 } // namespace fw
425 } // namespace nfd
426 
431 #define NFD_REGISTER_STRATEGY(S) \
432 static class NfdAuto ## S ## StrategyRegistrationClass \
433 { \
434 public: \
435  NfdAuto ## S ## StrategyRegistrationClass() \
436  { \
437  ::nfd::fw::Strategy::registerType<S>(); \
438  } \
439 } g_nfdAuto ## S ## StrategyRegistrationVariable
440 
441 #endif // NFD_DAEMON_FW_STRATEGY_HPP
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
trigger after new nexthop is added
Definition: strategy.cpp:210
Main class of NFD&#39;s forwarding engine.
Definition: forwarder.hpp:51
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:382
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:143
const Name & getInstanceName() const
Definition: strategy.hpp:112
const FaceTable & getFaceTable() const
Definition: strategy.hpp:345
bool wantNewNextHopTrigger() const
Definition: strategy.hpp:120
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:302
represents a FIB entry
Definition: fib-entry.hpp:53
signal::Signal< FaceTable, Face > & beforeRemoveFace
Definition: strategy.hpp:408
#define PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:40
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:242
container of all faces
Definition: face-table.hpp:38
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:134
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:86
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:333
virtual void afterReceiveNack(const FaceEndpoint &ingress, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
trigger after Nack is received
Definition: strategy.cpp:184
void enableNewNextHopTrigger(bool enabled)
set whether the afterNewNextHop trigger should be invoked for this strategy
Definition: strategy.hpp:391
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &egress, const Interest &interest)
send Interest to egress
Definition: strategy.cpp:197
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:108
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
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:321
PartialName parameters
parameter components
Definition: strategy.hpp:355
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:92
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:162
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:217
#define VIRTUAL_WITH_TESTS
Definition: common.hpp:39
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger after Data is received
Definition: strategy.cpp:172
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:123
static std::set< Name > listRegistered()
Definition: strategy.cpp:114
signal::Signal< FaceTable, Face > & afterAddFace
Definition: strategy.hpp:407
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:37
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:265
virtual ~Strategy()
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
virtual void onDroppedInterest(const FaceEndpoint &egress, const Interest &interest)
trigger after Interest dropped for exceeding allowed retransmissions
Definition: strategy.cpp:191
Face * getFace(FaceId id) const
Definition: strategy.hpp:339
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger before PIT entry is satisfied
Definition: strategy.cpp:154
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
Definition: strategy.hpp:289
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:288
Name strategyName
strategy name without parameters
Definition: strategy.hpp:353
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:354