blob: 9ea814aae6cd539c385a93238760b73b673a7b53 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04002/*
awlane6d7c37f2025-03-07 11:53:58 -06003 * Copyright (c) 2014-2025, The University of Memphis,
alvy297f4162015-03-03 17:15:33 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040020 */
alvy297f4162015-03-03 17:15:33 -060021
akmhoquefdbddb12014-05-02 18:35:19 -050022#ifndef NLSR_NAME_PREFIX_LIST_HPP
23#define NLSR_NAME_PREFIX_LIST_HPP
akmhoque53353462014-04-22 08:43:45 -050024
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080025#include "test-access-control.hpp"
26
Junxiao Shi52f16642023-08-15 00:18:35 +000027#include <ndn-cxx/name.hpp>
28
29#include <boost/operators.hpp>
30
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040031#include <initializer_list>
akmhoque53353462014-04-22 08:43:45 -050032#include <list>
Junxiao Shi52f16642023-08-15 00:18:35 +000033#include <map>
34#include <set>
akmhoque53353462014-04-22 08:43:45 -050035#include <string>
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040036
akmhoque53353462014-04-22 08:43:45 -050037namespace nlsr {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080038
awlane6d7c37f2025-03-07 11:53:58 -060039class PrefixInfo : private boost::equality_comparable<PrefixInfo>
40{
41public:
42 class Error : public ndn::tlv::Error
43 {
44 public:
45 using ndn::tlv::Error::Error;
46 };
47
48 PrefixInfo() = default;
49
50 PrefixInfo(const ndn::Block& block)
51 {
52 wireDecode(block);
53 }
54
55 PrefixInfo(const ndn::Name& name, double cost)
56 : m_prefixName(name),
57 m_prefixCost(cost)
58 {
59 }
60
61 const ndn::Name& getName() const
62 {
63 return m_prefixName;
64 }
65
66 double getCost() const
67 {
68 return m_prefixCost;
69 }
70
71 template<ndn::encoding::Tag TAG>
72 size_t
73 wireEncode(ndn::EncodingImpl<TAG>& block) const;
74
75 const ndn::Block&
76 wireEncode() const;
77
78 void
79 wireDecode(const ndn::Block& wire);
80
81private:
82 friend bool
83 operator==(const PrefixInfo& lhs, const PrefixInfo& rhs)
84 {
85 return (lhs.getName() == rhs.getName()) && (lhs.getCost() == rhs.getCost());
86 }
87
88 friend std::ostream&
89 operator<<(std::ostream& os, const PrefixInfo& info)
90 {
91 os << "Prefix Info: (" << info.getName() << ", " << info.getCost() << ")\n";
92 return os;
93 }
94
95private:
96 ndn::Name m_prefixName;
97 double m_prefixCost;
98
99 mutable ndn::Block m_wire;
100};
101
Junxiao Shi52f16642023-08-15 00:18:35 +0000102class NamePrefixList : private boost::equality_comparable<NamePrefixList>
akmhoque53353462014-04-22 08:43:45 -0500103{
akmhoque53353462014-04-22 08:43:45 -0500104public:
akmhoquec8a10f72014-04-25 18:42:55 -0500105 NamePrefixList();
akmhoque53353462014-04-22 08:43:45 -0500106
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400107 explicit
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000108 NamePrefixList(std::initializer_list<ndn::Name> names);
Nick Gordon96861ca2017-10-17 18:25:21 -0500109
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000110 /*! \brief Inserts name and source combination.
111 \retval true Name and source combination is inserted.
112 \retval false Name and source combination already exists.
alvy297f4162015-03-03 17:15:33 -0600113 */
114 bool
awlane6d7c37f2025-03-07 11:53:58 -0600115 insert(const ndn::Name& name, const std::string& source = "", double cost = 0);
116
117 bool
118 insert(const PrefixInfo& nameCost);
akmhoque53353462014-04-22 08:43:45 -0500119
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000120 /*! \brief Deletes name and source combination
121 \retval true Name and source combination is deleted.
122 \retval false Name and source combination does not exist.
alvy297f4162015-03-03 17:15:33 -0600123 */
124 bool
Junxiao Shi52f16642023-08-15 00:18:35 +0000125 erase(const ndn::Name& name, const std::string& source = "");
akmhoque53353462014-04-22 08:43:45 -0500126
akmhoque31d1d4b2014-05-05 22:08:14 -0500127 size_t
Nick Gordonff9a6272017-10-12 13:38:29 -0500128 size() const
akmhoque53353462014-04-22 08:43:45 -0500129 {
Junxiao Shi52f16642023-08-15 00:18:35 +0000130 return m_namesSources.size();
akmhoque53353462014-04-22 08:43:45 -0500131 }
132
awlane6d7c37f2025-03-07 11:53:58 -0600133 const PrefixInfo&
134 getPrefixInfoForName(const ndn::Name& name) const;
135
Nick Gordonf14ec352017-07-24 16:09:58 -0500136 std::list<ndn::Name>
137 getNames() const;
Nick Gordon56d1fae2017-05-26 16:39:25 -0500138
awlane6d7c37f2025-03-07 11:53:58 -0600139 std::list<PrefixInfo>
140 getPrefixInfo() const;
141
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000142#ifdef WITH_TESTS
Nick Gordonf14ec352017-07-24 16:09:58 -0500143 /*! Returns the sources that this name has.
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000144 If the name does not exist, returns an empty container.
Nick Gordonf14ec352017-07-24 16:09:58 -0500145 */
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000146 std::set<std::string>
Nick Gordonf14ec352017-07-24 16:09:58 -0500147 getSources(const ndn::Name& name) const;
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000148#endif
Nick Gordonf14ec352017-07-24 16:09:58 -0500149
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800150 void
151 clear()
152 {
Junxiao Shi52f16642023-08-15 00:18:35 +0000153 m_namesSources.clear();
154 }
155
156private: // non-member operators
157 // NOTE: the following "hidden friend" operators are available via
158 // argument-dependent lookup only and must be defined inline.
159 // boost::equality_comparable provides != operators.
160
161 friend bool
162 operator==(const NamePrefixList& lhs, const NamePrefixList& rhs)
163 {
awlane6d7c37f2025-03-07 11:53:58 -0600164 return lhs.getPrefixInfo() == rhs.getPrefixInfo();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800165 }
166
awlane6d7c37f2025-03-07 11:53:58 -0600167 struct PrefixInfoSource
168 {
169 std::set<std::string> sources;
170 // Because NFD only readvertises each prefix once, this will be the first cost
171 // announced via NFD
172 PrefixInfo costObj;
173 };
174
Nick Gordonf14ec352017-07-24 16:09:58 -0500175private:
awlane6d7c37f2025-03-07 11:53:58 -0600176 std::map<ndn::Name, PrefixInfoSource> m_namesSources;
akmhoque53353462014-04-22 08:43:45 -0500177
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000178 friend std::ostream&
179 operator<<(std::ostream& os, const NamePrefixList& list);
180};
Nick Gordon56d1fae2017-05-26 16:39:25 -0500181
awlane6d7c37f2025-03-07 11:53:58 -0600182NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(PrefixInfo);
183
Nick Gordonfad8e252016-08-11 14:21:38 -0500184} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500185
Nick Gordon56d1fae2017-05-26 16:39:25 -0500186#endif // NLSR_NAME_PREFIX_LIST_HPP