adjacent.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
21 #include "adjacent.hpp"
22 #include "logger.hpp"
23 
24 #include <iostream>
25 #include <string>
26 #include <cmath>
27 #include <limits>
28 
29 namespace nlsr {
30 
31 INIT_LOGGER(Adjacent);
32 
33 const double Adjacent::DEFAULT_LINK_COST = 10.0;
34 const double Adjacent::NON_ADJACENT_COST = -12345;
35 
37  : m_name()
38  , m_faceUri()
39  , m_linkCost(DEFAULT_LINK_COST)
40  , m_status(STATUS_INACTIVE)
41  , m_interestTimedOutNo(0)
42  , m_faceId(0)
43 {
44 }
45 
46 Adjacent::Adjacent(const ndn::Name& an)
47  : m_name(an)
48  , m_faceUri()
49  , m_linkCost(DEFAULT_LINK_COST)
50  , m_status(STATUS_INACTIVE)
51  , m_interestTimedOutNo(0)
52  , m_faceId(0)
53  {
54  }
55 
56 Adjacent::Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc,
57  Status s, uint32_t iton, uint64_t faceId)
58  : m_name(an)
59  , m_faceUri(faceUri)
60  , m_status(s)
61  , m_interestTimedOutNo(iton)
62  , m_faceId(faceId)
63  {
64  this->setLinkCost(lc);
65  }
66 
67 void
69 {
70  // NON_ADJACENT_COST is a negative value and is used for nodes that aren't direct neighbors.
71  // But for direct/active neighbors, the cost cannot be negative.
72  if (lc < 0 && lc != NON_ADJACENT_COST)
73  {
74  NLSR_LOG_ERROR(" Neighbor's link-cost cannot be negative");
75  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Neighbor's link-cost cannot be negative"));
76  }
77 
78  m_linkCost = lc;
79 }
80 
81 bool
82 Adjacent::operator==(const Adjacent& adjacent) const
83 {
84  return (m_name == adjacent.getName()) &&
85  (m_faceUri == adjacent.getFaceUri()) &&
86  (std::abs(m_linkCost - adjacent.getLinkCost()) <
87  std::numeric_limits<double>::epsilon());
88 }
89 
90 bool
91 Adjacent::operator<(const Adjacent& adjacent) const
92 {
93  auto linkCost = adjacent.getLinkCost();
94  return std::tie(m_name, m_linkCost) <
95  std::tie(adjacent.getName(), linkCost);
96 }
97 
98 std::ostream&
99 operator<<(std::ostream& os, const Adjacent& adjacent)
100 {
101  os << "Adjacent: " << adjacent.m_name << "\n Connecting FaceUri: " << adjacent.m_faceUri
102  << "\n Link cost: " << adjacent.m_linkCost << "\n Status: " << adjacent.m_status
103  << "\n Interest Timed Out: " << adjacent.m_interestTimedOutNo << std::endl;
104  return os;
105 }
106 
107 void
109 {
110  NLSR_LOG_DEBUG(*this);
111 }
112 
113 } // namespace nlsr
bool operator==(const Adjacent &adjacent) const
Equality is when name, Face URI, and link cost are all equal.
Definition: adjacent.cpp:82
const ndn::FaceUri & getFaceUri() const
Definition: adjacent.hpp:69
friend std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:99
static const double DEFAULT_LINK_COST
Definition: adjacent.hpp:160
double getLinkCost() const
Definition: adjacent.hpp:81
static const double NON_ADJACENT_COST
Definition: adjacent.hpp:161
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
const ndn::Name & getName() const
Definition: adjacent.hpp:57
bool operator<(const Adjacent &adjacent) const
Definition: adjacent.cpp:91
A neighbor reachable over a Face.
Definition: adjacent.hpp:38
void writeLog()
Definition: adjacent.cpp:108
#define NLSR_LOG_ERROR(x)
Definition: logger.hpp:41
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void setLinkCost(double lc)
Definition: adjacent.cpp:68