Loading...
Searching...
No Matches
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-2024, 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
31namespace nfd::fw {
32
35
36RandomStrategy::RandomStrategy(Forwarder& forwarder, const Name& name)
37 : Strategy(forwarder)
38 , ProcessNackTraits(this)
39{
41 if (!parsed.parameters.empty()) {
42 NDN_THROW(std::invalid_argument("RandomStrategy does not accept parameters"));
43 }
44 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
45 NDN_THROW(std::invalid_argument("RandomStrategy does not support version " +
46 std::to_string(*parsed.version)));
47 }
49}
50
51const Name&
53{
54 static const auto strategyName = Name("/localhost/nfd/strategy/random").appendVersion(1);
55 return strategyName;
56}
57
58void
59RandomStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
60 const shared_ptr<pit::Entry>& pitEntry)
61{
62 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
64
65 std::copy_if(fibEntry.getNextHops().begin(), fibEntry.getNextHops().end(), std::back_inserter(nhs),
66 [&] (const auto& nh) { return isNextHopEligible(ingress.face, interest, nh, pitEntry); });
67
68 if (nhs.empty()) {
69 NFD_LOG_INTEREST_FROM(interest, ingress, "no-nexthop");
70 lp::NackHeader nackHeader;
71 nackHeader.setReason(lp::NackReason::NO_ROUTE);
72 this->sendNack(nackHeader, ingress.face, pitEntry);
73 this->rejectPendingInterest(pitEntry);
74 return;
75 }
76
77 std::shuffle(nhs.begin(), nhs.end(), ndn::random::getRandomNumberEngine());
78 this->sendInterest(interest, nhs.front().getFace(), pitEntry);
79}
80
81void
82RandomStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
83 const shared_ptr<pit::Entry>& pitEntry)
84{
85 this->processNack(nack, ingress.face, pitEntry);
86}
87
88} // namespace nfd::fw
This file contains common algorithms used by forwarding strategies.
Represents a face-endpoint pair in the forwarder.
Main class of NFD's forwarding engine.
Definition forwarder.hpp:54
Represents an entry in the FIB.
Definition fib-entry.hpp:91
const NextHopList & getNextHops() const noexcept
void processNack(const lp::Nack &nack, const Face &inFace, const shared_ptr< pit::Entry > &pitEntry)
A forwarding strategy that randomly chooses a nexthop.
static const Name & getStrategyName()
void afterReceiveNack(const lp::Nack &nack, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
Trigger after a Nack is received.
void afterReceiveInterest(const Interest &interest, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
Trigger after an Interest is received.
RandomStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
Base class of all forwarding strategies.
Definition strategy.hpp:46
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition strategy.hpp:335
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition strategy.cpp:313
bool sendNack(const lp::NackHeader &header, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Nack packet.
Definition strategy.hpp:351
pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition strategy.cpp:236
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition strategy.cpp:123
void setInstanceName(const Name &name) noexcept
Set strategy instance name.
Definition strategy.hpp:448
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition strategy.cpp:134
#define NFD_LOG_INIT(name)
Definition logger.hpp:31
std::vector< NextHop > NextHopList
A collection of nexthops in a FIB entry.
Definition fib-entry.hpp:84
#define NFD_LOG_INTEREST_FROM(interest, ingress, msg)
Logs the reception of interest on ingress, followed by msg, at DEBUG level.
Definition strategy.hpp:542
#define NFD_REGISTER_STRATEGY(S)
Registers a forwarding strategy.
Definition strategy.hpp:531
std::optional< uint64_t > version
The strategy version number, if present.
Definition strategy.hpp:420
PartialName parameters
Parameter components, may be empty.
Definition strategy.hpp:421