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-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
95 
96 #ifdef DOXYGEN
97 
102  static const Name&
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 
154 
163  virtual void
164  afterReceiveLoopedInterest(const FaceEndpoint& ingress, const Interest& interest,
165  pit::Entry& pitEntry);
166 
186  virtual void
187  beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
188  const FaceEndpoint& ingress, const Data& data);
189 
194  virtual void
195  afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
196  const FaceEndpoint& ingress, const Data& data);
197 
221  virtual void
222  afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
223  const FaceEndpoint& ingress, const Data& data);
224 
246  virtual void
247  afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack,
248  const shared_ptr<pit::Entry>& pitEntry);
249 
254  virtual void
255  onDroppedInterest(const FaceEndpoint& egress, const Interest& interest);
256 
262  virtual void
263  afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry);
264 
265 protected: // actions
271  VIRTUAL_WITH_TESTS void
272  sendInterest(const shared_ptr<pit::Entry>& pitEntry,
273  const FaceEndpoint& egress, const Interest& interest);
274 
280  VIRTUAL_WITH_TESTS void
281  sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, const FaceEndpoint& egress);
282 
291  VIRTUAL_WITH_TESTS void
292  sendDataToAll(const shared_ptr<pit::Entry>& pitEntry,
293  const FaceEndpoint& ingress, const Data& data);
294 
301  VIRTUAL_WITH_TESTS void
302  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
303  {
304  this->setExpiryTimer(pitEntry, 0_ms);
305  }
306 
314  VIRTUAL_WITH_TESTS void
315  sendNack(const shared_ptr<pit::Entry>& pitEntry,
316  const FaceEndpoint& egress, const lp::NackHeader& header)
317  {
318  m_forwarder.onOutgoingNack(pitEntry, egress, header);
319  }
320 
327  void
328  sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
329  std::initializer_list<FaceEndpoint> exceptFaceEndpoints = {});
330 
333  void
334  setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
335  {
336  m_forwarder.setExpiryTimer(pitEntry, duration);
337  }
338 
339 protected: // accessors
342  const fib::Entry&
343  lookupFib(const pit::Entry& pitEntry) const;
344 
347  {
348  return m_measurements;
349  }
350 
351  Face*
352  getFace(FaceId id) const
353  {
354  return getFaceTable().get(id);
355  }
356 
357  const FaceTable&
358  getFaceTable() const
359  {
360  return m_forwarder.m_faceTable;
361  }
362 
363 protected: // instance name
365  {
367  optional<uint64_t> version;
369  };
370 
375  static ParsedInstanceName
376  parseInstanceName(const Name& input);
377 
388  static Name
389  makeInstanceName(const Name& input, const Name& strategyName);
390 
394  void
396  {
397  m_name = name;
398  }
399 
403  void
405  {
406  m_wantNewNextHopTrigger = enabled;
407  }
408 
409 private: // registry
410  typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc;
411  typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name
412 
413  static Registry&
414  getRegistry();
415 
416  static Registry::const_iterator
417  find(const Name& instanceName);
418 
419 protected: // accessors
422 
423 private: // instance fields
424  Name m_name;
425 
430  Forwarder& m_forwarder;
431 
432  MeasurementsAccessor m_measurements;
433 
434  bool m_wantNewNextHopTrigger = false;
435 };
436 
437 } // namespace fw
438 } // namespace nfd
439 
444 #define NFD_REGISTER_STRATEGY(S) \
445 static class NfdAuto ## S ## StrategyRegistrationClass \
446 { \
447 public: \
448  NfdAuto ## S ## StrategyRegistrationClass() \
449  { \
450  ::nfd::fw::Strategy::registerType<S>(); \
451  } \
452 } g_nfdAuto ## S ## StrategyRegistrationVariable
453 
454 #endif // NFD_DAEMON_FW_STRATEGY_HPP
nfd::fw::Strategy::getStrategyName
static const Name & getStrategyName()
nfd::fw::Strategy::sendNacks
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:274
nfd::fw::Strategy::getMeasurements
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:346
nfd::fw::Strategy::ParsedInstanceName::strategyName
Name strategyName
strategy name without parameters
Definition: strategy.hpp:366
ndn::lp::NackHeader
represents a Network NACK header
Definition: nack-header.hpp:58
nfd::fw::Strategy::sendInterest
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &egress, const Interest &interest)
send Interest to egress
Definition: strategy.cpp:206
nfd::fw::Strategy::Strategy
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:143
nfd::fw::Strategy::afterReceiveLoopedInterest
virtual void afterReceiveLoopedInterest(const FaceEndpoint &ingress, const Interest &interest, pit::Entry &pitEntry)
trigger after a looped Interest is received
Definition: strategy.cpp:155
ndn::PartialName
Name PartialName
Represents an arbitrary sequence of name components.
Definition: name.hpp:39
nfd::fw::Strategy::~Strategy
virtual ~Strategy()
VIRTUAL_WITH_TESTS
#define VIRTUAL_WITH_TESTS
Definition: common.hpp:39
nfd::measurements::MeasurementsAccessor
allows Strategy to access portion of Measurements table under its namespace
Definition: measurements-accessor.hpp:46
nfd::fw::Strategy::canCreate
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:86
nfd::fw::Strategy::beforeRemoveFace
signal::Signal< FaceTable, Face > & beforeRemoveFace
Definition: strategy.hpp:421
measurements-accessor.hpp
nfd::fw::Strategy::create
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:92
forwarder.hpp
nfd::fw::Strategy::areSameType
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:108
ndn::util::signal::Signal
provides a lightweight signal / event system
Definition: signal.hpp:52
nfd::fw::Strategy::getInstanceName
const Name & getInstanceName() const
Definition: strategy.hpp:112
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
nfd::fw::Strategy::afterAddFace
signal::Signal< FaceTable, Face > & afterAddFace
Definition: strategy.hpp:420
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::fw::Strategy::listRegistered
static std::set< Name > listRegistered()
Definition: strategy.cpp:114
nfd::face::FaceId
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
nfd::fw::Strategy::setInstanceName
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:395
nfd::face::Face
generalization of a network interface
Definition: face.hpp:53
nfd::FaceTable
container of all faces
Definition: face-table.hpp:39
nfd::fw::Strategy::sendNack
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &egress, const lp::NackHeader &header)
send Nack to egress
Definition: strategy.hpp:315
nfd::fw::Strategy::wantNewNextHopTrigger
bool wantNewNextHopTrigger() const
Definition: strategy.hpp:120
nfd::fw::Strategy::afterReceiveData
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger after Data is received
Definition: strategy.cpp:181
nfd::pit::Entry
An Interest table entry.
Definition: pit-entry.hpp:59
nfd::FaceEndpoint
Represents a face-endpoint pair in the forwarder.
Definition: face-endpoint.hpp:37
nfd::fw::Strategy::lookupFib
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:297
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
nfd::fw::Strategy::parseInstanceName
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:123
nfd::fib::NextHop
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:38
nfd::fw::Strategy::setExpiryTimer
void setExpiryTimer(const shared_ptr< pit::Entry > &pitEntry, time::milliseconds duration)
Schedule the PIT entry to be erased after duration.
Definition: strategy.hpp:334
nfd::fw::Strategy::rejectPendingInterest
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
Definition: strategy.hpp:302
nfd::fw::Strategy::registerType
static void registerType(const Name &strategyName=S::getStrategyName())
register a strategy type
Definition: strategy.hpp:48
nfd::fib::Entry
represents a FIB entry
Definition: fib-entry.hpp:54
nfd::fw::Strategy::onDroppedInterest
virtual void onDroppedInterest(const FaceEndpoint &egress, const Interest &interest)
trigger after Interest dropped for exceeding allowed retransmissions
Definition: strategy.cpp:200
nfd::fw::Strategy::getFace
Face * getFace(FaceId id) const
Definition: strategy.hpp:352
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
nfd::FaceTable::get
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
nfd::fw::Strategy
represents a forwarding strategy
Definition: strategy.hpp:38
nfd::fw::Strategy::afterReceiveNack
virtual void afterReceiveNack(const FaceEndpoint &ingress, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
trigger after Nack is received
Definition: strategy.cpp:193
nfd::Forwarder
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:52
nfd::fw::Strategy::getFaceTable
const FaceTable & getFaceTable() const
Definition: strategy.hpp:358
nfd::fw::Strategy::sendDataToAll
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:251
nfd::fw::Strategy::afterContentStoreHit
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:171
ndn::lp::Nack
represents a Network Nack
Definition: nack.hpp:39
nfd::fw::Strategy::sendData
void sendData(const shared_ptr< pit::Entry > &pitEntry, const Data &data, const FaceEndpoint &egress)
send data to egress
Definition: strategy.cpp:226
ndn::name
Definition: name-component-types.hpp:33
nfd::fw::Strategy::makeInstanceName
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:134
nfd::fw::Strategy::afterNewNextHop
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
trigger after new nexthop is added
Definition: strategy.cpp:219
nfd::fw::Strategy::beforeSatisfyInterest
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger before PIT entry is satisfied
Definition: strategy.cpp:163
nfd::fw::Strategy::ParsedInstanceName::version
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:367
PUBLIC_WITH_TESTS_ELSE_PROTECTED
#define PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:40
nfd::fw::Strategy::enableNewNextHopTrigger
void enableNewNextHopTrigger(bool enabled)
set whether the afterNewNextHop trigger should be invoked for this strategy
Definition: strategy.hpp:404
nfd::fw::Strategy::ParsedInstanceName::parameters
PartialName parameters
parameter components
Definition: strategy.hpp:368
nfd::fw::Strategy::afterReceiveInterest
virtual void afterReceiveInterest(const FaceEndpoint &ingress, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry)=0
trigger after Interest is received
nfd::fw::Strategy::ParsedInstanceName
Definition: strategy.hpp:365