fib-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, 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 "fib-entry.hpp"
27 
28 namespace nfd::fib {
29 
30 Entry::Entry(const Name& prefix)
31  : m_prefix(prefix)
32 {
33 }
34 
35 NextHopList::iterator
36 Entry::findNextHop(const Face& face) noexcept
37 {
38  return std::find_if(m_nextHops.begin(), m_nextHops.end(),
39  [&face] (const NextHop& nexthop) { return &nexthop.getFace() == &face; });
40 }
41 
42 bool
43 Entry::hasNextHop(const Face& face) const noexcept
44 {
45  return const_cast<Entry*>(this)->findNextHop(face) != m_nextHops.end();
46 }
47 
48 std::pair<NextHopList::iterator, bool>
49 Entry::addOrUpdateNextHop(Face& face, uint64_t cost)
50 {
51  auto it = findNextHop(face);
52  bool isNew = false;
53  if (it == m_nextHops.end()) {
54  m_nextHops.emplace_back(face);
55  it = std::prev(m_nextHops.end());
56  isNew = true;
57  }
58 
59  it->setCost(cost);
60  this->sortNextHops();
61 
62  return {it, isNew};
63 }
64 
65 bool
66 Entry::removeNextHop(const Face& face)
67 {
68  auto it = findNextHop(face);
69  if (it != m_nextHops.end()) {
70  m_nextHops.erase(it);
71  return true;
72  }
73  return false;
74 }
75 
76 void
77 Entry::sortNextHops()
78 {
79  std::sort(m_nextHops.begin(), m_nextHops.end(),
80  [] (const NextHop& a, const NextHop& b) { return a.getCost() < b.getCost(); });
81 }
82 
83 } // namespace nfd::fib
Generalization of a network interface.
Definition: face.hpp:118
Represents an entry in the FIB.
Definition: fib-entry.hpp:91
bool hasNextHop(const Face &face) const noexcept
Returns whether there is a NextHop record for face.
Definition: fib-entry.cpp:43
Entry(const Name &prefix)
Definition: fib-entry.cpp:30
Represents a nexthop record in a FIB entry.
Definition: fib-entry.hpp:50