blob: 8012306b029425f15ad8092de8502635342d4704 [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
Nick Gordonf14ec352017-07-24 16:09:58 -050022#include "name-prefix-list.hpp"
Nick Gordonf14ec352017-07-24 16:09:58 -050023#include "common.hpp"
awlane6d7c37f2025-03-07 11:53:58 -060024#include "tlv-nlsr.hpp"
Nick Gordonf14ec352017-07-24 16:09:58 -050025
akmhoque53353462014-04-22 08:43:45 -050026namespace nlsr {
27
dmcoomescf8d0ed2017-02-21 11:39:01 -060028NamePrefixList::NamePrefixList() = default;
akmhoque53353462014-04-22 08:43:45 -050029
Junxiao Shi931ca9f2023-08-15 02:59:46 +000030NamePrefixList::NamePrefixList(std::initializer_list<ndn::Name> names)
Nick Gordon96861ca2017-10-17 18:25:21 -050031{
Junxiao Shi52f16642023-08-15 00:18:35 +000032 for (const auto& name : names) {
33 insert(name);
34 }
akmhoque53353462014-04-22 08:43:45 -050035}
36
alvy297f4162015-03-03 17:15:33 -060037bool
awlane6d7c37f2025-03-07 11:53:58 -060038NamePrefixList::insert(const ndn::Name& name, const std::string& source, double cost)
akmhoque53353462014-04-22 08:43:45 -050039{
awlane6d7c37f2025-03-07 11:53:58 -060040 auto& soucePrefixInfo = m_namesSources[name];
41 soucePrefixInfo.costObj = PrefixInfo(name, cost);
42 return soucePrefixInfo.sources.insert(source).second;
43}
44
45bool
46NamePrefixList::insert(const PrefixInfo& nameCost)
47{
48 auto& soucePrefixInfo = m_namesSources[nameCost.getName()];
49 soucePrefixInfo.costObj = nameCost;
50 return soucePrefixInfo.sources.insert("").second;
Nick Gordonf14ec352017-07-24 16:09:58 -050051}
alvy297f4162015-03-03 17:15:33 -060052
Nick Gordonf14ec352017-07-24 16:09:58 -050053bool
Junxiao Shi52f16642023-08-15 00:18:35 +000054NamePrefixList::erase(const ndn::Name& name, const std::string& source)
Nick Gordonf14ec352017-07-24 16:09:58 -050055{
Junxiao Shi52f16642023-08-15 00:18:35 +000056 auto it = m_namesSources.find(name);
57 if (it == m_namesSources.end()) {
58 return false;
Nick Gordonf14ec352017-07-24 16:09:58 -050059 }
akmhoque53353462014-04-22 08:43:45 -050060
awlane6d7c37f2025-03-07 11:53:58 -060061 bool isRemoved = it->second.sources.erase(source);
62 if (it->second.sources.empty()) {
Junxiao Shi52f16642023-08-15 00:18:35 +000063 m_namesSources.erase(it);
64 }
65 return isRemoved;
Nick Gordonf14ec352017-07-24 16:09:58 -050066}
67
awlane6d7c37f2025-03-07 11:53:58 -060068const PrefixInfo&
69NamePrefixList::getPrefixInfoForName(const ndn::Name& name) const
70{
71 auto it = m_namesSources.find(name);
72 BOOST_ASSERT(it != m_namesSources.end());
73 return it->second.costObj;
74}
75
Nick Gordonf14ec352017-07-24 16:09:58 -050076std::list<ndn::Name>
77NamePrefixList::getNames() const
78{
79 std::list<ndn::Name> names;
Junxiao Shi52f16642023-08-15 00:18:35 +000080 for (const auto& [name, sources] : m_namesSources) {
awlane6d7c37f2025-03-07 11:53:58 -060081 names.emplace_back(name);
Nick Gordonf14ec352017-07-24 16:09:58 -050082 }
83 return names;
84}
85
awlane6d7c37f2025-03-07 11:53:58 -060086std::list<PrefixInfo>
87NamePrefixList::getPrefixInfo() const
88{
89 std::list<PrefixInfo> nameCosts;
90 for (const auto& [name, soucePrefixInfo] : m_namesSources) {
91 nameCosts.emplace_back(name, soucePrefixInfo.costObj.getCost());
92 }
93 return nameCosts;
94}
95
Junxiao Shi931ca9f2023-08-15 02:59:46 +000096#ifdef WITH_TESTS
Nick Gordonf14ec352017-07-24 16:09:58 -050097
Junxiao Shi931ca9f2023-08-15 02:59:46 +000098std::set<std::string>
Nick Gordonf14ec352017-07-24 16:09:58 -050099NamePrefixList::getSources(const ndn::Name& name) const
100{
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000101 if (auto it = m_namesSources.find(name); it != m_namesSources.end()) {
awlane6d7c37f2025-03-07 11:53:58 -0600102 return it->second.sources;
Nick Gordonf14ec352017-07-24 16:09:58 -0500103 }
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000104 return {};
akmhoque53353462014-04-22 08:43:45 -0500105}
106
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000107#endif
108
Nick Gordon56d1fae2017-05-26 16:39:25 -0500109std::ostream&
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400110operator<<(std::ostream& os, const NamePrefixList& list)
111{
Nick Gordon56d1fae2017-05-26 16:39:25 -0500112 os << "Name prefix list: {\n";
Junxiao Shi931ca9f2023-08-15 02:59:46 +0000113 for (const auto& [name, sources] : list.m_namesSources) {
114 os << name << "\nSources:\n";
awlane6d7c37f2025-03-07 11:53:58 -0600115 for (const auto& source : sources.sources) {
Nick Gordonf14ec352017-07-24 16:09:58 -0500116 os << " " << source << "\n";
117 }
Nick Gordon56d1fae2017-05-26 16:39:25 -0500118 }
119 os << "}" << std::endl;
120 return os;
121}
122
awlane6d7c37f2025-03-07 11:53:58 -0600123template<ndn::encoding::Tag TAG>
124size_t
125PrefixInfo::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
126{
127 size_t totalLength = 0;
128
129 totalLength += prependDoubleBlock(encoder, nlsr::tlv::Cost, m_prefixCost);
130
131 totalLength += m_prefixName.wireEncode(encoder);
132
133 totalLength += encoder.prependVarNumber(totalLength);
134 totalLength += encoder.prependVarNumber(nlsr::tlv::PrefixInfo);
135
136 return totalLength;
137}
138
139NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(PrefixInfo);
140
141const ndn::Block&
142PrefixInfo::wireEncode() const
143{
144 if (m_wire.hasWire()) {
145 return m_wire;
146 }
147
148 ndn::EncodingEstimator estimator;
149 size_t estimatedSize = wireEncode(estimator);
150
151 ndn::EncodingBuffer buffer(estimatedSize, 0);
152 wireEncode(buffer);
153
154 m_wire = buffer.block();
155
156 return m_wire;
157}
158
159void
160PrefixInfo::wireDecode(const ndn::Block& wire)
161{
162 m_wire = wire;
163
164 if (m_wire.type() != nlsr::tlv::PrefixInfo) {
165 NDN_THROW(Error("PrefixInfo", m_wire.type()));
166 }
167
168 m_wire.parse();
169
170 auto val = m_wire.elements_begin();
171
172 if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) {
173 m_prefixName.wireDecode(*val);
174 ++val;
175 }
176 else {
177 NDN_THROW(Error("Missing required Name field"));
178 }
179
180 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Cost) {
181 m_prefixCost = ndn::encoding::readDouble(*val);
182 ++val;
183 }
184 else {
185 NDN_THROW(Error("Missing required Cost field"));
186 }
187}
188
Nick Gordonfad8e252016-08-11 14:21:38 -0500189} // namespace nlsr