blob: b5b93a9f9ee42e4bf5094f20b2d8429155e9372a [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventod90338d2021-01-07 17:50:05 -05002/*
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04003 * Copyright (c) 2014-2022, 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/>.
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040020 */
Nick Gordond0a7df32017-05-30 16:44:34 -050021
Davide Pesaventod90338d2021-01-07 17:50:05 -050022#ifndef NLSR_ADJACENT_HPP
23#define NLSR_ADJACENT_HPP
24
akmhoque102aea42014-08-04 10:22:12 -050025#include <cmath>
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040026#include <string>
Nick Gordone9733ed2017-04-26 10:48:39 -050027
akmhoque31d1d4b2014-05-05 22:08:14 -050028#include <ndn-cxx/face.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050029#include <ndn-cxx/net/face-uri.hpp>
akmhoque53353462014-04-22 08:43:45 -050030
akmhoque53353462014-04-22 08:43:45 -050031namespace 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:
Davide Pesaventod90338d2021-01-07 17:50:05 -050052 using ndn::tlv::Error::Error;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080053 };
54
Vince Lehmancb76ade2014-08-28 21:24:41 -050055 enum Status
56 {
57 STATUS_UNKNOWN = -1,
58 STATUS_INACTIVE = 0,
59 STATUS_ACTIVE = 1
60 };
61
akmhoque157b0a42014-05-13 00:26:37 -050062 Adjacent();
akmhoque53353462014-04-22 08:43:45 -050063
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080064 Adjacent(const ndn::Block& block);
65
akmhoque157b0a42014-05-13 00:26:37 -050066 Adjacent(const ndn::Name& an);
akmhoque53353462014-04-22 08:43:45 -050067
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050068 Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc,
Vince Lehmancb76ade2014-08-28 21:24:41 -050069 Status s, uint32_t iton, uint64_t faceId);
akmhoque53353462014-04-22 08:43:45 -050070
akmhoque31d1d4b2014-05-05 22:08:14 -050071 const ndn::Name&
akmhoquefdbddb12014-05-02 18:35:19 -050072 getName() const
akmhoque53353462014-04-22 08:43:45 -050073 {
74 return m_name;
75 }
76
77 void
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080078 setName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050079 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080080 m_wire.reset();
81 m_name = name;
akmhoque53353462014-04-22 08:43:45 -050082 }
83
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050084 const ndn::FaceUri&
Nick Gordone9733ed2017-04-26 10:48:39 -050085 getFaceUri() const
akmhoque53353462014-04-22 08:43:45 -050086 {
Nick Gordone9733ed2017-04-26 10:48:39 -050087 return m_faceUri;
akmhoque53353462014-04-22 08:43:45 -050088 }
89
90 void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050091 setFaceUri(const ndn::FaceUri& faceUri)
akmhoque53353462014-04-22 08:43:45 -050092 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080093 m_wire.reset();
Nick Gordone9733ed2017-04-26 10:48:39 -050094 m_faceUri = faceUri;
akmhoque53353462014-04-22 08:43:45 -050095 }
96
dulalsaurabd0816a32019-07-26 13:11:24 +000097 double
akmhoquefdbddb12014-05-02 18:35:19 -050098 getLinkCost() const
akmhoque53353462014-04-22 08:43:45 -050099 {
dulalsaurabd0816a32019-07-26 13:11:24 +0000100 return ceil(m_linkCost);
akmhoque53353462014-04-22 08:43:45 -0500101 }
102
103 void
dulalsaurabd0816a32019-07-26 13:11:24 +0000104 setLinkCost(double lc);
akmhoque53353462014-04-22 08:43:45 -0500105
Vince Lehmancb76ade2014-08-28 21:24:41 -0500106 Status
akmhoquefdbddb12014-05-02 18:35:19 -0500107 getStatus() const
akmhoque53353462014-04-22 08:43:45 -0500108 {
109 return m_status;
110 }
111
112 void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500113 setStatus(Status s)
akmhoque53353462014-04-22 08:43:45 -0500114 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800115 m_wire.reset();
akmhoque53353462014-04-22 08:43:45 -0500116 m_status = s;
117 }
118
akmhoquefdbddb12014-05-02 18:35:19 -0500119 uint32_t
120 getInterestTimedOutNo() const
akmhoque53353462014-04-22 08:43:45 -0500121 {
122 return m_interestTimedOutNo;
123 }
124
125 void
akmhoquefdbddb12014-05-02 18:35:19 -0500126 setInterestTimedOutNo(uint32_t iton)
akmhoque53353462014-04-22 08:43:45 -0500127 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800128 m_wire.reset();
akmhoque53353462014-04-22 08:43:45 -0500129 m_interestTimedOutNo = iton;
130 }
131
akmhoquec04e7272014-07-02 11:00:14 -0500132 void
133 setFaceId(uint64_t faceId)
134 {
135 m_faceId = faceId;
136 }
137
138 uint64_t
Nick Gordond5c1a372016-10-31 13:56:23 -0500139 getFaceId() const
akmhoquec04e7272014-07-02 11:00:14 -0500140 {
141 return m_faceId;
142 }
143
Nick Gordond0a7df32017-05-30 16:44:34 -0500144 /*! \brief Equality is when name, Face URI, and link cost are all equal. */
akmhoque53353462014-04-22 08:43:45 -0500145 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500146 operator==(const Adjacent& adjacent) const;
akmhoque53353462014-04-22 08:43:45 -0500147
Nick Gordon2a1ac612017-10-06 15:36:49 -0500148 bool
149 operator!=(const Adjacent& adjacent) const
150 {
151 return !(*this == adjacent);
152 }
153
154 bool
155 operator<(const Adjacent& adjacent) const;
156
akmhoquec04e7272014-07-02 11:00:14 -0500157 inline bool
Nick Gordon49648702018-01-22 11:57:33 -0600158 compare(const ndn::Name& adjacencyName) const
akmhoquec04e7272014-07-02 11:00:14 -0500159 {
160 return m_name == adjacencyName;
161 }
162
163 inline bool
Nick Gordon49648702018-01-22 11:57:33 -0600164 compareFaceId(const uint64_t faceId) const
akmhoquec04e7272014-07-02 11:00:14 -0500165 {
166 return m_faceId == faceId;
167 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500168
akmhoque102aea42014-08-04 10:22:12 -0500169 inline bool
Nick Gordon49648702018-01-22 11:57:33 -0600170 compareFaceUri(const ndn::FaceUri& faceUri) const
akmhoque102aea42014-08-04 10:22:12 -0500171 {
Nick Gordone9733ed2017-04-26 10:48:39 -0500172 return m_faceUri == faceUri;
akmhoque102aea42014-08-04 10:22:12 -0500173 }
174
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800175 template<ndn::encoding::Tag TAG>
176 size_t
177 wireEncode(ndn::EncodingImpl<TAG>& block) const;
178
179 const ndn::Block&
180 wireEncode() const;
181
182 void
183 wireDecode(const ndn::Block& wire);
184
akmhoque157b0a42014-05-13 00:26:37 -0500185public:
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400186 static constexpr double DEFAULT_LINK_COST = 10.0;
187 static constexpr double NON_ADJACENT_COST = -12345.0;
akmhoque157b0a42014-05-13 00:26:37 -0500188
akmhoque53353462014-04-22 08:43:45 -0500189private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500190 /*! m_name The NLSR-configured router name of the neighbor */
akmhoque31d1d4b2014-05-05 22:08:14 -0500191 ndn::Name m_name;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500192 /*! m_faceUri The NFD-level specification of the Face*/
193 ndn::FaceUri m_faceUri;
Nick Gordond0a7df32017-05-30 16:44:34 -0500194 /*! m_linkCost The semi-arbitrary cost to traverse the link. */
akmhoque53353462014-04-22 08:43:45 -0500195 double m_linkCost;
Nick Gordond0a7df32017-05-30 16:44:34 -0500196 /*! m_status Whether the neighbor is active or not */
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800197 Status m_status = STATUS_UNKNOWN;
Nick Gordond0a7df32017-05-30 16:44:34 -0500198 /*! m_interestTimedOutNo How many failed Hello interests we have sent since the last reply */
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800199 uint32_t m_interestTimedOutNo = 0;
Nick Gordond0a7df32017-05-30 16:44:34 -0500200 /*! m_faceId The NFD-assigned ID for the neighbor, used to
201 * determine whether a Face is available */
akmhoquec04e7272014-07-02 11:00:14 -0500202 uint64_t m_faceId;
Nick Gordon2a1ac612017-10-06 15:36:49 -0500203
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800204 mutable ndn::Block m_wire;
205
Nick Gordon2a1ac612017-10-06 15:36:49 -0500206 friend std::ostream&
207 operator<<(std::ostream& os, const Adjacent& adjacent);
akmhoque53353462014-04-22 08:43:45 -0500208};
209
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800210NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Adjacent);
211
Nick Gordon2a1ac612017-10-06 15:36:49 -0500212std::ostream&
213operator<<(std::ostream& os, const Adjacent& adjacent);
214
akmhoque53353462014-04-22 08:43:45 -0500215} // namespace nlsr
216
Nick Gordone9733ed2017-04-26 10:48:39 -0500217#endif // NLSR_ADJACENT_HPP