NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
random-strategy.cpp
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 #include "random-strategy.hpp"
27 #include "algorithm.hpp"
28 
29 #include <ndn-cxx/util/random.hpp>
30 
31 namespace nfd {
32 namespace fw {
33 
36 
38  : Strategy(forwarder)
39  , ProcessNackTraits(this)
40 {
42  if (!parsed.parameters.empty()) {
43  NDN_THROW(std::invalid_argument("RandomStrategy does not accept parameters"));
44  }
45  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
46  NDN_THROW(std::invalid_argument(
47  "RandomStrategy does not support version " + to_string(*parsed.version)));
48  }
50 }
51 
52 const Name&
54 {
55  static Name strategyName("/localhost/nfd/strategy/random/%FD%01");
56  return strategyName;
57 }
58 
59 void
61  const shared_ptr<pit::Entry>& pitEntry)
62 {
63  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
64  const Face& inFace = ingress.face;
65  fib::NextHopList nhs;
66 
67  std::copy_if(fibEntry.getNextHops().begin(), fibEntry.getNextHops().end(), std::back_inserter(nhs),
68  [&] (const auto& nh) { return isNextHopEligible(inFace, interest, nh, pitEntry); });
69 
70  if (nhs.empty()) {
71  NFD_LOG_DEBUG(interest << " from=" << ingress << " no nexthop");
72 
73  lp::NackHeader nackHeader;
74  nackHeader.setReason(lp::NackReason::NO_ROUTE);
75  this->sendNack(pitEntry, ingress, nackHeader);
76  this->rejectPendingInterest(pitEntry);
77  return;
78  }
79 
80  std::shuffle(nhs.begin(), nhs.end(), ndn::random::getRandomNumberEngine());
81  this->sendInterest(pitEntry, FaceEndpoint(nhs.front().getFace(), 0), interest);
82 }
83 
84 void
86  const shared_ptr<pit::Entry>& pitEntry)
87 {
88  this->processNack(ingress.face, nack, pitEntry);
89 }
90 
91 } // namespace fw
92 } // namespace nfd
nfd::FaceEndpoint::face
Face & face
Definition: face-endpoint.hpp:46
ndn::lp::NackHeader::setReason
NackHeader & setReason(NackReason reason)
set reason code
Definition: nack-header.cpp:135
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::RandomStrategy
Randomly chooses a nexthop.
Definition: random-strategy.hpp:43
nfd::fw::RandomStrategy::afterReceiveNack
void afterReceiveNack(const FaceEndpoint &ingress, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Nack is received
Definition: random-strategy.cpp:85
random.hpp
nfd::fw::NFD_REGISTER_STRATEGY
NFD_REGISTER_STRATEGY(AccessStrategy)
nfd::fw::ProcessNackTraits
Definition: process-nack-traits.hpp:71
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
ndn::random::getRandomNumberEngine
RandomNumberEngine & getRandomNumberEngine()
Returns a reference to a thread-local instance of a properly seeded PRNG.
Definition: random.cpp:54
nfd::face::Face
generalization of a network interface
Definition: face.hpp:53
nfd::fw::Strategy::setInstanceName
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:395
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
nfd::fw::RandomStrategy::RandomStrategy
RandomStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
Definition: random-strategy.cpp:37
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::fib::Entry::getNextHops
const NextHopList & getNextHops() const
Definition: fib-entry.hpp:66
nfd::fw::RandomStrategy::getStrategyName
static const Name & getStrategyName()
Definition: random-strategy.cpp:53
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::fw::Strategy::rejectPendingInterest
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
Definition: strategy.hpp:302
nfd::fib::NextHopList
std::vector< NextHop > NextHopList
Definition: fib-entry.hpp:49
nfd::fib::Entry
represents a FIB entry
Definition: fib-entry.hpp:54
nfd::fw::RandomStrategy::afterReceiveInterest
void afterReceiveInterest(const FaceEndpoint &ingress, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received
Definition: random-strategy.cpp:60
nfd::fw::Strategy
represents a forwarding strategy
Definition: strategy.hpp:38
NFD_LOG_DEBUG
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
nfd::Forwarder
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:52
nfd::fw::ProcessNackTraitsBase::processNack
void processNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
Definition: process-nack-traits.cpp:35
ndn::lp::Nack
represents a Network Nack
Definition: nack.hpp:39
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
algorithm.hpp
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
random-strategy.hpp
nfd::fw::Strategy::ParsedInstanceName::version
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:367
nfd::fw::Strategy::ParsedInstanceName::parameters
PartialName parameters
parameter components
Definition: strategy.hpp:368
nfd::fw::Strategy::ParsedInstanceName
Definition: strategy.hpp:365
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31