Loading...
Searching...
No Matches
name-prefix-list.hpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2025, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
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/>.
20 */
21
22#ifndef NLSR_NAME_PREFIX_LIST_HPP
23#define NLSR_NAME_PREFIX_LIST_HPP
24
26
27#include <ndn-cxx/name.hpp>
28
29#include <boost/operators.hpp>
30
31#include <initializer_list>
32#include <list>
33#include <map>
34#include <set>
35#include <string>
36
37namespace nlsr {
38
39class 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
102class NamePrefixList : private boost::equality_comparable<NamePrefixList>
103{
104public:
106
107 explicit
108 NamePrefixList(std::initializer_list<ndn::Name> names);
109
114 bool
115 insert(const ndn::Name& name, const std::string& source = "", double cost = 0);
116
117 bool
118 insert(const PrefixInfo& nameCost);
119
124 bool
125 erase(const ndn::Name& name, const std::string& source = "");
126
127 size_t
128 size() const
129 {
130 return m_namesSources.size();
131 }
132
133 const PrefixInfo&
134 getPrefixInfoForName(const ndn::Name& name) const;
135
136 std::list<ndn::Name>
137 getNames() const;
138
139 std::list<PrefixInfo>
140 getPrefixInfo() const;
141
142#ifdef WITH_TESTS
146 std::set<std::string>
147 getSources(const ndn::Name& name) const;
148#endif
149
150 void
152 {
153 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
163 {
164 return lhs.getPrefixInfo() == rhs.getPrefixInfo();
165 }
166
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
175private:
176 std::map<ndn::Name, PrefixInfoSource> m_namesSources;
177
178 friend std::ostream&
179 operator<<(std::ostream& os, const NamePrefixList& list);
180};
181
183
184} // namespace nlsr
185
186#endif // NLSR_NAME_PREFIX_LIST_HPP
friend std::ostream & operator<<(std::ostream &os, const NamePrefixList &list)
std::list< PrefixInfo > getPrefixInfo() const
bool erase(const ndn::Name &name, const std::string &source="")
Deletes name and source combination.
const PrefixInfo & getPrefixInfoForName(const ndn::Name &name) const
friend bool operator==(const NamePrefixList &lhs, const NamePrefixList &rhs)
std::list< ndn::Name > getNames() const
bool insert(const ndn::Name &name, const std::string &source="", double cost=0)
Inserts name and source combination.
double getCost() const
const ndn::Block & wireEncode() const
const ndn::Name & getName() const
void wireDecode(const ndn::Block &wire)
PrefixInfo(const ndn::Block &block)
PrefixInfo()=default
friend bool operator==(const PrefixInfo &lhs, const PrefixInfo &rhs)
friend std::ostream & operator<<(std::ostream &os, const PrefixInfo &info)
PrefixInfo(const ndn::Name &name, double cost)
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)