blob: 6083dd35deed621a870f2532e4ddf05ed7f064cd [file] [log] [blame]
Spyridon Mastorakis77b63662014-11-25 19:22:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California,
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-load-balancer-strategy.hpp"
27
28#include <boost/random/uniform_int_distribution.hpp>
29
30#include <ndn-cxx/util/random.hpp>
31
32#include "core/logger.hpp"
33
34NFD_LOG_INIT("RandomLoadBalancerStrategy");
35
36namespace nfd {
37namespace fw {
38
39const Name
40 RandomLoadBalancerStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/random-load-balancer");
41
42RandomLoadBalancerStrategy::RandomLoadBalancerStrategy(Forwarder& forwarder, const Name& name)
43 : Strategy(forwarder, name)
44{
45}
46
47RandomLoadBalancerStrategy::~RandomLoadBalancerStrategy()
48{
49}
50
51static bool
52canForwardToNextHop(shared_ptr<pit::Entry> pitEntry, const fib::NextHop& nexthop)
53{
54 return pitEntry->canForwardTo(*nexthop.getFace());
55}
56
57static bool
58hasFaceForForwarding(const fib::NextHopList& nexthops, shared_ptr<pit::Entry>& pitEntry)
59{
60 return std::find_if(nexthops.begin(), nexthops.end(), bind(&canForwardToNextHop, pitEntry, _1))
61 != nexthops.end();
62}
63
64void
65RandomLoadBalancerStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
66 shared_ptr<fib::Entry> fibEntry,
67 shared_ptr<pit::Entry> pitEntry)
68{
69 NFD_LOG_TRACE("afterReceiveInterest");
70
71 if (pitEntry->hasUnexpiredOutRecords()) {
72 // not a new Interest, don't forward
73 return;
74 }
75
76 const fib::NextHopList& nexthops = fibEntry->getNextHops();
77
78 // Ensure there is at least 1 Face is available for forwarding
79 if (!hasFaceForForwarding(nexthops, pitEntry)) {
80 this->rejectPendingInterest(pitEntry);
81 return;
82 }
83
84 fib::NextHopList::const_iterator selected;
85 do {
86 boost::random::uniform_int_distribution<> dist(0, nexthops.size() - 1);
87 const size_t randomIndex = dist(m_randomGenerator);
88
89 uint64_t currentIndex = 0;
90
91 for (selected = nexthops.begin(); selected != nexthops.end() && currentIndex != randomIndex;
92 ++selected, ++currentIndex) {
93 }
94 } while (!canForwardToNextHop(pitEntry, *selected));
95
96 this->sendInterest(pitEntry, selected->getFace());
97}
98
99} // namespace fw
100} // namespace nfd