akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2021, The University of Memphis, |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 21 | |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 22 | #ifndef NLSR_ADJACENT_HPP |
| 23 | #define NLSR_ADJACENT_HPP |
| 24 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 25 | #include <string> |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 26 | #include <cmath> |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 27 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/face.hpp> |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/net/face-uri.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 30 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | namespace nlsr { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 32 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 33 | /*! \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 Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 37 | * |
| 38 | * Data abstraction for Adjacent |
| 39 | * Adjacent := ADJACENCY-TYPE TLV-LENGTH |
| 40 | * Name |
| 41 | * FaceUri |
| 42 | * LinkCost |
| 43 | * AdjacencyStatus |
| 44 | * AdjacencyInterestTimedOutNo |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 45 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | class Adjacent |
| 47 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | public: |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 49 | class Error : public ndn::tlv::Error |
| 50 | { |
| 51 | public: |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 52 | using ndn::tlv::Error::Error; |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 55 | enum Status |
| 56 | { |
| 57 | STATUS_UNKNOWN = -1, |
| 58 | STATUS_INACTIVE = 0, |
| 59 | STATUS_ACTIVE = 1 |
| 60 | }; |
| 61 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 62 | Adjacent(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 63 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 64 | Adjacent(const ndn::Block& block); |
| 65 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 66 | Adjacent(const ndn::Name& an); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 67 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 68 | Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc, |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 69 | Status s, uint32_t iton, uint64_t faceId); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 71 | const ndn::Name& |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 72 | getName() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 73 | { |
| 74 | return m_name; |
| 75 | } |
| 76 | |
| 77 | void |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 78 | setName(const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 80 | m_wire.reset(); |
| 81 | m_name = name; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 84 | const ndn::FaceUri& |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 85 | getFaceUri() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 86 | { |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 87 | return m_faceUri; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 91 | setFaceUri(const ndn::FaceUri& faceUri) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 92 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 93 | m_wire.reset(); |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 94 | m_faceUri = faceUri; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | } |
| 96 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 97 | double |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 98 | getLinkCost() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | { |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 100 | return ceil(m_linkCost); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 104 | setLinkCost(double lc); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 105 | |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 106 | Status |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 107 | getStatus() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 108 | { |
| 109 | return m_status; |
| 110 | } |
| 111 | |
| 112 | void |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 113 | setStatus(Status s) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 115 | m_wire.reset(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 116 | m_status = s; |
| 117 | } |
| 118 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 119 | uint32_t |
| 120 | getInterestTimedOutNo() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | { |
| 122 | return m_interestTimedOutNo; |
| 123 | } |
| 124 | |
| 125 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 126 | setInterestTimedOutNo(uint32_t iton) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 127 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 128 | m_wire.reset(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | m_interestTimedOutNo = iton; |
| 130 | } |
| 131 | |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 132 | void |
| 133 | setFaceId(uint64_t faceId) |
| 134 | { |
| 135 | m_faceId = faceId; |
| 136 | } |
| 137 | |
| 138 | uint64_t |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 139 | getFaceId() const |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 140 | { |
| 141 | return m_faceId; |
| 142 | } |
| 143 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 144 | /*! \brief Equality is when name, Face URI, and link cost are all equal. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 145 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 146 | operator==(const Adjacent& adjacent) const; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 147 | |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 148 | bool |
| 149 | operator!=(const Adjacent& adjacent) const |
| 150 | { |
| 151 | return !(*this == adjacent); |
| 152 | } |
| 153 | |
| 154 | bool |
| 155 | operator<(const Adjacent& adjacent) const; |
| 156 | |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 157 | inline bool |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 158 | compare(const ndn::Name& adjacencyName) const |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 159 | { |
| 160 | return m_name == adjacencyName; |
| 161 | } |
| 162 | |
| 163 | inline bool |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 164 | compareFaceId(const uint64_t faceId) const |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 165 | { |
| 166 | return m_faceId == faceId; |
| 167 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 168 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 169 | inline bool |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 170 | compareFaceUri(const ndn::FaceUri& faceUri) const |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 171 | { |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 172 | return m_faceUri == faceUri; |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 175 | 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 185 | public: |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 186 | static const double DEFAULT_LINK_COST; |
| 187 | static const double NON_ADJACENT_COST; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 188 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 189 | private: |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 190 | /*! m_name The NLSR-configured router name of the neighbor */ |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 191 | ndn::Name m_name; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 192 | /*! m_faceUri The NFD-level specification of the Face*/ |
| 193 | ndn::FaceUri m_faceUri; |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 194 | /*! m_linkCost The semi-arbitrary cost to traverse the link. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 195 | double m_linkCost; |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 196 | /*! m_status Whether the neighbor is active or not */ |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 197 | Status m_status = STATUS_UNKNOWN; |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 198 | /*! m_interestTimedOutNo How many failed Hello interests we have sent since the last reply */ |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 199 | uint32_t m_interestTimedOutNo = 0; |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 200 | /*! m_faceId The NFD-assigned ID for the neighbor, used to |
| 201 | * determine whether a Face is available */ |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 202 | uint64_t m_faceId; |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 203 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 204 | mutable ndn::Block m_wire; |
| 205 | |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 206 | friend std::ostream& |
| 207 | operator<<(std::ostream& os, const Adjacent& adjacent); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 208 | }; |
| 209 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 210 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Adjacent); |
| 211 | |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 212 | std::ostream& |
| 213 | operator<<(std::ostream& os, const Adjacent& adjacent); |
| 214 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 215 | } // namespace nlsr |
| 216 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 217 | #endif // NLSR_ADJACENT_HPP |