blob: 79111e27da6e0f230e1827af8585742ea3010d84 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shic1e12362014-01-24 20:03:26 -070024
25#include "fib-entry.hpp"
Junxiao Shic1e12362014-01-24 20:03:26 -070026
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080027namespace nfd {
Junxiao Shic1e12362014-01-24 20:03:26 -070028namespace fib {
29
30Entry::Entry(const Name& prefix)
31 : m_prefix(prefix)
32{
33}
34
35static inline bool
36predicate_NextHop_eq_Face(const NextHop& nexthop, shared_ptr<Face> face)
37{
38 return nexthop.getFace() == face;
39}
40
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070041bool
42Entry::hasNextHop(shared_ptr<Face> face) const
43{
44 NextHopList::const_iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
45 bind(&predicate_NextHop_eq_Face, _1, face));
46 return it != m_nextHops.end();
47}
48
Junxiao Shic1e12362014-01-24 20:03:26 -070049void
Alexander Afanasyev11b9f3f2014-04-10 00:01:15 -070050Entry::addNextHop(shared_ptr<Face> face, uint64_t cost)
Junxiao Shic1e12362014-01-24 20:03:26 -070051{
52 NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
53 bind(&predicate_NextHop_eq_Face, _1, face));
54 if (it == m_nextHops.end()) {
55 m_nextHops.push_back(fib::NextHop(face));
56 it = m_nextHops.end() - 1;
57 }
58 // now it refers to the NextHop for face
Junxiao Shi7bb01512014-03-05 21:34:09 -070059
Junxiao Shic1e12362014-01-24 20:03:26 -070060 it->setCost(cost);
Junxiao Shi7bb01512014-03-05 21:34:09 -070061
Junxiao Shic1e12362014-01-24 20:03:26 -070062 this->sortNextHops();
Junxiao Shic1e12362014-01-24 20:03:26 -070063}
64
65void
66Entry::removeNextHop(shared_ptr<Face> face)
67{
68 NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
69 bind(&predicate_NextHop_eq_Face, _1, face));
70 if (it == m_nextHops.end()) {
71 return;
72 }
Junxiao Shi7bb01512014-03-05 21:34:09 -070073
Junxiao Shic1e12362014-01-24 20:03:26 -070074 m_nextHops.erase(it);
75}
76
77static inline bool
78compare_NextHop_cost(const NextHop& a, const NextHop& b)
79{
80 return a.getCost() < b.getCost();
81}
82
83void
84Entry::sortNextHops()
85{
86 std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost);
87}
88
89
90} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080091} // namespace nfd