Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 10 | namespace ndn { |
| 11 | namespace fib { |
| 12 | |
| 13 | Entry::Entry(const Name& prefix) |
| 14 | : m_prefix(prefix) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | static inline bool |
| 19 | predicate_NextHop_eq_Face(const NextHop& nexthop, shared_ptr<Face> face) |
| 20 | { |
| 21 | return nexthop.getFace() == face; |
| 22 | } |
| 23 | |
| 24 | void |
| 25 | Entry::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 | |
| 41 | void |
| 42 | Entry::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 | |
| 53 | static inline bool |
| 54 | compare_NextHop_cost(const NextHop& a, const NextHop& b) |
| 55 | { |
| 56 | return a.getCost() < b.getCost(); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | Entry::sortNextHops() |
| 61 | { |
| 62 | std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | } // namespace fib |
| 67 | } // namespace ndn |