blob: a00a5828d9d2dbdff1b7ec40858882d7354530fc [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"
8#include <algorithm>
9
10namespace ndn {
11namespace fib {
12
13Entry::Entry(const Name& prefix)
14 : m_prefix(prefix)
15{
16}
17
18static inline bool
19predicate_NextHop_eq_Face(const NextHop& nexthop, shared_ptr<Face> face)
20{
21 return nexthop.getFace() == face;
22}
23
24void
25Entry::addNextHop(shared_ptr<Face> face, int32_t cost)
26{
27 NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
28 bind(&predicate_NextHop_eq_Face, _1, face));
29 if (it == m_nextHops.end()) {
30 m_nextHops.push_back(fib::NextHop(face));
31 it = m_nextHops.end() - 1;
32 }
33 // now it refers to the NextHop for face
34
35 it->setCost(cost);
36
37 this->sortNextHops();
38
39}
40
41void
42Entry::removeNextHop(shared_ptr<Face> face)
43{
44 NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
45 bind(&predicate_NextHop_eq_Face, _1, face));
46 if (it == m_nextHops.end()) {
47 return;
48 }
49
50 m_nextHops.erase(it);
51}
52
53static inline bool
54compare_NextHop_cost(const NextHop& a, const NextHop& b)
55{
56 return a.getCost() < b.getCost();
57}
58
59void
60Entry::sortNextHops()
61{
62 std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost);
63}
64
65
66} // namespace fib
67} // namespace ndn