strategy.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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] = &make_unique<S, Forwarder&, const Name&>;
55  }
56 
62  static bool
63  canCreate(const Name& instanceName);
64 
70  static unique_ptr<Strategy>
71  create(const Name& instanceName, Forwarder& forwarder);
72 
75  static bool
76  areSameType(const Name& instanceNameA, const Name& instanceNameB);
77 
80  static std::set<Name>
82 
83 public: // constructor, destructor, strategy name
88  explicit
89  Strategy(Forwarder& forwarder);
90 
91  virtual
92  ~Strategy();
93 
94 #ifdef DOXYGEN
95 
100  static const Name&
101  getStrategyName();
102 #endif
103 
109  const Name&
111  {
112  return m_name;
113  }
114 
115 public: // triggers
133  virtual void
134  afterReceiveInterest(const Face& inFace, const Interest& interest,
135  const shared_ptr<pit::Entry>& pitEntry) = 0;
136 
147  virtual void
148  beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
149  const Face& inFace, const Data& data);
150 
164  virtual void
165  beforeExpirePendingInterest(const shared_ptr<pit::Entry>& pitEntry);
166 
180  virtual void
181  afterReceiveNack(const Face& inFace, const lp::Nack& nack,
182  const shared_ptr<pit::Entry>& pitEntry);
183 
184 protected: // actions
190  VIRTUAL_WITH_TESTS void
191  sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
192  const Interest& interest)
193  {
194  m_forwarder.onOutgoingInterest(pitEntry, outFace, interest);
195  }
196 
203  VIRTUAL_WITH_TESTS void
204  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
205  {
206  m_forwarder.onInterestReject(pitEntry);
207  }
208 
216  VIRTUAL_WITH_TESTS void
217  sendNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
218  const lp::NackHeader& header)
219  {
220  m_forwarder.onOutgoingNack(pitEntry, outFace, header);
221  }
222 
230  void
231  sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
232  std::initializer_list<const Face*> exceptFaces = std::initializer_list<const Face*>());
233 
234 protected: // accessors
237  const fib::Entry&
238  lookupFib(const pit::Entry& pitEntry) const;
239 
240  MeasurementsAccessor&
242  {
243  return m_measurements;
244  }
245 
246  Face*
247  getFace(FaceId id) const
248  {
249  return m_forwarder.getFace(id);
250  }
251 
252  const FaceTable&
253  getFaceTable() const
254  {
255  return m_forwarder.getFaceTable();
256  }
257 
258 protected: // instance name
260  {
262  ndn::optional<uint64_t> version;
263  PartialName parameters;
264  };
265 
270  static ParsedInstanceName
271  parseInstanceName(const Name& input);
272 
283  static Name
284  makeInstanceName(const Name& input, const Name& strategyName);
285 
289  void
290  setInstanceName(const Name& name)
291  {
292  m_name = name;
293  }
294 
295 private: // registry
296  typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc;
297  typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name
298 
299  static Registry&
300  getRegistry();
301 
302  static Registry::const_iterator
303  find(const Name& instanceName);
304 
305 protected: // accessors
306  signal::Signal<FaceTable, Face&>& afterAddFace;
307  signal::Signal<FaceTable, Face&>& beforeRemoveFace;
308 
309 private: // instance fields
310  Name m_name;
311 
316  Forwarder& m_forwarder;
317 
318  MeasurementsAccessor m_measurements;
319 };
320 
321 } // namespace fw
322 } // namespace nfd
323 
328 #define NFD_REGISTER_STRATEGY(S) \
329 static class NfdAuto ## S ## StrategyRegistrationClass \
330 { \
331 public: \
332  NfdAuto ## S ## StrategyRegistrationClass() \
333  { \
334  ::nfd::fw::Strategy::registerType<S>(); \
335  } \
336 } g_nfdAuto ## S ## StrategyRegistrationVariable
337 
338 #endif // NFD_DAEMON_FW_STRATEGY_HPP
main class of NFD
Definition: forwarder.hpp:52
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:290
Strategy(Forwarder &forwarder)
construct a strategy instance
Definition: strategy.cpp:141
const Name & getInstanceName() const
Definition: strategy.hpp:110
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
const FaceTable & getFaceTable() const
Definition: strategy.hpp:253
virtual void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry)=0
trigger after Interest is received
represents a FIB entry
Definition: fib-entry.hpp:51
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, const Interest &interest)
send Interest to outFace
Definition: strategy.hpp:191
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:217
container of all faces
Definition: face-table.hpp:37
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
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:241
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:57
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:306
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
static const Name & getStrategyName()
PartialName parameters
parameter components
Definition: strategy.hpp:263
FaceTable & getFaceTable()
Definition: forwarder.hpp:68
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:90
ndn::optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:262
represents a forwarding strategy
Definition: strategy.hpp:37
#define VIRTUAL_WITH_TESTS
Definition: common.hpp:38
signal::Signal< FaceTable, Face & > & beforeRemoveFace
Definition: strategy.hpp:307
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:121
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
uint64_t FaceId
identifies a face
Definition: face.hpp:39
virtual ~Strategy()
Face * getFace(FaceId id) const
Definition: strategy.hpp:247
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
decide that a pending Interest cannot be forwarded
Definition: strategy.hpp:204
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:196
Name strategyName
strategy name without parameters
Definition: strategy.hpp:261
Face * getFace(FaceId id) const
get existing Face
Definition: forwarder.hpp:78