akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Muktadir R Chowdhury | 800833b | 2016-07-29 13:43:59 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, The University of Memphis, |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 21 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | #include <iostream> |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 23 | |
| 24 | #include "common.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 25 | #include "nexthop-list.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 26 | #include "nexthop.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 27 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 28 | |
| 29 | namespace nlsr { |
| 30 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 31 | INIT_LOGGER("NexthopList"); |
| 32 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | using namespace std; |
| 34 | |
| 35 | static bool |
| 36 | nexthopCompare(NextHop& nh1, NextHop& nh2) |
| 37 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 38 | return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static bool |
| 42 | nexthopRemoveCompare(NextHop& nh1, NextHop& nh2) |
| 43 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 44 | return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() && |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 45 | nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) ; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static bool |
| 49 | nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2) |
| 50 | { |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 51 | if (nh1.getRouteCostAsAdjustedInteger() < nh2.getRouteCostAsAdjustedInteger()) { |
| 52 | return true; |
| 53 | } |
| 54 | else if (nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) { |
| 55 | return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri(); |
| 56 | } |
| 57 | else { |
| 58 | return false; |
| 59 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | Add next hop to the Next Hop list |
| 64 | If next hop is new it is added |
| 65 | If next hop already exists in next |
| 66 | hop list then updates the route |
| 67 | cost with new next hop's route cost |
| 68 | */ |
| 69 | |
| 70 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 71 | NexthopList::addNextHop(NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 72 | { |
| 73 | std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(), |
| 74 | m_nexthopList.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 75 | ndn::bind(&nexthopCompare, _1, nh)); |
| 76 | if (it == m_nexthopList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | m_nexthopList.push_back(nh); |
Alexander Afanasyev | 1717b24 | 2014-07-15 13:53:17 -0700 | [diff] [blame] | 78 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 80 | if ((*it).getRouteCost() > nh.getRouteCost()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | (*it).setRouteCost(nh.getRouteCost()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | Remove a next hop only if both next hop face and route cost are same |
| 87 | |
| 88 | */ |
| 89 | |
| 90 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 91 | NexthopList::removeNextHop(NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 92 | { |
| 93 | std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(), |
| 94 | m_nexthopList.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 95 | ndn::bind(&nexthopRemoveCompare, _1, nh)); |
| 96 | if (it != m_nexthopList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | m_nexthopList.erase(it); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 102 | NexthopList::sort() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 103 | { |
| 104 | m_nexthopList.sort(nextHopSortingComparator); |
| 105 | } |
| 106 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 107 | void |
| 108 | NexthopList::writeLog() |
| 109 | { |
| 110 | int i = 1; |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 111 | sort(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 112 | for (std::list<NextHop>::iterator it = m_nexthopList.begin(); |
| 113 | it != m_nexthopList.end() ; it++, i++) { |
| 114 | _LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri() |
| 115 | << " Route Cost: " << (*it).getRouteCost()); |
| 116 | } |
| 117 | } |
| 118 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame^] | 119 | } // namespace nlsr |