nexthop-list.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2020, The University of Memphis,
4  * Regents of the University of California,
5  * Arizona Board of Regents.
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/>.
20  */
21 
22 #include "nexthop-list.hpp"
23 #include "common.hpp"
24 #include "nexthop.hpp"
25 
26 #include <ndn-cxx/util/ostream-joiner.hpp>
27 
28 namespace nlsr {
29 
30 static bool
31 nexthopAddCompare(const NextHop& nh1, const NextHop& nh2)
32 {
33  return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
34 }
35 
36 static bool
37 nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2)
38 {
39  return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
41 }
42 
43 bool
44 operator==(const NexthopList& lhs, const NexthopList& rhs)
45 {
46  if (lhs.size() != rhs.size()) {
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 
64 bool
65 operator!=(const NexthopList& lhs, const NexthopList& rhs)
66 {
67  return !(lhs == rhs);
68 }
69 
70 std::ostream&
71 operator<<(std::ostream& os, const NexthopList& nhl)
72 {
73  os << " ";
74  std::copy(nhl.cbegin(), nhl.cend(), ndn::make_ostream_joiner(os, "\n "));
75  return os;
76 }
77 
78 void
80 {
81  auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
82  std::bind(&nexthopAddCompare, _1, nh));
83  if (it == m_nexthopList.end()) {
84  m_nexthopList.insert(nh);
85  }
86  else if (it->getRouteCost() > nh.getRouteCost()) {
87  removeNextHop(*it);
88  m_nexthopList.insert(nh);
89  }
90 }
91 
92 void
94 {
95  auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
96  std::bind(&nexthopRemoveCompare, _1, nh));
97  if (it != m_nexthopList.end()) {
98  m_nexthopList.erase(it);
99  }
100 }
101 
102 } // namespace nlsr
Data abstraction for Nexthop.
Definition: nexthop.hpp:44
const std::string & getConnectingFaceUri() const
Definition: nexthop.hpp:68
double getRouteCost() const
Definition: nexthop.hpp:94
uint64_t getRouteCostAsAdjustedInteger() const
Definition: nexthop.hpp:80
size_t size() const
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
const_iterator cend() const
const_iterator cbegin() const
void removeNextHop(const NextHop &nh)
Remove a next hop from the Next Hop list.
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California,...
static bool nexthopAddCompare(const NextHop &nh1, const NextHop &nh2)
static bool nexthopRemoveCompare(const NextHop &nh1, const NextHop &nh2)
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:179
bool operator==(const NamePrefixTableEntry &lhs, const NamePrefixTableEntry &rhs)
bool operator!=(const NexthopList &lhs, const NexthopList &rhs)