blob: 97a89b089672233b60c877eeb4540ddad6cdf95f [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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 **/
akmhoquec8a10f72014-04-25 18:42:55 -050021#include "nexthop-list.hpp"
Nick Gordone98480b2017-05-24 11:23:03 -050022#include "common.hpp"
akmhoque53353462014-04-22 08:43:45 -050023#include "nexthop.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050024#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050025
26namespace nlsr {
27
akmhoque674b0b12014-05-20 14:33:28 -050028INIT_LOGGER("NexthopList");
29
akmhoque53353462014-04-22 08:43:45 -050030static bool
Vince Lehmanef21d8e2015-04-01 15:59:39 -050031nexthopAddCompare(const NextHop& nh1, const NextHop& nh2)
akmhoque53353462014-04-22 08:43:45 -050032{
akmhoque157b0a42014-05-13 00:26:37 -050033 return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -050034}
35
36static bool
Vince Lehmanef21d8e2015-04-01 15:59:39 -050037nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2)
akmhoque53353462014-04-22 08:43:45 -050038{
akmhoque157b0a42014-05-13 00:26:37 -050039 return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
Vince Lehman145064a2014-08-23 11:44:16 -050040 nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) ;
akmhoque53353462014-04-22 08:43:45 -050041}
42
Nick Gordonb50e51b2016-07-22 16:05:57 -050043bool
44operator==(const NexthopList& lhs, const NexthopList& rhs)
45{
46 if (lhs.getSize() != rhs.getSize()) {
47 return false;
48 }
49
50 NexthopList slhs = lhs;
51 NexthopList srhs = rhs;
52
53 for (struct {std::set<NextHop>::iterator lItr;
54 std::set<NextHop>::iterator rItr;} pair = {slhs.begin(), srhs.begin()};
55 (pair.lItr != slhs.end() || pair.rItr != srhs.end());
56 pair.rItr++, pair.lItr++) {
57 if (!((*pair.lItr) == (*pair.rItr))) {
58 return false;
59 }
60 }
61 return true;
62}
63
64std::ostream&
65operator<<(std::ostream& os, const NexthopList& nhl)
66{
67 NexthopList& ucnhl = const_cast<NexthopList&>(nhl);
68 os << "NexthopList(\nNext hops: ";
69 for (auto&& nh : ucnhl.getNextHops()) {
70 os << nh;
71 }
72 os << ")";
73 return os;
74}
75
akmhoque53353462014-04-22 08:43:45 -050076void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050077NexthopList::addNextHop(const NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050078{
Vince Lehmanef21d8e2015-04-01 15:59:39 -050079 std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
akmhoque53353462014-04-22 08:43:45 -050080 m_nexthopList.end(),
dmcoomes9f936662017-03-02 10:33:09 -060081 std::bind(&nexthopAddCompare, _1, nh));
akmhoque157b0a42014-05-13 00:26:37 -050082 if (it == m_nexthopList.end()) {
Vince Lehmanef21d8e2015-04-01 15:59:39 -050083 m_nexthopList.insert(nh);
akmhoque53353462014-04-22 08:43:45 -050084 }
Vince Lehmanef21d8e2015-04-01 15:59:39 -050085 else if (it->getRouteCost() > nh.getRouteCost()) {
86 removeNextHop(*it);
87 m_nexthopList.insert(nh);
akmhoque53353462014-04-22 08:43:45 -050088 }
89}
90
akmhoque53353462014-04-22 08:43:45 -050091void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050092NexthopList::removeNextHop(const NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050093{
Vince Lehmanef21d8e2015-04-01 15:59:39 -050094 std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
akmhoque53353462014-04-22 08:43:45 -050095 m_nexthopList.end(),
dmcoomes9f936662017-03-02 10:33:09 -060096 std::bind(&nexthopRemoveCompare, _1, nh));
akmhoque157b0a42014-05-13 00:26:37 -050097 if (it != m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050098 m_nexthopList.erase(it);
99 }
100}
101
102void
akmhoque674b0b12014-05-20 14:33:28 -0500103NexthopList::writeLog()
104{
105 int i = 1;
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500106
107 for (std::set<NextHop, NextHopComparator>::iterator it = m_nexthopList.begin();
akmhoque674b0b12014-05-20 14:33:28 -0500108 it != m_nexthopList.end() ; it++, i++) {
109 _LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri()
110 << " Route Cost: " << (*it).getRouteCost());
111 }
112}
113
Nick Gordonfad8e252016-08-11 14:21:38 -0500114} // namespace nlsr