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-2022, 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 
25 namespace nlsr {
26 
27 INIT_LOGGER(Adjacent);
28 
30  : m_name()
31  , m_faceUri()
32  , m_linkCost(DEFAULT_LINK_COST)
33  , m_status(STATUS_INACTIVE)
34  , m_interestTimedOutNo(0)
35  , m_faceId(0)
36 {
37 }
38 
39 Adjacent::Adjacent(const ndn::Block& block)
40 {
41  wireDecode(block);
42 }
43 
44 Adjacent::Adjacent(const ndn::Name& an)
45  : m_name(an)
46  , m_faceUri()
47  , m_linkCost(DEFAULT_LINK_COST)
48  , m_status(STATUS_INACTIVE)
49  , m_interestTimedOutNo(0)
50  , m_faceId(0)
51 {
52 }
53 
54 Adjacent::Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc,
55  Status s, uint32_t iton, uint64_t faceId)
56  : m_name(an)
57  , m_faceUri(faceUri)
58  , m_status(s)
59  , m_interestTimedOutNo(iton)
60  , m_faceId(faceId)
61 {
62  this->setLinkCost(lc);
63 }
64 
65 void
67 {
68  // NON_ADJACENT_COST is a negative value and is used for nodes that aren't direct neighbors.
69  // But for direct/active neighbors, the cost cannot be negative.
70  if (lc < 0 && lc != NON_ADJACENT_COST) {
71  NLSR_LOG_ERROR(" Neighbor's link-cost cannot be negative");
72  NDN_THROW(ndn::tlv::Error("Neighbor's link-cost cannot be negative"));
73  }
74 
75  m_linkCost = lc;
76 }
77 
79 
80 template<ndn::encoding::Tag TAG>
81 size_t
82 Adjacent::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
83 {
84  size_t totalLength = 0;
85 
86  totalLength += prependDoubleBlock(encoder, nlsr::tlv::Cost, m_linkCost);
87 
88  totalLength += prependStringBlock(encoder, nlsr::tlv::Uri, m_faceUri.toString());
89 
90  totalLength += m_name.wireEncode(encoder);
91 
92  totalLength += encoder.prependVarNumber(totalLength);
93  totalLength += encoder.prependVarNumber(nlsr::tlv::Adjacency);
94 
95  return totalLength;
96 }
97 
98 const ndn::Block&
100 {
101  if (m_wire.hasWire()) {
102  return m_wire;
103  }
104 
105  ndn::EncodingEstimator estimator;
106  size_t estimatedSize = wireEncode(estimator);
107 
108  ndn::EncodingBuffer buffer(estimatedSize, 0);
109  wireEncode(buffer);
110 
111  m_wire = buffer.block();
112 
113  return m_wire;
114 }
115 
116 void
117 Adjacent::wireDecode(const ndn::Block& wire)
118 {
119  m_name.clear();
120  m_faceUri = ndn::FaceUri();
121  m_linkCost = 0;
122 
123  m_wire = wire;
124 
125  if (m_wire.type() != nlsr::tlv::Adjacency) {
126  NDN_THROW(Error("Adjacency", m_wire.type()));
127  }
128 
129  m_wire.parse();
130 
131  auto val = m_wire.elements_begin();
132 
133  if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) {
134  m_name.wireDecode(*val);
135  ++val;
136  }
137  else {
138  NDN_THROW(Error("Missing required Name field"));
139  }
140 
141  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Uri) {
142  m_faceUri = ndn::FaceUri(readString(*val));
143  ++val;
144  }
145  else {
146  NDN_THROW(Error("Missing required Uri field"));
147  }
148 
149  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Cost) {
150  m_linkCost = ndn::encoding::readDouble(*val);
151  ++val;
152  }
153  else {
154  NDN_THROW(Error("Missing required Cost field"));
155  }
156 }
157 
158 bool
159 Adjacent::operator==(const Adjacent& adjacent) const
160 {
161  return (m_name == adjacent.getName()) &&
162  (m_faceUri == adjacent.getFaceUri()) &&
163  (std::abs(m_linkCost - adjacent.getLinkCost()) <
164  std::numeric_limits<double>::epsilon());
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:117
bool operator<(const Adjacent &adjacent) const
Definition: adjacent.cpp:168
void setLinkCost(double lc)
Definition: adjacent.cpp:66
const ndn::Block & wireEncode() const
Definition: adjacent.cpp:99
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:159
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
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)