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 | |
| 23 | void |
| 24 | Entry::addNextHop(shared_ptr<Face> face, int32_t cost) |
| 25 | { |
| 26 | NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(), |
| 27 | bind(&predicate_NextHop_eq_Face, _1, face)); |
| 28 | if (it == m_nextHops.end()) { |
| 29 | m_nextHops.push_back(fib::NextHop(face)); |
| 30 | it = m_nextHops.end() - 1; |
| 31 | } |
| 32 | // now it refers to the NextHop for face |
| 33 | |
| 34 | it->setCost(cost); |
| 35 | |
| 36 | this->sortNextHops(); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | Entry::removeNextHop(shared_ptr<Face> face) |
| 42 | { |
| 43 | NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(), |
| 44 | bind(&predicate_NextHop_eq_Face, _1, face)); |
| 45 | if (it == m_nextHops.end()) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | m_nextHops.erase(it); |
| 50 | } |
| 51 | |
| 52 | static inline bool |
| 53 | compare_NextHop_cost(const NextHop& a, const NextHop& b) |
| 54 | { |
| 55 | return a.getCost() < b.getCost(); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | Entry::sortNextHops() |
| 60 | { |
| 61 | std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost); |
| 62 | } |
| 63 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 64 | void |
| 65 | Entry::setStrategy(shared_ptr<fw::Strategy> strategy) |
| 66 | { |
| 67 | m_strategy = strategy; |
| 68 | } |
| 69 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 70 | |
| 71 | } // namespace fib |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 72 | } // namespace nfd |