blob: 6a9b6863ecd141a78e8d830a01c559cdd1b969a3 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "fib-entry.hpp"
Junxiao Shic1e12362014-01-24 20:03:26 -07008
Alexander Afanasyev18bbf812014-01-29 01:40:23 -08009namespace nfd {
Junxiao Shic1e12362014-01-24 20:03:26 -070010namespace fib {
11
12Entry::Entry(const Name& prefix)
13 : m_prefix(prefix)
14{
15}
16
17static inline bool
18predicate_NextHop_eq_Face(const NextHop& nexthop, shared_ptr<Face> face)
19{
20 return nexthop.getFace() == face;
21}
22
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070023bool
24Entry::hasNextHop(shared_ptr<Face> face) const
25{
26 NextHopList::const_iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
27 bind(&predicate_NextHop_eq_Face, _1, face));
28 return it != m_nextHops.end();
29}
30
Junxiao Shic1e12362014-01-24 20:03:26 -070031void
32Entry::addNextHop(shared_ptr<Face> face, int32_t cost)
33{
34 NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
35 bind(&predicate_NextHop_eq_Face, _1, face));
36 if (it == m_nextHops.end()) {
37 m_nextHops.push_back(fib::NextHop(face));
38 it = m_nextHops.end() - 1;
39 }
40 // now it refers to the NextHop for face
Junxiao Shi7bb01512014-03-05 21:34:09 -070041
Junxiao Shic1e12362014-01-24 20:03:26 -070042 it->setCost(cost);
Junxiao Shi7bb01512014-03-05 21:34:09 -070043
Junxiao Shic1e12362014-01-24 20:03:26 -070044 this->sortNextHops();
45
46}
47
48void
49Entry::removeNextHop(shared_ptr<Face> face)
50{
51 NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
52 bind(&predicate_NextHop_eq_Face, _1, face));
53 if (it == m_nextHops.end()) {
54 return;
55 }
Junxiao Shi7bb01512014-03-05 21:34:09 -070056
Junxiao Shic1e12362014-01-24 20:03:26 -070057 m_nextHops.erase(it);
58}
59
60static inline bool
61compare_NextHop_cost(const NextHop& a, const NextHop& b)
62{
63 return a.getCost() < b.getCost();
64}
65
66void
67Entry::sortNextHops()
68{
69 std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost);
70}
71
72
73} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080074} // namespace nfd