Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, 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 | |
| 31 | namespace nfd { |
| 32 | namespace fw { |
| 33 | |
| 34 | NFD_REGISTER_STRATEGY(RandomStrategy); |
| 35 | NFD_LOG_INIT(RandomStrategy); |
| 36 | |
| 37 | RandomStrategy::RandomStrategy(Forwarder& forwarder, const Name& name) |
| 38 | : Strategy(forwarder) |
| 39 | , ProcessNackTraits(this) |
| 40 | { |
| 41 | ParsedInstanceName parsed = parseInstanceName(name); |
| 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 | } |
| 49 | this->setInstanceName(makeInstanceName(name, getStrategyName())); |
| 50 | } |
| 51 | |
| 52 | const Name& |
| 53 | RandomStrategy::getStrategyName() |
| 54 | { |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame^] | 55 | static const auto strategyName = Name("/localhost/nfd/strategy/random").appendVersion(1); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 56 | return strategyName; |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | RandomStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest, |
| 61 | const shared_ptr<pit::Entry>& pitEntry) |
| 62 | { |
| 63 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 64 | fib::NextHopList nhs; |
| 65 | |
| 66 | 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] | 67 | [&] (const auto& nh) { return isNextHopEligible(ingress.face, interest, nh, pitEntry); }); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 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); |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 74 | this->sendNack(pitEntry, ingress.face, nackHeader); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 75 | this->rejectPendingInterest(pitEntry); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | std::shuffle(nhs.begin(), nhs.end(), ndn::random::getRandomNumberEngine()); |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 80 | this->sendInterest(pitEntry, nhs.front().getFace(), interest); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
| 84 | RandomStrategy::afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack, |
| 85 | const shared_ptr<pit::Entry>& pitEntry) |
| 86 | { |
| 87 | this->processNack(ingress.face, nack, pitEntry); |
| 88 | } |
| 89 | |
| 90 | } // namespace fw |
| 91 | } // namespace nfd |