blob: 96b2ae7a983cd1e49c35cf10f291565e66248a93 [file] [log] [blame]
Klaus Schneidercf1d0c02019-08-31 19:05:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Eric Newberry358414d2021-03-21 20:56:42 -07003 * Copyright (c) 2014-2021, 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
31namespace nfd {
32namespace fw {
33
34NFD_REGISTER_STRATEGY(RandomStrategy);
35NFD_LOG_INIT(RandomStrategy);
36
37RandomStrategy::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
52const Name&
53RandomStrategy::getStrategyName()
54{
Eric Newberry358414d2021-03-21 20:56:42 -070055 static const auto strategyName = Name("/localhost/nfd/strategy/random").appendVersion(1);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070056 return strategyName;
57}
58
59void
60RandomStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
61 const shared_ptr<pit::Entry>& pitEntry)
62{
63 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070064 fib::NextHopList nhs;
65
66 std::copy_if(fibEntry.getNextHops().begin(), fibEntry.getNextHops().end(), std::back_inserter(nhs),
Teng Liangebc20f62020-06-23 16:55:20 -070067 [&] (const auto& nh) { return isNextHopEligible(ingress.face, interest, nh, pitEntry); });
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070068
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 Liangebc20f62020-06-23 16:55:20 -070074 this->sendNack(pitEntry, ingress.face, nackHeader);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070075 this->rejectPendingInterest(pitEntry);
76 return;
77 }
78
79 std::shuffle(nhs.begin(), nhs.end(), ndn::random::getRandomNumberEngine());
Teng Liangebc20f62020-06-23 16:55:20 -070080 this->sendInterest(pitEntry, nhs.front().getFace(), interest);
Klaus Schneidercf1d0c02019-08-31 19:05:40 -070081}
82
83void
84RandomStrategy::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