Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 24 | |
| 25 | #include "fib-entry.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 27 | namespace nfd { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 28 | namespace fib { |
| 29 | |
| 30 | Entry::Entry(const Name& prefix) |
| 31 | : m_prefix(prefix) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | static inline bool |
| 36 | predicate_NextHop_eq_Face(const NextHop& nexthop, shared_ptr<Face> face) |
| 37 | { |
| 38 | return nexthop.getFace() == face; |
| 39 | } |
| 40 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 41 | bool |
| 42 | Entry::hasNextHop(shared_ptr<Face> face) const |
| 43 | { |
| 44 | NextHopList::const_iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(), |
| 45 | bind(&predicate_NextHop_eq_Face, _1, face)); |
| 46 | return it != m_nextHops.end(); |
| 47 | } |
| 48 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 49 | void |
| 50 | Entry::addNextHop(shared_ptr<Face> face, int32_t cost) |
| 51 | { |
| 52 | NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(), |
| 53 | bind(&predicate_NextHop_eq_Face, _1, face)); |
| 54 | if (it == m_nextHops.end()) { |
| 55 | m_nextHops.push_back(fib::NextHop(face)); |
| 56 | it = m_nextHops.end() - 1; |
| 57 | } |
| 58 | // now it refers to the NextHop for face |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 60 | it->setCost(cost); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 62 | this->sortNextHops(); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
| 66 | Entry::removeNextHop(shared_ptr<Face> face) |
| 67 | { |
| 68 | NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(), |
| 69 | bind(&predicate_NextHop_eq_Face, _1, face)); |
| 70 | if (it == m_nextHops.end()) { |
| 71 | return; |
| 72 | } |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 73 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 74 | m_nextHops.erase(it); |
| 75 | } |
| 76 | |
| 77 | static inline bool |
| 78 | compare_NextHop_cost(const NextHop& a, const NextHop& b) |
| 79 | { |
| 80 | return a.getCost() < b.getCost(); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | Entry::sortNextHops() |
| 85 | { |
| 86 | std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | } // namespace fib |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 91 | } // namespace nfd |