akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 2 | /* |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2025, The University of Memphis, |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 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 Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 20 | */ |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 21 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 22 | #include "name-prefix-list.hpp" |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 23 | #include "common.hpp" |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 24 | #include "tlv-nlsr.hpp" |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -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 | NamePrefixList::NamePrefixList() = default; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 30 | NamePrefixList::NamePrefixList(std::initializer_list<ndn::Name> names) |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 31 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 32 | for (const auto& name : names) { |
| 33 | insert(name); |
| 34 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | } |
| 36 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 37 | bool |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 38 | NamePrefixList::insert(const ndn::Name& name, const std::string& source, double cost) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 39 | { |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 40 | auto& soucePrefixInfo = m_namesSources[name]; |
| 41 | soucePrefixInfo.costObj = PrefixInfo(name, cost); |
| 42 | return soucePrefixInfo.sources.insert(source).second; |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | NamePrefixList::insert(const PrefixInfo& nameCost) |
| 47 | { |
| 48 | auto& soucePrefixInfo = m_namesSources[nameCost.getName()]; |
| 49 | soucePrefixInfo.costObj = nameCost; |
| 50 | return soucePrefixInfo.sources.insert("").second; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 51 | } |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 52 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 53 | bool |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 54 | NamePrefixList::erase(const ndn::Name& name, const std::string& source) |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 55 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 56 | auto it = m_namesSources.find(name); |
| 57 | if (it == m_namesSources.end()) { |
| 58 | return false; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 59 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 61 | bool isRemoved = it->second.sources.erase(source); |
| 62 | if (it->second.sources.empty()) { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 63 | m_namesSources.erase(it); |
| 64 | } |
| 65 | return isRemoved; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 66 | } |
| 67 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 68 | const PrefixInfo& |
| 69 | NamePrefixList::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 Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 76 | std::list<ndn::Name> |
| 77 | NamePrefixList::getNames() const |
| 78 | { |
| 79 | std::list<ndn::Name> names; |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 80 | for (const auto& [name, sources] : m_namesSources) { |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 81 | names.emplace_back(name); |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 82 | } |
| 83 | return names; |
| 84 | } |
| 85 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 86 | std::list<PrefixInfo> |
| 87 | NamePrefixList::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 Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 96 | #ifdef WITH_TESTS |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 97 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 98 | std::set<std::string> |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 99 | NamePrefixList::getSources(const ndn::Name& name) const |
| 100 | { |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 101 | if (auto it = m_namesSources.find(name); it != m_namesSources.end()) { |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 102 | return it->second.sources; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 103 | } |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 104 | return {}; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 109 | std::ostream& |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 110 | operator<<(std::ostream& os, const NamePrefixList& list) |
| 111 | { |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 112 | os << "Name prefix list: {\n"; |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 113 | for (const auto& [name, sources] : list.m_namesSources) { |
| 114 | os << name << "\nSources:\n"; |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 115 | for (const auto& source : sources.sources) { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 116 | os << " " << source << "\n"; |
| 117 | } |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 118 | } |
| 119 | os << "}" << std::endl; |
| 120 | return os; |
| 121 | } |
| 122 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame] | 123 | template<ndn::encoding::Tag TAG> |
| 124 | size_t |
| 125 | PrefixInfo::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 | |
| 139 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(PrefixInfo); |
| 140 | |
| 141 | const ndn::Block& |
| 142 | PrefixInfo::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 | |
| 159 | void |
| 160 | PrefixInfo::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 Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 189 | } // namespace nlsr |