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" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 8 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 9 | namespace nfd { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 10 | namespace fib { |
| 11 | |
| 12 | Entry::Entry(const Name& prefix) |
| 13 | : m_prefix(prefix) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | static inline bool |
| 18 | predicate_NextHop_eq_Face(const NextHop& nexthop, shared_ptr<Face> face) |
| 19 | { |
| 20 | return nexthop.getFace() == face; |
| 21 | } |
| 22 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 23 | bool |
| 24 | Entry::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 Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 31 | void |
| 32 | Entry::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 Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 42 | it->setCost(cost); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 43 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 44 | this->sortNextHops(); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
| 48 | Entry::removeNextHop(shared_ptr<Face> face) |
| 49 | { |
| 50 | NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(), |
| 51 | bind(&predicate_NextHop_eq_Face, _1, face)); |
| 52 | if (it == m_nextHops.end()) { |
| 53 | return; |
| 54 | } |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 56 | m_nextHops.erase(it); |
| 57 | } |
| 58 | |
| 59 | static inline bool |
| 60 | compare_NextHop_cost(const NextHop& a, const NextHop& b) |
| 61 | { |
| 62 | return a.getCost() < b.getCost(); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | Entry::sortNextHops() |
| 67 | { |
| 68 | std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | } // namespace fib |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 73 | } // namespace nfd |