akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
Junxiao Shi | b573484 | 2024-01-09 21:14:53 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 5 | * |
| 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/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 19 | */ |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 20 | |
| 21 | #include "adjacent.hpp" |
| 22 | #include "logger.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 23 | #include "tlv-nlsr.hpp" |
Junxiao Shi | b573484 | 2024-01-09 21:14:53 +0000 | [diff] [blame] | 24 | #include "utility/numeric.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 25 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 26 | namespace nlsr { |
| 27 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 28 | INIT_LOGGER(Adjacent); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 29 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 30 | Adjacent::Adjacent() |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 31 | : m_name() |
| 32 | , m_faceUri() |
| 33 | , m_linkCost(DEFAULT_LINK_COST) |
| 34 | , m_status(STATUS_INACTIVE) |
| 35 | , m_interestTimedOutNo(0) |
| 36 | , m_faceId(0) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 40 | Adjacent::Adjacent(const ndn::Block& block) |
| 41 | { |
| 42 | wireDecode(block); |
| 43 | } |
| 44 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 45 | Adjacent::Adjacent(const ndn::Name& an) |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 46 | : m_name(an) |
| 47 | , m_faceUri() |
| 48 | , m_linkCost(DEFAULT_LINK_COST) |
| 49 | , m_status(STATUS_INACTIVE) |
| 50 | , m_interestTimedOutNo(0) |
| 51 | , m_faceId(0) |
| 52 | { |
| 53 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 54 | |
Ashlesh Gawande | 4187857 | 2019-09-29 00:16:02 -0500 | [diff] [blame] | 55 | Adjacent::Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc, |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 56 | Status s, uint32_t iton, uint64_t faceId) |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 57 | : m_name(an) |
| 58 | , m_faceUri(faceUri) |
| 59 | , m_status(s) |
| 60 | , m_interestTimedOutNo(iton) |
| 61 | , m_faceId(faceId) |
| 62 | { |
| 63 | this->setLinkCost(lc); |
| 64 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 65 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 66 | void |
| 67 | Adjacent::setLinkCost(double lc) |
| 68 | { |
| 69 | // NON_ADJACENT_COST is a negative value and is used for nodes that aren't direct neighbors. |
| 70 | // But for direct/active neighbors, the cost cannot be negative. |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 71 | if (lc < 0 && lc != NON_ADJACENT_COST) { |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 72 | NLSR_LOG_ERROR(" Neighbor's link-cost cannot be negative"); |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 73 | NDN_THROW(ndn::tlv::Error("Neighbor's link-cost cannot be negative")); |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | m_linkCost = lc; |
| 77 | } |
| 78 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 79 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacent); |
| 80 | |
| 81 | template<ndn::encoding::Tag TAG> |
| 82 | size_t |
| 83 | Adjacent::wireEncode(ndn::EncodingImpl<TAG>& encoder) const |
| 84 | { |
| 85 | size_t totalLength = 0; |
| 86 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 87 | totalLength += prependDoubleBlock(encoder, nlsr::tlv::Cost, m_linkCost); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 88 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 89 | totalLength += prependStringBlock(encoder, nlsr::tlv::Uri, m_faceUri.toString()); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 90 | |
| 91 | totalLength += m_name.wireEncode(encoder); |
| 92 | |
| 93 | totalLength += encoder.prependVarNumber(totalLength); |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 94 | totalLength += encoder.prependVarNumber(nlsr::tlv::Adjacency); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 95 | |
| 96 | return totalLength; |
| 97 | } |
| 98 | |
| 99 | const ndn::Block& |
| 100 | Adjacent::wireEncode() const |
| 101 | { |
| 102 | if (m_wire.hasWire()) { |
| 103 | return m_wire; |
| 104 | } |
| 105 | |
| 106 | ndn::EncodingEstimator estimator; |
| 107 | size_t estimatedSize = wireEncode(estimator); |
| 108 | |
| 109 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 110 | wireEncode(buffer); |
| 111 | |
| 112 | m_wire = buffer.block(); |
| 113 | |
| 114 | return m_wire; |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | Adjacent::wireDecode(const ndn::Block& wire) |
| 119 | { |
| 120 | m_name.clear(); |
| 121 | m_faceUri = ndn::FaceUri(); |
| 122 | m_linkCost = 0; |
| 123 | |
| 124 | m_wire = wire; |
| 125 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 126 | if (m_wire.type() != nlsr::tlv::Adjacency) { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 127 | NDN_THROW(Error("Adjacency", m_wire.type())); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | m_wire.parse(); |
| 131 | |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 132 | auto val = m_wire.elements_begin(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 133 | |
| 134 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) { |
| 135 | m_name.wireDecode(*val); |
| 136 | ++val; |
| 137 | } |
| 138 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 139 | NDN_THROW(Error("Missing required Name field")); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 142 | if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Uri) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 143 | m_faceUri = ndn::FaceUri(readString(*val)); |
| 144 | ++val; |
| 145 | } |
| 146 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 147 | NDN_THROW(Error("Missing required Uri field")); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 150 | if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Cost) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 151 | m_linkCost = ndn::encoding::readDouble(*val); |
| 152 | ++val; |
| 153 | } |
| 154 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 155 | NDN_THROW(Error("Missing required Cost field")); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 159 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 160 | Adjacent::operator==(const Adjacent& adjacent) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 161 | { |
Junxiao Shi | b573484 | 2024-01-09 21:14:53 +0000 | [diff] [blame] | 162 | return m_name == adjacent.getName() && |
| 163 | m_faceUri == adjacent.getFaceUri() && |
| 164 | util::diffInEpsilon(m_linkCost, adjacent.getLinkCost()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 167 | bool |
| 168 | Adjacent::operator<(const Adjacent& adjacent) const |
| 169 | { |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 170 | auto linkCost = adjacent.getLinkCost(); |
| 171 | return std::tie(m_name, m_linkCost) < |
| 172 | std::tie(adjacent.getName(), linkCost); |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | std::ostream& |
| 176 | operator<<(std::ostream& os, const Adjacent& adjacent) |
| 177 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 178 | os << "Adjacent: " << adjacent.m_name |
| 179 | << "\n\t\tConnecting FaceUri: " << adjacent.m_faceUri |
| 180 | << "\n\t\tLink cost: " << adjacent.m_linkCost |
| 181 | << "\n\t\tStatus: " << adjacent.m_status |
| 182 | << "\n\t\tInterest Timed Out: " << adjacent.m_interestTimedOutNo << std::endl; |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 183 | return os; |
| 184 | } |
| 185 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 186 | } // namespace nlsr |