blob: 3ca8defbc4f66c74095916b26c53cbca1f6e82f6 [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/*
Davide Pesavento972de802024-02-16 18:42:55 -05003 * Copyright (c) 2014-2024, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040028namespace nfd::fib {
Junxiao Shic1e12362014-01-24 20:03:26 -070029
30Entry::Entry(const Name& prefix)
31 : m_prefix(prefix)
32{
33}
34
Junxiao Shi56a21bf2014-11-02 21:11:50 -070035NextHopList::iterator
Davide Pesavento972de802024-02-16 18:42:55 -050036Entry::findNextHop(const Face& face) noexcept
Junxiao Shic1e12362014-01-24 20:03:26 -070037{
Junxiao Shi56a21bf2014-11-02 21:11:50 -070038 return std::find_if(m_nextHops.begin(), m_nextHops.end(),
Davide Pesavento972de802024-02-16 18:42:55 -050039 [&face] (const NextHop& nexthop) { return &nexthop.getFace() == &face; });
Junxiao Shic1e12362014-01-24 20:03:26 -070040}
41
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070042bool
Davide Pesavento972de802024-02-16 18:42:55 -050043Entry::hasNextHop(const Face& face) const noexcept
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070044{
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000045 return const_cast<Entry*>(this)->findNextHop(face) != m_nextHops.end();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070046}
47
Ju Pand8315bf2019-07-31 06:59:07 +000048std::pair<NextHopList::iterator, bool>
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000049Entry::addOrUpdateNextHop(Face& face, uint64_t cost)
Junxiao Shic1e12362014-01-24 20:03:26 -070050{
Davide Pesavento972de802024-02-16 18:42:55 -050051 auto it = findNextHop(face);
Ju Pand8315bf2019-07-31 06:59:07 +000052 bool isNew = false;
Junxiao Shic1e12362014-01-24 20:03:26 -070053 if (it == m_nextHops.end()) {
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000054 m_nextHops.emplace_back(face);
Junxiao Shia6de4292016-07-12 02:08:10 +000055 it = std::prev(m_nextHops.end());
Ju Pand8315bf2019-07-31 06:59:07 +000056 isNew = true;
Junxiao Shic1e12362014-01-24 20:03:26 -070057 }
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000058
Junxiao Shic1e12362014-01-24 20:03:26 -070059 it->setCost(cost);
Junxiao Shic1e12362014-01-24 20:03:26 -070060 this->sortNextHops();
Ju Pand8315bf2019-07-31 06:59:07 +000061
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040062 return {it, isNew};
Junxiao Shic1e12362014-01-24 20:03:26 -070063}
64
Ju Pand8315bf2019-07-31 06:59:07 +000065bool
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000066Entry::removeNextHop(const Face& face)
Junxiao Shic1e12362014-01-24 20:03:26 -070067{
Davide Pesavento972de802024-02-16 18:42:55 -050068 auto it = findNextHop(face);
Junxiao Shi56a21bf2014-11-02 21:11:50 -070069 if (it != m_nextHops.end()) {
70 m_nextHops.erase(it);
Ju Pand8315bf2019-07-31 06:59:07 +000071 return true;
Junxiao Shic1e12362014-01-24 20:03:26 -070072 }
Ju Pand8315bf2019-07-31 06:59:07 +000073 return false;
Junxiao Shic1e12362014-01-24 20:03:26 -070074}
75
76void
77Entry::sortNextHops()
78{
Junxiao Shi56a21bf2014-11-02 21:11:50 -070079 std::sort(m_nextHops.begin(), m_nextHops.end(),
80 [] (const NextHop& a, const NextHop& b) { return a.getCost() < b.getCost(); });
Junxiao Shic1e12362014-01-24 20:03:26 -070081}
82
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040083} // namespace nfd::fib