akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 19 | **/ |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 20 | |
| 21 | #include "adjacent.hpp" |
| 22 | #include "logger.hpp" |
| 23 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | #include <iostream> |
| 25 | #include <string> |
| 26 | #include <cmath> |
| 27 | #include <limits> |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 28 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | namespace nlsr { |
| 30 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 31 | INIT_LOGGER(Adjacent); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 32 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame^] | 33 | const double Adjacent::DEFAULT_LINK_COST = 10.0; |
| 34 | const double Adjacent::NON_ADJACENT_COST = -12345; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 35 | |
| 36 | Adjacent::Adjacent() |
| 37 | : m_name() |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 38 | , m_faceUri() |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 39 | , m_linkCost(DEFAULT_LINK_COST) |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 40 | , m_status(STATUS_INACTIVE) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 41 | , m_interestTimedOutNo(0) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 42 | , m_faceId(0) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | Adjacent::Adjacent(const ndn::Name& an) |
| 47 | : m_name(an) |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 48 | , m_faceUri() |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 49 | , m_linkCost(DEFAULT_LINK_COST) |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 50 | , m_status(STATUS_INACTIVE) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 51 | , m_interestTimedOutNo(0) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 52 | , m_faceId(0) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 56 | Adjacent::Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc, |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 57 | Status s, uint32_t iton, uint64_t faceId) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 58 | : m_name(an) |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 59 | , m_faceUri(faceUri) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 60 | , m_status(s) |
| 61 | , m_interestTimedOutNo(iton) |
| 62 | , m_faceId(faceId) |
| 63 | { |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame^] | 64 | this->setLinkCost(lc); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 65 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame^] | 67 | void |
| 68 | Adjacent::setLinkCost(double lc) |
| 69 | { |
| 70 | // NON_ADJACENT_COST is a negative value and is used for nodes that aren't direct neighbors. |
| 71 | // But for direct/active neighbors, the cost cannot be negative. |
| 72 | if (lc < 0 && lc != NON_ADJACENT_COST) |
| 73 | { |
| 74 | NLSR_LOG_ERROR(" Neighbor's link-cost cannot be negative"); |
| 75 | BOOST_THROW_EXCEPTION(ndn::tlv::Error("Neighbor's link-cost cannot be negative")); |
| 76 | } |
| 77 | |
| 78 | m_linkCost = lc; |
| 79 | } |
| 80 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 82 | Adjacent::operator==(const Adjacent& adjacent) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 83 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 84 | return (m_name == adjacent.getName()) && |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 85 | (m_faceUri == adjacent.getFaceUri()) && |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 86 | (std::abs(m_linkCost - adjacent.getLinkCost()) < |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 87 | std::numeric_limits<double>::epsilon()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 90 | bool |
| 91 | Adjacent::operator<(const Adjacent& adjacent) const |
| 92 | { |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 93 | auto linkCost = adjacent.getLinkCost(); |
| 94 | return std::tie(m_name, m_linkCost) < |
| 95 | std::tie(adjacent.getName(), linkCost); |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | std::ostream& |
| 99 | operator<<(std::ostream& os, const Adjacent& adjacent) |
| 100 | { |
| 101 | os << "Adjacent: " << adjacent.m_name << "\n Connecting FaceUri: " << adjacent.m_faceUri |
| 102 | << "\n Link cost: " << adjacent.m_linkCost << "\n Status: " << adjacent.m_status |
| 103 | << "\n Interest Timed Out: " << adjacent.m_interestTimedOutNo << std::endl; |
| 104 | return os; |
| 105 | } |
| 106 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 107 | void |
| 108 | Adjacent::writeLog() |
| 109 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 110 | NLSR_LOG_DEBUG(*this); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 113 | } // namespace nlsr |