blob: b2ea76c0f5b3118d581a698f35cb315256cbcd52 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi56a21bf2014-11-02 21:11:50 -07003 * Copyright (c) 2014, 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
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
37Entry::findNextHop(Face& face)
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(),
40 [&face] (const NextHop& nexthop) {
41 return nexthop.getFace().get() == &face;
42 });
Junxiao Shic1e12362014-01-24 20:03:26 -070043}
44
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070045bool
46Entry::hasNextHop(shared_ptr<Face> face) const
47{
Junxiao Shi56a21bf2014-11-02 21:11:50 -070048 return const_cast<Entry*>(this)->findNextHop(*face) != m_nextHops.end();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070049}
50
Junxiao Shic1e12362014-01-24 20:03:26 -070051void
Alexander Afanasyev11b9f3f2014-04-10 00:01:15 -070052Entry::addNextHop(shared_ptr<Face> face, uint64_t cost)
Junxiao Shic1e12362014-01-24 20:03:26 -070053{
Junxiao Shi56a21bf2014-11-02 21:11:50 -070054 auto it = this->findNextHop(*face);
Junxiao Shic1e12362014-01-24 20:03:26 -070055 if (it == m_nextHops.end()) {
56 m_nextHops.push_back(fib::NextHop(face));
Junxiao Shi56a21bf2014-11-02 21:11:50 -070057 it = m_nextHops.end();
58 --it;
Junxiao Shic1e12362014-01-24 20:03:26 -070059 }
60 // now it refers to the NextHop for face
Junxiao Shi7bb01512014-03-05 21:34:09 -070061
Junxiao Shic1e12362014-01-24 20:03:26 -070062 it->setCost(cost);
Junxiao Shi7bb01512014-03-05 21:34:09 -070063
Junxiao Shic1e12362014-01-24 20:03:26 -070064 this->sortNextHops();
Junxiao Shic1e12362014-01-24 20:03:26 -070065}
66
67void
68Entry::removeNextHop(shared_ptr<Face> face)
69{
Junxiao Shi56a21bf2014-11-02 21:11:50 -070070 auto it = this->findNextHop(*face);
71 if (it != m_nextHops.end()) {
72 m_nextHops.erase(it);
Junxiao Shic1e12362014-01-24 20:03:26 -070073 }
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
83
84} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080085} // namespace nfd