blob: cf14936faaca416f53f14865f2207b526c9f5930 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
ashiqopu3ad49db2018-10-20 22:38:47 +00002/*
3 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shia6de4292016-07-12 02:08:10 +00004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi56a21bf2014-11-02 21:11:50 -070024 */
Junxiao Shic1e12362014-01-24 20:03:26 -070025
26#include "fib-entry.hpp"
Junxiao Shic1e12362014-01-24 20:03:26 -070027
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080028namespace nfd {
Junxiao Shic1e12362014-01-24 20:03:26 -070029namespace fib {
30
31Entry::Entry(const Name& prefix)
32 : m_prefix(prefix)
Junxiao Shi340d5532016-08-13 04:00:35 +000033 , m_nameTreeEntry(nullptr)
Junxiao Shic1e12362014-01-24 20:03:26 -070034{
35}
36
Junxiao Shi56a21bf2014-11-02 21:11:50 -070037NextHopList::iterator
ashiqopu3ad49db2018-10-20 22:38:47 +000038Entry::findNextHop(const Face& face, uint64_t endpointId)
Junxiao Shic1e12362014-01-24 20:03:26 -070039{
Junxiao Shi56a21bf2014-11-02 21:11:50 -070040 return std::find_if(m_nextHops.begin(), m_nextHops.end(),
ashiqopu3ad49db2018-10-20 22:38:47 +000041 [&face, endpointId] (const NextHop& nexthop) {
42 return &nexthop.getFace() == &face && nexthop.getEndpointId() == endpointId;
Junxiao Shi56a21bf2014-11-02 21:11:50 -070043 });
Junxiao Shic1e12362014-01-24 20:03:26 -070044}
45
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070046bool
ashiqopu3ad49db2018-10-20 22:38:47 +000047Entry::hasNextHop(const Face& face, uint64_t endpointId) const
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070048{
ashiqopu3ad49db2018-10-20 22:38:47 +000049 return const_cast<Entry*>(this)->findNextHop(face, endpointId) != m_nextHops.end();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070050}
51
Junxiao Shic1e12362014-01-24 20:03:26 -070052void
ashiqopu3ad49db2018-10-20 22:38:47 +000053Entry::addOrUpdateNextHop(Face& face, uint64_t endpointId, uint64_t cost)
Junxiao Shic1e12362014-01-24 20:03:26 -070054{
ashiqopu3ad49db2018-10-20 22:38:47 +000055 auto it = this->findNextHop(face, endpointId);
Junxiao Shic1e12362014-01-24 20:03:26 -070056 if (it == m_nextHops.end()) {
ashiqopu3ad49db2018-10-20 22:38:47 +000057 m_nextHops.emplace_back(face, endpointId);
Junxiao Shia6de4292016-07-12 02:08:10 +000058 it = std::prev(m_nextHops.end());
Junxiao Shic1e12362014-01-24 20:03:26 -070059 }
Junxiao Shic1e12362014-01-24 20:03:26 -070060 it->setCost(cost);
Junxiao Shic1e12362014-01-24 20:03:26 -070061 this->sortNextHops();
Junxiao Shic1e12362014-01-24 20:03:26 -070062}
63
64void
ashiqopu3ad49db2018-10-20 22:38:47 +000065Entry::removeNextHop(const Face& face, uint64_t endpointId)
Junxiao Shic1e12362014-01-24 20:03:26 -070066{
ashiqopu3ad49db2018-10-20 22:38:47 +000067 auto it = this->findNextHop(face, endpointId);
Junxiao Shi56a21bf2014-11-02 21:11:50 -070068 if (it != m_nextHops.end()) {
69 m_nextHops.erase(it);
Junxiao Shic1e12362014-01-24 20:03:26 -070070 }
Junxiao Shic1e12362014-01-24 20:03:26 -070071}
72
73void
ashiqopu3ad49db2018-10-20 22:38:47 +000074Entry::removeNextHopByFace(const Face& face)
75{
76 auto it = std::remove_if(m_nextHops.begin(), m_nextHops.end(),
77 [&face] (const NextHop& nexthop) {
78 return &nexthop.getFace() == &face;
79 });
80 m_nextHops.erase(it, m_nextHops.end());
81}
82
83void
Junxiao Shic1e12362014-01-24 20:03:26 -070084Entry::sortNextHops()
85{
Junxiao Shi56a21bf2014-11-02 21:11:50 -070086 std::sort(m_nextHops.begin(), m_nextHops.end(),
87 [] (const NextHop& a, const NextHop& b) { return a.getCost() < b.getCost(); });
Junxiao Shic1e12362014-01-24 20:03:26 -070088}
89
Junxiao Shic1e12362014-01-24 20:03:26 -070090} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080091} // namespace nfd