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-2022, 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)
37 {
38  return std::find_if(m_nextHops.begin(), m_nextHops.end(),
39  [&face] (const NextHop& nexthop) {
40  return &nexthop.getFace() == &face;
41  });
42 }
43 
44 bool
45 Entry::hasNextHop(const Face& face) const
46 {
47  return const_cast<Entry*>(this)->findNextHop(face) != m_nextHops.end();
48 }
49 
50 std::pair<NextHopList::iterator, bool>
51 Entry::addOrUpdateNextHop(Face& face, uint64_t cost)
52 {
53  auto it = this->findNextHop(face);
54  bool isNew = false;
55  if (it == m_nextHops.end()) {
56  m_nextHops.emplace_back(face);
57  it = std::prev(m_nextHops.end());
58  isNew = true;
59  }
60 
61  it->setCost(cost);
62  this->sortNextHops();
63 
64  return {it, isNew};
65 }
66 
67 bool
68 Entry::removeNextHop(const Face& face)
69 {
70  auto it = this->findNextHop(face);
71  if (it != m_nextHops.end()) {
72  m_nextHops.erase(it);
73  return true;
74  }
75  return false;
76 }
77 
78 void
79 Entry::sortNextHops()
80 {
81  std::sort(m_nextHops.begin(), m_nextHops.end(),
82  [] (const NextHop& a, const NextHop& b) { return a.getCost() < b.getCost(); });
83 }
84 
85 } // namespace nfd::fib
Generalization of a network interface.
Definition: face.hpp:56
Represents an entry in the FIB.
Definition: fib-entry.hpp:54
bool hasNextHop(const Face &face) const
Definition: fib-entry.cpp:45
Entry(const Name &prefix)
Definition: fib-entry.cpp:30
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:37