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 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 22 | #ifndef NLSR_NAME_PREFIX_LIST_HPP |
| 23 | #define NLSR_NAME_PREFIX_LIST_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 25 | #include "test-access-control.hpp" |
| 26 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 27 | #include <ndn-cxx/name.hpp> |
| 28 | |
| 29 | #include <boost/operators.hpp> |
| 30 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 31 | #include <initializer_list> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | #include <list> |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 33 | #include <map> |
| 34 | #include <set> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | #include <string> |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 36 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | namespace nlsr { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 38 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame^] | 39 | class PrefixInfo : private boost::equality_comparable<PrefixInfo> |
| 40 | { |
| 41 | public: |
| 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 | |
| 81 | private: |
| 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 | |
| 95 | private: |
| 96 | ndn::Name m_prefixName; |
| 97 | double m_prefixCost; |
| 98 | |
| 99 | mutable ndn::Block m_wire; |
| 100 | }; |
| 101 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 102 | class NamePrefixList : private boost::equality_comparable<NamePrefixList> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 103 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 104 | public: |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 105 | NamePrefixList(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 106 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 107 | explicit |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 108 | NamePrefixList(std::initializer_list<ndn::Name> names); |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 109 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 110 | /*! \brief Inserts name and source combination. |
| 111 | \retval true Name and source combination is inserted. |
| 112 | \retval false Name and source combination already exists. |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 113 | */ |
| 114 | bool |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame^] | 115 | insert(const ndn::Name& name, const std::string& source = "", double cost = 0); |
| 116 | |
| 117 | bool |
| 118 | insert(const PrefixInfo& nameCost); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 119 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 120 | /*! \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. |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 123 | */ |
| 124 | bool |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 125 | erase(const ndn::Name& name, const std::string& source = ""); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 126 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 127 | size_t |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 128 | size() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 130 | return m_namesSources.size(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | } |
| 132 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame^] | 133 | const PrefixInfo& |
| 134 | getPrefixInfoForName(const ndn::Name& name) const; |
| 135 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 136 | std::list<ndn::Name> |
| 137 | getNames() const; |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 138 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame^] | 139 | std::list<PrefixInfo> |
| 140 | getPrefixInfo() const; |
| 141 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 142 | #ifdef WITH_TESTS |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 143 | /*! Returns the sources that this name has. |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 144 | If the name does not exist, returns an empty container. |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 145 | */ |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 146 | std::set<std::string> |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 147 | getSources(const ndn::Name& name) const; |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 148 | #endif |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 149 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 150 | void |
| 151 | clear() |
| 152 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 153 | m_namesSources.clear(); |
| 154 | } |
| 155 | |
| 156 | private: // 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 | { |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame^] | 164 | return lhs.getPrefixInfo() == rhs.getPrefixInfo(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 165 | } |
| 166 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame^] | 167 | 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 Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 175 | private: |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame^] | 176 | std::map<ndn::Name, PrefixInfoSource> m_namesSources; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 177 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 178 | friend std::ostream& |
| 179 | operator<<(std::ostream& os, const NamePrefixList& list); |
| 180 | }; |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 181 | |
awlane | 6d7c37f | 2025-03-07 11:53:58 -0600 | [diff] [blame^] | 182 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(PrefixInfo); |
| 183 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 184 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 185 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 186 | #endif // NLSR_NAME_PREFIX_LIST_HPP |