blob: 4afe6d74dbb605b5f1db741d1dfe0bc2179fd036 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dulalsaurabd0816a32019-07-26 13:11:24 +00003 * Copyright (c) 2014-2019, 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
21#include "adjacent.hpp"
22#include "logger.hpp"
23
akmhoque53353462014-04-22 08:43:45 -050024#include <iostream>
25#include <string>
26#include <cmath>
27#include <limits>
akmhoquec8a10f72014-04-25 18:42:55 -050028
akmhoque53353462014-04-22 08:43:45 -050029namespace nlsr {
30
dmcoomescf8d0ed2017-02-21 11:39:01 -060031INIT_LOGGER(Adjacent);
akmhoque674b0b12014-05-20 14:33:28 -050032
dulalsaurabd0816a32019-07-26 13:11:24 +000033const double Adjacent::DEFAULT_LINK_COST = 10.0;
34const double Adjacent::NON_ADJACENT_COST = -12345;
akmhoque157b0a42014-05-13 00:26:37 -050035
36Adjacent::Adjacent()
37 : m_name()
Nick Gordone9733ed2017-04-26 10:48:39 -050038 , m_faceUri()
akmhoque157b0a42014-05-13 00:26:37 -050039 , m_linkCost(DEFAULT_LINK_COST)
Vince Lehmancb76ade2014-08-28 21:24:41 -050040 , m_status(STATUS_INACTIVE)
akmhoque157b0a42014-05-13 00:26:37 -050041 , m_interestTimedOutNo(0)
akmhoquec04e7272014-07-02 11:00:14 -050042 , m_faceId(0)
akmhoque157b0a42014-05-13 00:26:37 -050043{
44}
45
46Adjacent::Adjacent(const ndn::Name& an)
47 : m_name(an)
Nick Gordone9733ed2017-04-26 10:48:39 -050048 , m_faceUri()
akmhoque157b0a42014-05-13 00:26:37 -050049 , m_linkCost(DEFAULT_LINK_COST)
Vince Lehmancb76ade2014-08-28 21:24:41 -050050 , m_status(STATUS_INACTIVE)
akmhoque157b0a42014-05-13 00:26:37 -050051 , m_interestTimedOutNo(0)
akmhoquec04e7272014-07-02 11:00:14 -050052 , m_faceId(0)
akmhoque157b0a42014-05-13 00:26:37 -050053 {
54 }
55
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050056Adjacent::Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc,
Vince Lehmancb76ade2014-08-28 21:24:41 -050057 Status s, uint32_t iton, uint64_t faceId)
akmhoquec04e7272014-07-02 11:00:14 -050058 : m_name(an)
Nick Gordone9733ed2017-04-26 10:48:39 -050059 , m_faceUri(faceUri)
akmhoquec04e7272014-07-02 11:00:14 -050060 , m_status(s)
61 , m_interestTimedOutNo(iton)
62 , m_faceId(faceId)
63 {
dulalsaurabd0816a32019-07-26 13:11:24 +000064 this->setLinkCost(lc);
akmhoquec04e7272014-07-02 11:00:14 -050065 }
akmhoque53353462014-04-22 08:43:45 -050066
dulalsaurabd0816a32019-07-26 13:11:24 +000067void
68Adjacent::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
akmhoque53353462014-04-22 08:43:45 -050081bool
akmhoquefdbddb12014-05-02 18:35:19 -050082Adjacent::operator==(const Adjacent& adjacent) const
akmhoque53353462014-04-22 08:43:45 -050083{
akmhoquefdbddb12014-05-02 18:35:19 -050084 return (m_name == adjacent.getName()) &&
Nick Gordone9733ed2017-04-26 10:48:39 -050085 (m_faceUri == adjacent.getFaceUri()) &&
akmhoquefdbddb12014-05-02 18:35:19 -050086 (std::abs(m_linkCost - adjacent.getLinkCost()) <
Nick Gordone9733ed2017-04-26 10:48:39 -050087 std::numeric_limits<double>::epsilon());
akmhoque53353462014-04-22 08:43:45 -050088}
89
Nick Gordon2a1ac612017-10-06 15:36:49 -050090bool
91Adjacent::operator<(const Adjacent& adjacent) const
92{
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050093 auto linkCost = adjacent.getLinkCost();
94 return std::tie(m_name, m_linkCost) <
95 std::tie(adjacent.getName(), linkCost);
Nick Gordon2a1ac612017-10-06 15:36:49 -050096}
97
98std::ostream&
99operator<<(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
akmhoque674b0b12014-05-20 14:33:28 -0500107void
108Adjacent::writeLog()
109{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500110 NLSR_LOG_DEBUG(*this);
akmhoque674b0b12014-05-20 14:33:28 -0500111}
112
Nick Gordonfad8e252016-08-11 14:21:38 -0500113} // namespace nlsr