adjacent.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, The University of Memphis,
4  * Regents of the University of California
5  *
6  * This file is part of NLSR (Named-data Link State Routing).
7  * See AUTHORS.md for complete list of NLSR authors and contributors.
8  *
9  * NLSR is free software: you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License as published by the Free Software Foundation,
11  * either version 3 of the License, or (at your option) any later version.
12  *
13  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "adjacent.hpp"
22 #include "logger.hpp"
23 #include "tlv-nlsr.hpp"
24 #include "utility/numeric.hpp"
25 
26 namespace nlsr {
27 
28 INIT_LOGGER(Adjacent);
29 
31  : m_name()
32  , m_faceUri()
33  , m_linkCost(DEFAULT_LINK_COST)
34  , m_status(STATUS_INACTIVE)
35  , m_interestTimedOutNo(0)
36  , m_faceId(0)
37 {
38 }
39 
40 Adjacent::Adjacent(const ndn::Block& block)
41 {
42  wireDecode(block);
43 }
44 
45 Adjacent::Adjacent(const ndn::Name& an)
46  : m_name(an)
47  , m_faceUri()
48  , m_linkCost(DEFAULT_LINK_COST)
49  , m_status(STATUS_INACTIVE)
50  , m_interestTimedOutNo(0)
51  , m_faceId(0)
52 {
53 }
54 
55 Adjacent::Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc,
56  Status s, uint32_t iton, uint64_t faceId)
57  : m_name(an)
58  , m_faceUri(faceUri)
59  , m_status(s)
60  , m_interestTimedOutNo(iton)
61  , m_faceId(faceId)
62 {
63  this->setLinkCost(lc);
64 }
65 
66 void
68 {
69  // NON_ADJACENT_COST is a negative value and is used for nodes that aren't direct neighbors.
70  // But for direct/active neighbors, the cost cannot be negative.
71  if (lc < 0 && lc != NON_ADJACENT_COST) {
72  NLSR_LOG_ERROR(" Neighbor's link-cost cannot be negative");
73  NDN_THROW(ndn::tlv::Error("Neighbor's link-cost cannot be negative"));
74  }
75 
76  m_linkCost = lc;
77 }
78 
80 
81 template<ndn::encoding::Tag TAG>
82 size_t
83 Adjacent::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
84 {
85  size_t totalLength = 0;
86 
87  totalLength += prependDoubleBlock(encoder, nlsr::tlv::Cost, m_linkCost);
88 
89  totalLength += prependStringBlock(encoder, nlsr::tlv::Uri, m_faceUri.toString());
90 
91  totalLength += m_name.wireEncode(encoder);
92 
93  totalLength += encoder.prependVarNumber(totalLength);
94  totalLength += encoder.prependVarNumber(nlsr::tlv::Adjacency);
95 
96  return totalLength;
97 }
98 
99 const ndn::Block&
101 {
102  if (m_wire.hasWire()) {
103  return m_wire;
104  }
105 
106  ndn::EncodingEstimator estimator;
107  size_t estimatedSize = wireEncode(estimator);
108 
109  ndn::EncodingBuffer buffer(estimatedSize, 0);
110  wireEncode(buffer);
111 
112  m_wire = buffer.block();
113 
114  return m_wire;
115 }
116 
117 void
118 Adjacent::wireDecode(const ndn::Block& wire)
119 {
120  m_name.clear();
121  m_faceUri = ndn::FaceUri();
122  m_linkCost = 0;
123 
124  m_wire = wire;
125 
126  if (m_wire.type() != nlsr::tlv::Adjacency) {
127  NDN_THROW(Error("Adjacency", m_wire.type()));
128  }
129 
130  m_wire.parse();
131 
132  auto val = m_wire.elements_begin();
133 
134  if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) {
135  m_name.wireDecode(*val);
136  ++val;
137  }
138  else {
139  NDN_THROW(Error("Missing required Name field"));
140  }
141 
142  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Uri) {
143  m_faceUri = ndn::FaceUri(readString(*val));
144  ++val;
145  }
146  else {
147  NDN_THROW(Error("Missing required Uri field"));
148  }
149 
150  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Cost) {
151  m_linkCost = ndn::encoding::readDouble(*val);
152  ++val;
153  }
154  else {
155  NDN_THROW(Error("Missing required Cost field"));
156  }
157 }
158 
159 bool
160 Adjacent::operator==(const Adjacent& adjacent) const
161 {
162  return m_name == adjacent.getName() &&
163  m_faceUri == adjacent.getFaceUri() &&
164  util::diffInEpsilon(m_linkCost, adjacent.getLinkCost());
165 }
166 
167 bool
168 Adjacent::operator<(const Adjacent& adjacent) const
169 {
170  auto linkCost = adjacent.getLinkCost();
171  return std::tie(m_name, m_linkCost) <
172  std::tie(adjacent.getName(), linkCost);
173 }
174 
175 std::ostream&
176 operator<<(std::ostream& os, const Adjacent& adjacent)
177 {
178  os << "Adjacent: " << adjacent.m_name
179  << "\n\t\tConnecting FaceUri: " << adjacent.m_faceUri
180  << "\n\t\tLink cost: " << adjacent.m_linkCost
181  << "\n\t\tStatus: " << adjacent.m_status
182  << "\n\t\tInterest Timed Out: " << adjacent.m_interestTimedOutNo << std::endl;
183  return os;
184 }
185 
186 } // namespace nlsr
A neighbor reachable over a Face.
Definition: adjacent.hpp:47
static constexpr double NON_ADJACENT_COST
Definition: adjacent.hpp:187
const ndn::FaceUri & getFaceUri() const
Definition: adjacent.hpp:85
void wireDecode(const ndn::Block &wire)
Definition: adjacent.cpp:118
bool operator<(const Adjacent &adjacent) const
Definition: adjacent.cpp:168
void setLinkCost(double lc)
Definition: adjacent.cpp:67
const ndn::Block & wireEncode() const
Definition: adjacent.cpp:100
const ndn::Name & getName() const
Definition: adjacent.hpp:72
bool operator==(const Adjacent &adjacent) const
Equality is when name, Face URI, and link cost are all equal.
Definition: adjacent.cpp:160
double getLinkCost() const
Definition: adjacent.hpp:98
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_ERROR(x)
Definition: logger.hpp:41
bool diffInEpsilon(double lhs, double rhs)
Determine whether the difference between two numbers are within epsilon.
Definition: numeric.hpp:36
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:176
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)