blob: a540a5cd37c511bc1737c45c6428379e2dd8c3ce [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Nick Gordond0a7df32017-05-30 16:44:34 -050021
akmhoque53353462014-04-22 08:43:45 -050022#include <string>
akmhoque102aea42014-08-04 10:22:12 -050023#include <cmath>
Nick Gordone9733ed2017-04-26 10:48:39 -050024
akmhoque31d1d4b2014-05-05 22:08:14 -050025#include <ndn-cxx/face.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050026#include <ndn-cxx/net/face-uri.hpp>
akmhoque53353462014-04-22 08:43:45 -050027
akmhoquefdbddb12014-05-02 18:35:19 -050028#ifndef NLSR_ADJACENT_HPP
29#define NLSR_ADJACENT_HPP
akmhoque53353462014-04-22 08:43:45 -050030
31namespace nlsr {
akmhoque157b0a42014-05-13 00:26:37 -050032
Nick Gordond0a7df32017-05-30 16:44:34 -050033/*! \brief A neighbor reachable over a Face.
34 *
35 * Represents another node that we expect to be running NLSR that we
36 * should be able to reach over a direct Face connection.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080037 *
38 * Data abstraction for Adjacent
39 * Adjacent := ADJACENCY-TYPE TLV-LENGTH
40 * Name
41 * FaceUri
42 * LinkCost
43 * AdjacencyStatus
44 * AdjacencyInterestTimedOutNo
Nick Gordond0a7df32017-05-30 16:44:34 -050045 */
akmhoque53353462014-04-22 08:43:45 -050046class Adjacent
47{
akmhoque53353462014-04-22 08:43:45 -050048public:
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080049 class Error : public ndn::tlv::Error
50 {
51 public:
52 explicit
53 Error(const std::string& what)
54 : ndn::tlv::Error(what)
55 {
56 }
57 };
58
Vince Lehmancb76ade2014-08-28 21:24:41 -050059 enum Status
60 {
61 STATUS_UNKNOWN = -1,
62 STATUS_INACTIVE = 0,
63 STATUS_ACTIVE = 1
64 };
65
akmhoque157b0a42014-05-13 00:26:37 -050066 Adjacent();
akmhoque53353462014-04-22 08:43:45 -050067
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080068 Adjacent(const ndn::Block& block);
69
akmhoque157b0a42014-05-13 00:26:37 -050070 Adjacent(const ndn::Name& an);
akmhoque53353462014-04-22 08:43:45 -050071
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050072 Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc,
Vince Lehmancb76ade2014-08-28 21:24:41 -050073 Status s, uint32_t iton, uint64_t faceId);
akmhoque53353462014-04-22 08:43:45 -050074
akmhoque31d1d4b2014-05-05 22:08:14 -050075 const ndn::Name&
akmhoquefdbddb12014-05-02 18:35:19 -050076 getName() const
akmhoque53353462014-04-22 08:43:45 -050077 {
78 return m_name;
79 }
80
81 void
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080082 setName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050083 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080084 m_wire.reset();
85 m_name = name;
akmhoque53353462014-04-22 08:43:45 -050086 }
87
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050088 const ndn::FaceUri&
Nick Gordone9733ed2017-04-26 10:48:39 -050089 getFaceUri() const
akmhoque53353462014-04-22 08:43:45 -050090 {
Nick Gordone9733ed2017-04-26 10:48:39 -050091 return m_faceUri;
akmhoque53353462014-04-22 08:43:45 -050092 }
93
94 void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050095 setFaceUri(const ndn::FaceUri& faceUri)
akmhoque53353462014-04-22 08:43:45 -050096 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080097 m_wire.reset();
Nick Gordone9733ed2017-04-26 10:48:39 -050098 m_faceUri = faceUri;
akmhoque53353462014-04-22 08:43:45 -050099 }
100
dulalsaurabd0816a32019-07-26 13:11:24 +0000101 double
akmhoquefdbddb12014-05-02 18:35:19 -0500102 getLinkCost() const
akmhoque53353462014-04-22 08:43:45 -0500103 {
dulalsaurabd0816a32019-07-26 13:11:24 +0000104 return ceil(m_linkCost);
akmhoque53353462014-04-22 08:43:45 -0500105 }
106
107 void
dulalsaurabd0816a32019-07-26 13:11:24 +0000108 setLinkCost(double lc);
akmhoque53353462014-04-22 08:43:45 -0500109
Vince Lehmancb76ade2014-08-28 21:24:41 -0500110 Status
akmhoquefdbddb12014-05-02 18:35:19 -0500111 getStatus() const
akmhoque53353462014-04-22 08:43:45 -0500112 {
113 return m_status;
114 }
115
116 void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500117 setStatus(Status s)
akmhoque53353462014-04-22 08:43:45 -0500118 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800119 m_wire.reset();
akmhoque53353462014-04-22 08:43:45 -0500120 m_status = s;
121 }
122
akmhoquefdbddb12014-05-02 18:35:19 -0500123 uint32_t
124 getInterestTimedOutNo() const
akmhoque53353462014-04-22 08:43:45 -0500125 {
126 return m_interestTimedOutNo;
127 }
128
129 void
akmhoquefdbddb12014-05-02 18:35:19 -0500130 setInterestTimedOutNo(uint32_t iton)
akmhoque53353462014-04-22 08:43:45 -0500131 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800132 m_wire.reset();
akmhoque53353462014-04-22 08:43:45 -0500133 m_interestTimedOutNo = iton;
134 }
135
akmhoquec04e7272014-07-02 11:00:14 -0500136 void
137 setFaceId(uint64_t faceId)
138 {
139 m_faceId = faceId;
140 }
141
142 uint64_t
Nick Gordond5c1a372016-10-31 13:56:23 -0500143 getFaceId() const
akmhoquec04e7272014-07-02 11:00:14 -0500144 {
145 return m_faceId;
146 }
147
Nick Gordond0a7df32017-05-30 16:44:34 -0500148 /*! \brief Equality is when name, Face URI, and link cost are all equal. */
akmhoque53353462014-04-22 08:43:45 -0500149 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500150 operator==(const Adjacent& adjacent) const;
akmhoque53353462014-04-22 08:43:45 -0500151
Nick Gordon2a1ac612017-10-06 15:36:49 -0500152 bool
153 operator!=(const Adjacent& adjacent) const
154 {
155 return !(*this == adjacent);
156 }
157
158 bool
159 operator<(const Adjacent& adjacent) const;
160
akmhoquec04e7272014-07-02 11:00:14 -0500161 inline bool
Nick Gordon49648702018-01-22 11:57:33 -0600162 compare(const ndn::Name& adjacencyName) const
akmhoquec04e7272014-07-02 11:00:14 -0500163 {
164 return m_name == adjacencyName;
165 }
166
167 inline bool
Nick Gordon49648702018-01-22 11:57:33 -0600168 compareFaceId(const uint64_t faceId) const
akmhoquec04e7272014-07-02 11:00:14 -0500169 {
170 return m_faceId == faceId;
171 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500172
akmhoque102aea42014-08-04 10:22:12 -0500173 inline bool
Nick Gordon49648702018-01-22 11:57:33 -0600174 compareFaceUri(const ndn::FaceUri& faceUri) const
akmhoque102aea42014-08-04 10:22:12 -0500175 {
Nick Gordone9733ed2017-04-26 10:48:39 -0500176 return m_faceUri == faceUri;
akmhoque102aea42014-08-04 10:22:12 -0500177 }
178
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800179 template<ndn::encoding::Tag TAG>
180 size_t
181 wireEncode(ndn::EncodingImpl<TAG>& block) const;
182
183 const ndn::Block&
184 wireEncode() const;
185
186 void
187 wireDecode(const ndn::Block& wire);
188
akmhoque157b0a42014-05-13 00:26:37 -0500189public:
dulalsaurabd0816a32019-07-26 13:11:24 +0000190 static const double DEFAULT_LINK_COST;
191 static const double NON_ADJACENT_COST;
akmhoque157b0a42014-05-13 00:26:37 -0500192
akmhoque53353462014-04-22 08:43:45 -0500193private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500194 /*! m_name The NLSR-configured router name of the neighbor */
akmhoque31d1d4b2014-05-05 22:08:14 -0500195 ndn::Name m_name;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500196 /*! m_faceUri The NFD-level specification of the Face*/
197 ndn::FaceUri m_faceUri;
Nick Gordond0a7df32017-05-30 16:44:34 -0500198 /*! m_linkCost The semi-arbitrary cost to traverse the link. */
akmhoque53353462014-04-22 08:43:45 -0500199 double m_linkCost;
Nick Gordond0a7df32017-05-30 16:44:34 -0500200 /*! m_status Whether the neighbor is active or not */
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800201 Status m_status = STATUS_UNKNOWN;
Nick Gordond0a7df32017-05-30 16:44:34 -0500202 /*! m_interestTimedOutNo How many failed Hello interests we have sent since the last reply */
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800203 uint32_t m_interestTimedOutNo = 0;
Nick Gordond0a7df32017-05-30 16:44:34 -0500204 /*! m_faceId The NFD-assigned ID for the neighbor, used to
205 * determine whether a Face is available */
akmhoquec04e7272014-07-02 11:00:14 -0500206 uint64_t m_faceId;
Nick Gordon2a1ac612017-10-06 15:36:49 -0500207
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800208 mutable ndn::Block m_wire;
209
Nick Gordon2a1ac612017-10-06 15:36:49 -0500210 friend std::ostream&
211 operator<<(std::ostream& os, const Adjacent& adjacent);
akmhoque53353462014-04-22 08:43:45 -0500212};
213
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800214NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Adjacent);
215
Nick Gordon2a1ac612017-10-06 15:36:49 -0500216std::ostream&
217operator<<(std::ostream& os, const Adjacent& adjacent);
218
akmhoque53353462014-04-22 08:43:45 -0500219} // namespace nlsr
220
Nick Gordone9733ed2017-04-26 10:48:39 -0500221#endif // NLSR_ADJACENT_HPP