NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2018, 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] = &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
140  virtual void
141  afterReceiveInterest(const Face& inFace, const Interest& interest,
142  const shared_ptr<pit::Entry>& pitEntry) = 0;
143 
163  virtual void
164  beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
165  const Face& inFace, const Data& data);
166 
171  virtual void
172  afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
173  const Face& inFace, const Data& data);
174 
198  virtual void
199  afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
200  const Face& inFace, const Data& data);
201 
223  virtual void
224  afterReceiveNack(const Face& inFace, const lp::Nack& nack,
225  const shared_ptr<pit::Entry>& pitEntry);
226 
231  virtual void
232  onDroppedInterest(const Face& outFace, const Interest& interest);
233 
234 protected: // actions
240  VIRTUAL_WITH_TESTS void
241  sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& outFace,
242  const Interest& interest)
243  {
244  m_forwarder.onOutgoingInterest(pitEntry, outFace, interest);
245  }
246 
252  VIRTUAL_WITH_TESTS void
253  sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, const Face& outFace);
254 
263  VIRTUAL_WITH_TESTS void
264  sendDataToAll(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data);
265 
272  VIRTUAL_WITH_TESTS void
273  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
274  {
275  this->setExpiryTimer(pitEntry, 0_ms);
276  }
277 
285  VIRTUAL_WITH_TESTS void
286  sendNack(const shared_ptr<pit::Entry>& pitEntry, const Face& outFace,
287  const lp::NackHeader& header)
288  {
289  m_forwarder.onOutgoingNack(pitEntry, outFace, header);
290  }
291 
299  void
300  sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
301  std::initializer_list<const Face*> exceptFaces = std::initializer_list<const Face*>());
302 
305  void
306  setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
307  {
308  m_forwarder.setExpiryTimer(pitEntry, duration);
309  }
310 
311 protected: // accessors
314  const fib::Entry&
315  lookupFib(const pit::Entry& pitEntry) const;
316 
319  {
320  return m_measurements;
321  }
322 
323  Face*
324  getFace(FaceId id) const
325  {
326  return m_forwarder.getFace(id);
327  }
328 
329  const FaceTable&
330  getFaceTable() const
331  {
332  return m_forwarder.getFaceTable();
333  }
334 
335 protected: // instance name
337  {
341  };
342 
347  static ParsedInstanceName
348  parseInstanceName(const Name& input);
349 
360  static Name
361  makeInstanceName(const Name& input, const Name& strategyName);
362 
366  void
368  {
369  m_name = name;
370  }
371 
372 private: // registry
373  typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc;
374  typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name
375 
376  static Registry&
377  getRegistry();
378 
379  static Registry::const_iterator
380  find(const Name& instanceName);
381 
382 protected: // accessors
385 
386 private: // instance fields
387  Name m_name;
388 
393  Forwarder& m_forwarder;
394 
395  MeasurementsAccessor m_measurements;
396 };
397 
398 } // namespace fw
399 } // namespace nfd
400 
405 #define NFD_REGISTER_STRATEGY(S) \
406 static class NfdAuto ## S ## StrategyRegistrationClass \
407 { \
408 public: \
409  NfdAuto ## S ## StrategyRegistrationClass() \
410  { \
411  ::nfd::fw::Strategy::registerType<S>(); \
412  } \
413 } g_nfdAuto ## S ## StrategyRegistrationVariable
414 
415 #endif // NFD_DAEMON_FW_STRATEGY_HPP
signal::Signal< FaceTable, Face & > & afterAddFace
Definition: strategy.hpp:383
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:121
static std::set< Name > listRegistered()
Definition: strategy.cpp:112
generalization of a network interface
Definition: face.hpp:67
represents a FIB entry
Definition: fib-entry.hpp:51
PartialName parameters
parameter components
Definition: strategy.hpp:340
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:286
virtual void afterContentStoreHit(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
trigger after a Data is matched in CS
Definition: strategy.cpp:160
Strategy(Forwarder &forwarder)
construct a strategy instance
Definition: strategy.cpp:141
Face * getFace(FaceId id) const
Definition: strategy.hpp:324
#define VIRTUAL_WITH_TESTS
Definition: common.hpp:38
main class of NFD
Definition: forwarder.hpp:54
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:367
Represents an Interest packet.
Definition: interest.hpp:42
provides a lightweight signal / event system
Definition: signal.hpp:50
represents a Network Nack
Definition: nack.hpp:40
virtual void afterReceiveNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
trigger after Nack is received
Definition: strategy.cpp:182
virtual ~Strategy()
FaceTable & getFaceTable()
Definition: forwarder.hpp:70
virtual void onDroppedInterest(const Face &outFace, const Interest &interest)
trigger after Interest dropped for exceeding allowed retransmissions
Definition: strategy.cpp:190
Name strategyName
strategy name without parameters
Definition: strategy.hpp:338
container of all faces
Definition: face-table.hpp:37
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:318
an Interest table entry
Definition: pit-entry.hpp:57
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
trigger after Data is received
Definition: strategy.cpp:170
Name PartialName
Represents an arbitrary sequence of name components.
Definition: name.hpp:38
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
Definition: strategy.hpp:273
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
Represents an absolute name.
Definition: name.hpp:42
signal::Signal< FaceTable, Face & > & beforeRemoveFace
Definition: strategy.hpp:384
represents a forwarding strategy
Definition: strategy.hpp:37
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:230
void sendData(const shared_ptr< pit::Entry > &pitEntry, const Data &data, const Face &outFace)
send data to outFace
Definition: strategy.cpp:196
static const Name & getStrategyName()
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:339
allows Strategy to access portion of Measurements table under its namespace
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:90
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:252
const FaceTable & getFaceTable() const
Definition: strategy.hpp:330
const Name & getInstanceName() const
Definition: strategy.hpp:110
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:132
void setExpiryTimer(const shared_ptr< pit::Entry > &pitEntry, time::milliseconds duration)
Schedule the PIT entry to be erased after duration.
Definition: strategy.hpp:306
void sendDataToAll(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
send data to all matched and qualified faces
Definition: strategy.cpp:208
virtual void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry)=0
trigger after Interest is received
uint64_t FaceId
identifies a face
Definition: face.hpp:39
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:84
Represents a Data packet.
Definition: data.hpp:35
represents a Network NACK header
Definition: nack-header.hpp:59
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, const Interest &interest)
send Interest to outFace
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
Face * getFace(FaceId id) const
get existing Face
Definition: forwarder.hpp:80