blob: 1cda97802d6fbfb60cd75f92224f73901a3e1afc [file] [log] [blame]
Klaus Schneidercf1d0c02019-08-31 19:05:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Klaus Schneidercf1d0c02019-08-31 19:05:40 -07004 * 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::fw {
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070032
33NFD_REGISTER_STRATEGY(RandomStrategy);
34NFD_LOG_INIT(RandomStrategy);
35
36RandomStrategy::RandomStrategy(Forwarder& forwarder, const Name& name)
37 : Strategy(forwarder)
38 , ProcessNackTraits(this)
39{
40 ParsedInstanceName parsed = parseInstanceName(name);
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()) {
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050045 NDN_THROW(std::invalid_argument("RandomStrategy does not support version " +
46 std::to_string(*parsed.version)));
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070047 }
48 this->setInstanceName(makeInstanceName(name, getStrategyName()));
49}
50
51const Name&
52RandomStrategy::getStrategyName()
53{
Eric Newberry358414d2021-03-21 20:56:42 -070054 static const auto strategyName = Name("/localhost/nfd/strategy/random").appendVersion(1);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070055 return strategyName;
56}
57
58void
Davide Pesavento0498ce82021-06-14 02:02:21 -040059RandomStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070060 const shared_ptr<pit::Entry>& pitEntry)
61{
62 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070063 fib::NextHopList nhs;
64
65 std::copy_if(fibEntry.getNextHops().begin(), fibEntry.getNextHops().end(), std::back_inserter(nhs),
Teng Liangebc20f62020-06-23 16:55:20 -070066 [&] (const auto& nh) { return isNextHopEligible(ingress.face, interest, nh, pitEntry); });
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070067
68 if (nhs.empty()) {
Alex Lane653eb072023-07-27 22:11:46 -040069 NFD_LOG_INTEREST_FROM(interest, ingress, "no-nexthop");
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070070 lp::NackHeader nackHeader;
71 nackHeader.setReason(lp::NackReason::NO_ROUTE);
Davide Pesavento0498ce82021-06-14 02:02:21 -040072 this->sendNack(nackHeader, ingress.face, pitEntry);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070073 this->rejectPendingInterest(pitEntry);
74 return;
75 }
76
77 std::shuffle(nhs.begin(), nhs.end(), ndn::random::getRandomNumberEngine());
Davide Pesavento0498ce82021-06-14 02:02:21 -040078 this->sendInterest(interest, nhs.front().getFace(), pitEntry);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070079}
80
81void
Davide Pesavento0498ce82021-06-14 02:02:21 -040082RandomStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070083 const shared_ptr<pit::Entry>& pitEntry)
84{
Davide Pesavento0498ce82021-06-14 02:02:21 -040085 this->processNack(nack, ingress.face, pitEntry);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070086}
87
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040088} // namespace nfd::fw