blob: b4950e70f7137a610a6d35bc48eff18845f79f9a [file] [log] [blame]
Klaus Schneidercf1d0c02019-08-31 19:05:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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()) {
45 NDN_THROW(std::invalid_argument(
46 "RandomStrategy does not support version " + to_string(*parsed.version)));
47 }
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()) {
69 NFD_LOG_DEBUG(interest << " from=" << ingress << " no nexthop");
70
71 lp::NackHeader nackHeader;
72 nackHeader.setReason(lp::NackReason::NO_ROUTE);
Davide Pesavento0498ce82021-06-14 02:02:21 -040073 this->sendNack(nackHeader, ingress.face, pitEntry);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070074 this->rejectPendingInterest(pitEntry);
75 return;
76 }
77
78 std::shuffle(nhs.begin(), nhs.end(), ndn::random::getRandomNumberEngine());
Davide Pesavento0498ce82021-06-14 02:02:21 -040079 this->sendInterest(interest, nhs.front().getFace(), pitEntry);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070080}
81
82void
Davide Pesavento0498ce82021-06-14 02:02:21 -040083RandomStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070084 const shared_ptr<pit::Entry>& pitEntry)
85{
Davide Pesavento0498ce82021-06-14 02:02:21 -040086 this->processNack(nack, ingress.face, pitEntry);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070087}
88
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040089} // namespace nfd::fw