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 | |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 22 | #include "common.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 23 | #include "nexthop-list.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | #include "nexthop.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 25 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 26 | |
| 27 | namespace nlsr { |
| 28 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 29 | INIT_LOGGER("NexthopList"); |
| 30 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | using namespace std; |
| 32 | |
| 33 | static bool |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 34 | nexthopAddCompare(const NextHop& nh1, const NextHop& nh2) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 36 | return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static bool |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 40 | nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 42 | return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() && |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 43 | nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) ; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 44 | } |
| 45 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | void |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 47 | NexthopList::addNextHop(const NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | { |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 49 | std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(), |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | m_nexthopList.end(), |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 51 | ndn::bind(&nexthopAddCompare, _1, nh)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 52 | if (it == m_nexthopList.end()) { |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 53 | m_nexthopList.insert(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | } |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 55 | else if (it->getRouteCost() > nh.getRouteCost()) { |
| 56 | removeNextHop(*it); |
| 57 | m_nexthopList.insert(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 61 | void |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 62 | NexthopList::removeNextHop(const NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 63 | { |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 64 | std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(), |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 65 | m_nexthopList.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 66 | ndn::bind(&nexthopRemoveCompare, _1, nh)); |
| 67 | if (it != m_nexthopList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | m_nexthopList.erase(it); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 73 | NexthopList::writeLog() |
| 74 | { |
| 75 | int i = 1; |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame^] | 76 | |
| 77 | for (std::set<NextHop, NextHopComparator>::iterator it = m_nexthopList.begin(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 78 | it != m_nexthopList.end() ; it++, i++) { |
| 79 | _LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri() |
| 80 | << " Route Cost: " << (*it).getRouteCost()); |
| 81 | } |
| 82 | } |
| 83 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 84 | } // namespace nlsr |