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