Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 31 | namespace nfd::fw { |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 32 | |
| 33 | NFD_REGISTER_STRATEGY(RandomStrategy); |
| 34 | NFD_LOG_INIT(RandomStrategy); |
| 35 | |
| 36 | RandomStrategy::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 Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame^] | 45 | NDN_THROW(std::invalid_argument("RandomStrategy does not support version " + |
| 46 | std::to_string(*parsed.version))); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 47 | } |
| 48 | this->setInstanceName(makeInstanceName(name, getStrategyName())); |
| 49 | } |
| 50 | |
| 51 | const Name& |
| 52 | RandomStrategy::getStrategyName() |
| 53 | { |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 54 | static const auto strategyName = Name("/localhost/nfd/strategy/random").appendVersion(1); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 55 | return strategyName; |
| 56 | } |
| 57 | |
| 58 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 59 | RandomStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress, |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 60 | const shared_ptr<pit::Entry>& pitEntry) |
| 61 | { |
| 62 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 63 | fib::NextHopList nhs; |
| 64 | |
| 65 | std::copy_if(fibEntry.getNextHops().begin(), fibEntry.getNextHops().end(), std::back_inserter(nhs), |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 66 | [&] (const auto& nh) { return isNextHopEligible(ingress.face, interest, nh, pitEntry); }); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 67 | |
| 68 | if (nhs.empty()) { |
Alex Lane | 653eb07 | 2023-07-27 22:11:46 -0400 | [diff] [blame] | 69 | NFD_LOG_INTEREST_FROM(interest, ingress, "no-nexthop"); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 70 | lp::NackHeader nackHeader; |
| 71 | nackHeader.setReason(lp::NackReason::NO_ROUTE); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 72 | this->sendNack(nackHeader, ingress.face, pitEntry); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 73 | this->rejectPendingInterest(pitEntry); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | std::shuffle(nhs.begin(), nhs.end(), ndn::random::getRandomNumberEngine()); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 78 | this->sendInterest(interest, nhs.front().getFace(), pitEntry); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 82 | RandomStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress, |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 83 | const shared_ptr<pit::Entry>& pitEntry) |
| 84 | { |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 85 | this->processNack(nack, ingress.face, pitEntry); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 88 | } // namespace nfd::fw |