nexthop-list.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "nexthop-list.hpp"
23 #include "common.hpp"
24 #include "nexthop.hpp"
25 #include "logger.hpp"
26 
27 namespace nlsr {
28 
29 INIT_LOGGER("NexthopList");
30 
31 static bool
32 nexthopAddCompare(const NextHop& nh1, const NextHop& nh2)
33 {
34  return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
35 }
36 
37 static bool
38 nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2)
39 {
40  return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
42 }
43 
44 bool
45 operator==(const NexthopList& lhs, const NexthopList& rhs)
46 {
47  if (lhs.size() != rhs.size()) {
48  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 
65 bool
66 operator!=(const NexthopList& lhs, const NexthopList& rhs)
67 {
68  return !(lhs == rhs);
69 }
70 
71 std::ostream&
72 operator<<(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 
83 void
85 {
86  std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
87  m_nexthopList.end(),
88  std::bind(&nexthopAddCompare, _1, nh));
89  if (it == m_nexthopList.end()) {
90  m_nexthopList.insert(nh);
91  }
92  else if (it->getRouteCost() > nh.getRouteCost()) {
93  removeNextHop(*it);
94  m_nexthopList.insert(nh);
95  }
96 }
97 
98 void
100 {
101  std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
102  m_nexthopList.end(),
103  std::bind(&nexthopRemoveCompare, _1, nh));
104  if (it != m_nexthopList.end()) {
105  m_nexthopList.erase(it);
106  }
107 }
108 
109 void
111 {
112  int i = 1;
113 
114  for (std::set<NextHop, NextHopComparator>::iterator it = m_nexthopList.begin();
115  it != m_nexthopList.end() ; it++, i++) {
116  NLSR_LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri()
117  << " Route Cost: " << (*it).getRouteCost());
118  }
119 }
120 
121 } // namespace nlsr
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:83
bool operator==(const NamePrefixTableEntry &lhs, const NamePrefixTableEntry &rhs)
size_t size() const
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:41
bool operator!=(const NexthopList &lhs, const NexthopList &rhs)
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
const std::string & getConnectingFaceUri() const
Definition: nexthop.hpp:48
static bool nexthopRemoveCompare(const NextHop &nh1, const NextHop &nh2)
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
static bool nexthopAddCompare(const NextHop &nh1, const NextHop &nh2)
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
uint64_t getRouteCostAsAdjustedInteger() const
Definition: nexthop.hpp:60
std::set< NextHop, NextHopComparator > & getNextHops()
double getRouteCost() const
Definition: nexthop.hpp:74
void removeNextHop(const NextHop &nh)
Remove a next hop from the Next Hop list.