blob: 8b9366e98461962962152400282bcd9fcd3dabd3 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, The University of Memphis,
Vince Lehmancec38852015-03-31 13:21:38 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Nick Gordonc0c6bcf2017-08-15 18:11:21 -050021
akmhoquec8a10f72014-04-25 18:42:55 -050022#include "nexthop-list.hpp"
Nick Gordone98480b2017-05-24 11:23:03 -050023#include "common.hpp"
akmhoque53353462014-04-22 08:43:45 -050024#include "nexthop.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050025#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050026
27namespace nlsr {
28
dmcoomescf8d0ed2017-02-21 11:39:01 -060029INIT_LOGGER(route.NexthopList);
akmhoque674b0b12014-05-20 14:33:28 -050030
akmhoque53353462014-04-22 08:43:45 -050031static bool
Vince Lehmanef21d8e2015-04-01 15:59:39 -050032nexthopAddCompare(const NextHop& nh1, const NextHop& nh2)
akmhoque53353462014-04-22 08:43:45 -050033{
akmhoque157b0a42014-05-13 00:26:37 -050034 return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -050035}
36
37static bool
Vince Lehmanef21d8e2015-04-01 15:59:39 -050038nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2)
akmhoque53353462014-04-22 08:43:45 -050039{
akmhoque157b0a42014-05-13 00:26:37 -050040 return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
Vince Lehman145064a2014-08-23 11:44:16 -050041 nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) ;
akmhoque53353462014-04-22 08:43:45 -050042}
43
Nick Gordonb50e51b2016-07-22 16:05:57 -050044bool
45operator==(const NexthopList& lhs, const NexthopList& rhs)
46{
Nick Gordonff9a6272017-10-12 13:38:29 -050047 if (lhs.size() != rhs.size()) {
Nick Gordonb50e51b2016-07-22 16:05:57 -050048 return false;
49 }
50
51 NexthopList slhs = lhs;
52 NexthopList srhs = rhs;
53
54 for (struct {std::set<NextHop>::iterator lItr;
55 std::set<NextHop>::iterator rItr;} pair = {slhs.begin(), srhs.begin()};
56 (pair.lItr != slhs.end() || pair.rItr != srhs.end());
57 pair.rItr++, pair.lItr++) {
58 if (!((*pair.lItr) == (*pair.rItr))) {
59 return false;
60 }
61 }
62 return true;
63}
64
Nick Gordonc0c6bcf2017-08-15 18:11:21 -050065bool
66operator!=(const NexthopList& lhs, const NexthopList& rhs)
67{
68 return !(lhs == rhs);
69}
70
Nick Gordonb50e51b2016-07-22 16:05:57 -050071std::ostream&
72operator<<(std::ostream& os, const NexthopList& nhl)
73{
74 NexthopList& ucnhl = const_cast<NexthopList&>(nhl);
75 os << "NexthopList(\nNext hops: ";
76 for (auto&& nh : ucnhl.getNextHops()) {
77 os << nh;
78 }
79 os << ")";
80 return os;
81}
82
akmhoque53353462014-04-22 08:43:45 -050083void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050084NexthopList::addNextHop(const NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050085{
Vince Lehmanef21d8e2015-04-01 15:59:39 -050086 std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
akmhoque53353462014-04-22 08:43:45 -050087 m_nexthopList.end(),
dmcoomes9f936662017-03-02 10:33:09 -060088 std::bind(&nexthopAddCompare, _1, nh));
akmhoque157b0a42014-05-13 00:26:37 -050089 if (it == m_nexthopList.end()) {
Vince Lehmanef21d8e2015-04-01 15:59:39 -050090 m_nexthopList.insert(nh);
akmhoque53353462014-04-22 08:43:45 -050091 }
Vince Lehmanef21d8e2015-04-01 15:59:39 -050092 else if (it->getRouteCost() > nh.getRouteCost()) {
93 removeNextHop(*it);
94 m_nexthopList.insert(nh);
akmhoque53353462014-04-22 08:43:45 -050095 }
96}
97
akmhoque53353462014-04-22 08:43:45 -050098void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050099NexthopList::removeNextHop(const NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500100{
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500101 std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
akmhoque53353462014-04-22 08:43:45 -0500102 m_nexthopList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600103 std::bind(&nexthopRemoveCompare, _1, nh));
akmhoque157b0a42014-05-13 00:26:37 -0500104 if (it != m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500105 m_nexthopList.erase(it);
106 }
107}
108
109void
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600110NexthopList::writeLog() const
akmhoque674b0b12014-05-20 14:33:28 -0500111{
112 int i = 1;
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500113
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600114 for (const auto& nexthop : m_nexthopList) {
115 NLSR_LOG_DEBUG("Nexthop " << i++ << ": " << nexthop.getConnectingFaceUri() <<
116 " Route Cost: " << nexthop.getRouteCost());
akmhoque674b0b12014-05-20 14:33:28 -0500117 }
118}
119
Nick Gordonfad8e252016-08-11 14:21:38 -0500120} // namespace nlsr