blob: 178ae785978e5e9141c992b3a50a16923bc67eee [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,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
Nick Gordond0a7df32017-05-30 16:44:34 -050020
akmhoque53353462014-04-22 08:43:45 -050021#include <string>
akmhoque102aea42014-08-04 10:22:12 -050022#include <cmath>
Nick Gordone9733ed2017-04-26 10:48:39 -050023
akmhoque31d1d4b2014-05-05 22:08:14 -050024#include <ndn-cxx/face.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050025#include <ndn-cxx/net/face-uri.hpp>
akmhoque53353462014-04-22 08:43:45 -050026
akmhoquefdbddb12014-05-02 18:35:19 -050027#ifndef NLSR_ADJACENT_HPP
28#define NLSR_ADJACENT_HPP
akmhoque53353462014-04-22 08:43:45 -050029
30namespace nlsr {
akmhoque157b0a42014-05-13 00:26:37 -050031
Nick Gordond0a7df32017-05-30 16:44:34 -050032/*! \brief A neighbor reachable over a Face.
33 *
34 * Represents another node that we expect to be running NLSR that we
35 * should be able to reach over a direct Face connection.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080036 *
37 * Data abstraction for Adjacent
38 * Adjacent := ADJACENCY-TYPE TLV-LENGTH
39 * Name
40 * FaceUri
41 * LinkCost
42 * AdjacencyStatus
43 * AdjacencyInterestTimedOutNo
Nick Gordond0a7df32017-05-30 16:44:34 -050044 */
akmhoque53353462014-04-22 08:43:45 -050045class Adjacent
46{
akmhoque53353462014-04-22 08:43:45 -050047public:
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080048 class Error : public ndn::tlv::Error
49 {
50 public:
51 explicit
52 Error(const std::string& what)
53 : ndn::tlv::Error(what)
54 {
55 }
56 };
57
Vince Lehmancb76ade2014-08-28 21:24:41 -050058 enum Status
59 {
60 STATUS_UNKNOWN = -1,
61 STATUS_INACTIVE = 0,
62 STATUS_ACTIVE = 1
63 };
64
akmhoque157b0a42014-05-13 00:26:37 -050065 Adjacent();
akmhoque53353462014-04-22 08:43:45 -050066
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080067 Adjacent(const ndn::Block& block);
68
akmhoque157b0a42014-05-13 00:26:37 -050069 Adjacent(const ndn::Name& an);
akmhoque53353462014-04-22 08:43:45 -050070
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050071 Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc,
Vince Lehmancb76ade2014-08-28 21:24:41 -050072 Status s, uint32_t iton, uint64_t faceId);
akmhoque53353462014-04-22 08:43:45 -050073
akmhoque31d1d4b2014-05-05 22:08:14 -050074 const ndn::Name&
akmhoquefdbddb12014-05-02 18:35:19 -050075 getName() const
akmhoque53353462014-04-22 08:43:45 -050076 {
77 return m_name;
78 }
79
80 void
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080081 setName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050082 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080083 m_wire.reset();
84 m_name = name;
akmhoque53353462014-04-22 08:43:45 -050085 }
86
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050087 const ndn::FaceUri&
Nick Gordone9733ed2017-04-26 10:48:39 -050088 getFaceUri() const
akmhoque53353462014-04-22 08:43:45 -050089 {
Nick Gordone9733ed2017-04-26 10:48:39 -050090 return m_faceUri;
akmhoque53353462014-04-22 08:43:45 -050091 }
92
93 void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050094 setFaceUri(const ndn::FaceUri& faceUri)
akmhoque53353462014-04-22 08:43:45 -050095 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080096 m_wire.reset();
Nick Gordone9733ed2017-04-26 10:48:39 -050097 m_faceUri = faceUri;
akmhoque53353462014-04-22 08:43:45 -050098 }
99
dulalsaurabd0816a32019-07-26 13:11:24 +0000100 double
akmhoquefdbddb12014-05-02 18:35:19 -0500101 getLinkCost() const
akmhoque53353462014-04-22 08:43:45 -0500102 {
dulalsaurabd0816a32019-07-26 13:11:24 +0000103 return ceil(m_linkCost);
akmhoque53353462014-04-22 08:43:45 -0500104 }
105
106 void
dulalsaurabd0816a32019-07-26 13:11:24 +0000107 setLinkCost(double lc);
akmhoque53353462014-04-22 08:43:45 -0500108
Vince Lehmancb76ade2014-08-28 21:24:41 -0500109 Status
akmhoquefdbddb12014-05-02 18:35:19 -0500110 getStatus() const
akmhoque53353462014-04-22 08:43:45 -0500111 {
112 return m_status;
113 }
114
115 void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500116 setStatus(Status s)
akmhoque53353462014-04-22 08:43:45 -0500117 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800118 m_wire.reset();
akmhoque53353462014-04-22 08:43:45 -0500119 m_status = s;
120 }
121
akmhoquefdbddb12014-05-02 18:35:19 -0500122 uint32_t
123 getInterestTimedOutNo() const
akmhoque53353462014-04-22 08:43:45 -0500124 {
125 return m_interestTimedOutNo;
126 }
127
128 void
akmhoquefdbddb12014-05-02 18:35:19 -0500129 setInterestTimedOutNo(uint32_t iton)
akmhoque53353462014-04-22 08:43:45 -0500130 {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800131 m_wire.reset();
akmhoque53353462014-04-22 08:43:45 -0500132 m_interestTimedOutNo = iton;
133 }
134
akmhoquec04e7272014-07-02 11:00:14 -0500135 void
136 setFaceId(uint64_t faceId)
137 {
138 m_faceId = faceId;
139 }
140
141 uint64_t
Nick Gordond5c1a372016-10-31 13:56:23 -0500142 getFaceId() const
akmhoquec04e7272014-07-02 11:00:14 -0500143 {
144 return m_faceId;
145 }
146
Nick Gordond0a7df32017-05-30 16:44:34 -0500147 /*! \brief Equality is when name, Face URI, and link cost are all equal. */
akmhoque53353462014-04-22 08:43:45 -0500148 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500149 operator==(const Adjacent& adjacent) const;
akmhoque53353462014-04-22 08:43:45 -0500150
Nick Gordon2a1ac612017-10-06 15:36:49 -0500151 bool
152 operator!=(const Adjacent& adjacent) const
153 {
154 return !(*this == adjacent);
155 }
156
157 bool
158 operator<(const Adjacent& adjacent) const;
159
akmhoquec04e7272014-07-02 11:00:14 -0500160 inline bool
Nick Gordon49648702018-01-22 11:57:33 -0600161 compare(const ndn::Name& adjacencyName) const
akmhoquec04e7272014-07-02 11:00:14 -0500162 {
163 return m_name == adjacencyName;
164 }
165
166 inline bool
Nick Gordon49648702018-01-22 11:57:33 -0600167 compareFaceId(const uint64_t faceId) const
akmhoquec04e7272014-07-02 11:00:14 -0500168 {
169 return m_faceId == faceId;
170 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500171
akmhoque102aea42014-08-04 10:22:12 -0500172 inline bool
Nick Gordon49648702018-01-22 11:57:33 -0600173 compareFaceUri(const ndn::FaceUri& faceUri) const
akmhoque102aea42014-08-04 10:22:12 -0500174 {
Nick Gordone9733ed2017-04-26 10:48:39 -0500175 return m_faceUri == faceUri;
akmhoque102aea42014-08-04 10:22:12 -0500176 }
177
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800178 template<ndn::encoding::Tag TAG>
179 size_t
180 wireEncode(ndn::EncodingImpl<TAG>& block) const;
181
182 const ndn::Block&
183 wireEncode() const;
184
185 void
186 wireDecode(const ndn::Block& wire);
187
akmhoque157b0a42014-05-13 00:26:37 -0500188public:
dulalsaurabd0816a32019-07-26 13:11:24 +0000189 static const double DEFAULT_LINK_COST;
190 static const double NON_ADJACENT_COST;
akmhoque157b0a42014-05-13 00:26:37 -0500191
akmhoque53353462014-04-22 08:43:45 -0500192private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500193 /*! m_name The NLSR-configured router name of the neighbor */
akmhoque31d1d4b2014-05-05 22:08:14 -0500194 ndn::Name m_name;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500195 /*! m_faceUri The NFD-level specification of the Face*/
196 ndn::FaceUri m_faceUri;
Nick Gordond0a7df32017-05-30 16:44:34 -0500197 /*! m_linkCost The semi-arbitrary cost to traverse the link. */
akmhoque53353462014-04-22 08:43:45 -0500198 double m_linkCost;
Nick Gordond0a7df32017-05-30 16:44:34 -0500199 /*! m_status Whether the neighbor is active or not */
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800200 Status m_status = STATUS_UNKNOWN;
Nick Gordond0a7df32017-05-30 16:44:34 -0500201 /*! m_interestTimedOutNo How many failed Hello interests we have sent since the last reply */
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800202 uint32_t m_interestTimedOutNo = 0;
Nick Gordond0a7df32017-05-30 16:44:34 -0500203 /*! m_faceId The NFD-assigned ID for the neighbor, used to
204 * determine whether a Face is available */
akmhoquec04e7272014-07-02 11:00:14 -0500205 uint64_t m_faceId;
Nick Gordon2a1ac612017-10-06 15:36:49 -0500206
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800207 mutable ndn::Block m_wire;
208
Nick Gordon2a1ac612017-10-06 15:36:49 -0500209 friend std::ostream&
210 operator<<(std::ostream& os, const Adjacent& adjacent);
akmhoque53353462014-04-22 08:43:45 -0500211};
212
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800213NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Adjacent);
214
Nick Gordon2a1ac612017-10-06 15:36:49 -0500215std::ostream&
216operator<<(std::ostream& os, const Adjacent& adjacent);
217
akmhoque53353462014-04-22 08:43:45 -0500218} // namespace nlsr
219
Nick Gordone9733ed2017-04-26 10:48:39 -0500220#endif // NLSR_ADJACENT_HPP