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-2021, 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 const auto strategyName = Name("/localhost/nfd/strategy/random").appendVersion(1);
56  return strategyName;
57 }
58 
59 void
61  const shared_ptr<pit::Entry>& pitEntry)
62 {
63  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
64  fib::NextHopList nhs;
65 
66  std::copy_if(fibEntry.getNextHops().begin(), fibEntry.getNextHops().end(), std::back_inserter(nhs),
67  [&] (const auto& nh) { return isNextHopEligible(ingress.face, interest, nh, pitEntry); });
68 
69  if (nhs.empty()) {
70  NFD_LOG_DEBUG(interest << " from=" << ingress << " no nexthop");
71 
72  lp::NackHeader nackHeader;
73  nackHeader.setReason(lp::NackReason::NO_ROUTE);
74  this->sendNack(nackHeader, ingress.face, pitEntry);
75  this->rejectPendingInterest(pitEntry);
76  return;
77  }
78 
79  std::shuffle(nhs.begin(), nhs.end(), ndn::random::getRandomNumberEngine());
80  this->sendInterest(interest, nhs.front().getFace(), pitEntry);
81 }
82 
83 void
85  const shared_ptr<pit::Entry>& pitEntry)
86 {
87  this->processNack(nack, ingress.face, pitEntry);
88 }
89 
90 } // namespace fw
91 } // namespace nfd
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
std::string to_string(const T &val)
Definition: backports.hpp:86
NackHeader & setReason(NackReason reason)
set reason code
Represents a face-endpoint pair in the forwarder.
represents a FIB entry
Definition: fib-entry.hpp:53
PartialName parameters
parameter components
Definition: strategy.hpp:390
RandomStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
Main class of NFD&#39;s forwarding engine.
Definition: forwarder.hpp:53
void setInstanceName(const Name &name)
Set strategy instance name.
Definition: strategy.hpp:417
Represents an Interest packet.
Definition: interest.hpp:48
Represents a collection of nexthops.
Definition: fib-entry.hpp:39
NFD_VIRTUAL_WITH_TESTS void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:319
represents a Network Nack
Definition: nack.hpp:38
#define NDN_THROW(e)
Definition: exception.hpp:61
void processNack(const lp::Nack &nack, const Face &inFace, const shared_ptr< pit::Entry > &pitEntry)
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
RandomNumberEngine & getRandomNumberEngine()
Returns a reference to a thread-local instance of a properly seeded PRNG.
Definition: random.cpp:54
static const Name & getStrategyName()
void afterReceiveInterest(const Interest &interest, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
Trigger after an Interest is received.
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
NFD_REGISTER_STRATEGY(AccessStrategy)
Represents an absolute name.
Definition: name.hpp:41
NFD_VIRTUAL_WITH_TESTS bool sendNack(const lp::NackHeader &header, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Nack packet.
Definition: strategy.hpp:335
Represents a forwarding strategy.
Definition: strategy.hpp:38
void afterReceiveNack(const lp::Nack &nack, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
Trigger after a Nack is received.
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:389
This file contains common algorithms used by forwarding strategies.
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition: strategy.cpp:331
bool isNextHopEligible(const Face &inFace, const Interest &interest, const fib::NextHop &nexthop, const shared_ptr< pit::Entry > &pitEntry, bool wantUnused, time::steady_clock::time_point now)
Definition: algorithm.cpp:131
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition: strategy.cpp:134
const NextHopList & getNextHops() const
Definition: fib-entry.hpp:66
NFD_VIRTUAL_WITH_TESTS pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition: strategy.cpp:237
represents a Network NACK header
Definition: nack-header.hpp:57
A forwarding strategy that randomly chooses a nexthop.