blob: bb49979dbf44d7ea378fa44da35a383652b37b49 [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)
33{
34}
35
Junxiao Shi56a21bf2014-11-02 21:11:50 -070036NextHopList::iterator
ashiqopu77d0bfd2019-02-20 20:37:31 +000037Entry::findNextHop(const Face& face, EndpointId endpointId)
Junxiao Shic1e12362014-01-24 20:03:26 -070038{
Junxiao Shi56a21bf2014-11-02 21:11:50 -070039 return std::find_if(m_nextHops.begin(), m_nextHops.end(),
ashiqopu3ad49db2018-10-20 22:38:47 +000040 [&face, endpointId] (const NextHop& nexthop) {
41 return &nexthop.getFace() == &face && nexthop.getEndpointId() == endpointId;
Junxiao Shi56a21bf2014-11-02 21:11:50 -070042 });
Junxiao Shic1e12362014-01-24 20:03:26 -070043}
44
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070045bool
ashiqopu77d0bfd2019-02-20 20:37:31 +000046Entry::hasNextHop(const Face& face, EndpointId endpointId) const
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070047{
ashiqopu3ad49db2018-10-20 22:38:47 +000048 return const_cast<Entry*>(this)->findNextHop(face, endpointId) != m_nextHops.end();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070049}
50
Junxiao Shic1e12362014-01-24 20:03:26 -070051void
ashiqopu77d0bfd2019-02-20 20:37:31 +000052Entry::addOrUpdateNextHop(Face& face, EndpointId endpointId, uint64_t cost)
Junxiao Shic1e12362014-01-24 20:03:26 -070053{
ashiqopu3ad49db2018-10-20 22:38:47 +000054 auto it = this->findNextHop(face, endpointId);
Junxiao Shic1e12362014-01-24 20:03:26 -070055 if (it == m_nextHops.end()) {
ashiqopu3ad49db2018-10-20 22:38:47 +000056 m_nextHops.emplace_back(face, endpointId);
Junxiao Shia6de4292016-07-12 02:08:10 +000057 it = std::prev(m_nextHops.end());
Junxiao Shic1e12362014-01-24 20:03:26 -070058 }
Junxiao Shic1e12362014-01-24 20:03:26 -070059 it->setCost(cost);
Junxiao Shic1e12362014-01-24 20:03:26 -070060 this->sortNextHops();
Junxiao Shic1e12362014-01-24 20:03:26 -070061}
62
63void
ashiqopu77d0bfd2019-02-20 20:37:31 +000064Entry::removeNextHop(const Face& face, EndpointId endpointId)
Junxiao Shic1e12362014-01-24 20:03:26 -070065{
ashiqopu3ad49db2018-10-20 22:38:47 +000066 auto it = this->findNextHop(face, endpointId);
Junxiao Shi56a21bf2014-11-02 21:11:50 -070067 if (it != m_nextHops.end()) {
68 m_nextHops.erase(it);
Junxiao Shic1e12362014-01-24 20:03:26 -070069 }
Junxiao Shic1e12362014-01-24 20:03:26 -070070}
71
72void
ashiqopu3ad49db2018-10-20 22:38:47 +000073Entry::removeNextHopByFace(const Face& face)
74{
75 auto it = std::remove_if(m_nextHops.begin(), m_nextHops.end(),
76 [&face] (const NextHop& nexthop) {
77 return &nexthop.getFace() == &face;
78 });
79 m_nextHops.erase(it, m_nextHops.end());
80}
81
82void
Junxiao Shic1e12362014-01-24 20:03:26 -070083Entry::sortNextHops()
84{
Junxiao Shi56a21bf2014-11-02 21:11:50 -070085 std::sort(m_nextHops.begin(), m_nextHops.end(),
86 [] (const NextHop& a, const NextHop& b) { return a.getCost() < b.getCost(); });
Junxiao Shic1e12362014-01-24 20:03:26 -070087}
88
Junxiao Shic1e12362014-01-24 20:03:26 -070089} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080090} // namespace nfd