blob: 279e0369f6c46a48d22b7ed90e71f381dc8fa2ea [file] [log] [blame]
Junxiao Shi688a6412017-06-22 10:12:07 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento88a0d812017-08-19 21:31:42 -04002/*
Davide Pesaventodb4da5e2018-06-15 11:37:52 -04003 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi688a6412017-06-22 10:12:07 +00004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "delegation-list.hpp"
23
24namespace ndn {
25
26BOOST_CONCEPT_ASSERT((boost::EqualityComparable<DelegationList>));
27BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<DelegationList>));
28BOOST_CONCEPT_ASSERT((WireDecodable<DelegationList>));
29
Junxiao Shi688a6412017-06-22 10:12:07 +000030DelegationList::Error::Error(const std::string& what, const std::exception& innerException)
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040031 : Error(what + ": "s + innerException.what())
Junxiao Shi688a6412017-06-22 10:12:07 +000032{
33}
34
35DelegationList::DelegationList()
36 : m_isSorted(true)
37{
38}
39
Junxiao Shid21abd32017-06-30 02:56:40 +000040DelegationList::DelegationList(std::initializer_list<Delegation> dels)
41 : m_isSorted(true)
42{
43 for (const Delegation& del : dels) {
44 this->insert(del, INS_REPLACE);
45 }
46}
47
Junxiao Shi688a6412017-06-22 10:12:07 +000048DelegationList::DelegationList(const Block& block, bool wantSort)
49{
50 this->wireDecode(block, wantSort);
51}
52
53bool
54DelegationList::isValidTlvType(uint32_t type)
55{
56 switch (type) {
57 case tlv::Content:
58 case tlv::ForwardingHint:
59 return true;
60 default:
61 return false;
62 }
63}
64
65template<encoding::Tag TAG>
66size_t
67DelegationList::wireEncode(EncodingImpl<TAG>& encoder, uint32_t type) const
68{
69 if (!isValidTlvType(type)) {
70 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid TLV-TYPE " + to_string(type) +
71 " when encoding DelegationList"));
72 }
73
74 if (this->size() == 0) {
75 BOOST_THROW_EXCEPTION(Error("Empty DelegationList"));
76 }
77
78 // LinkContent ::= (type) TLV-LENGTH
79 // Delegation+
80
81 // Delegation ::= LINK-DELEGATION-TYPE TLV-LENGTH
82 // Preference
83 // Name
84
85 // Preference ::= LINK-PREFERENCE-TYPE TLV-LENGTH
86 // nonNegativeInteger
87
88 size_t totalLen = 0;
89 for (auto i = m_dels.rbegin(); i != m_dels.rend(); ++i) {
90 size_t delLen = 0;
91 delLen += i->name.wireEncode(encoder);
92 delLen += prependNonNegativeIntegerBlock(encoder, tlv::LinkPreference, i->preference);
93 delLen += encoder.prependVarNumber(delLen);
94 delLen += encoder.prependVarNumber(tlv::LinkDelegation);
95 totalLen += delLen;
96 }
97 totalLen += encoder.prependVarNumber(totalLen);
98 totalLen += encoder.prependVarNumber(type);
99 return totalLen;
100}
101
102template size_t
Davide Pesavento88a0d812017-08-19 21:31:42 -0400103DelegationList::wireEncode<encoding::EncoderTag>(EncodingBuffer&, uint32_t) const;
Junxiao Shi688a6412017-06-22 10:12:07 +0000104
105template size_t
Davide Pesavento88a0d812017-08-19 21:31:42 -0400106DelegationList::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, uint32_t) const;
Junxiao Shi688a6412017-06-22 10:12:07 +0000107
108void
109DelegationList::wireDecode(const Block& block, bool wantSort)
110{
111 if (!isValidTlvType(block.type())) {
112 BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(block.type()) +
113 " when decoding DelegationList"));
114 }
115
116 m_isSorted = wantSort;
117 m_dels.clear();
118
119 block.parse();
120 for (const auto& del : block.elements()) {
121 if (del.type() != tlv::LinkDelegation) {
122 BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(del.type()) +
123 " when decoding Delegation"));
124 }
125 del.parse();
126
127 auto val = del.elements_begin();
128 if (val == del.elements_end() || val->type() != tlv::LinkPreference) {
129 BOOST_THROW_EXCEPTION(Error("Missing Preference field in Delegation"));
130 }
131 uint64_t preference = 0;
132 try {
133 preference = readNonNegativeInteger(*val);
134 }
135 catch (const tlv::Error& inner) {
136 BOOST_THROW_EXCEPTION(Error("Invalid Preference field in Delegation", inner));
137 }
138
139 ++val;
140 if (val == del.elements_end() || val->type() != tlv::Name) {
141 BOOST_THROW_EXCEPTION(Error("Missing Name field in Delegation"));
142 }
143 Name name;
144 try {
145 name.wireDecode(*val);
146 }
147 catch (const tlv::Error& inner) {
148 BOOST_THROW_EXCEPTION(Error("Invalid Name field in Delegation", inner));
149 }
150
151 this->insertImpl(preference, name);
152 }
153
154 if (this->size() == 0) {
155 BOOST_THROW_EXCEPTION(Error("Empty DelegationList"));
156 }
157}
158
159void
160DelegationList::sort()
161{
162 if (m_isSorted) {
163 return;
164 }
165
166 std::vector<Delegation> dels;
167 dels.swap(m_dels);
168
169 m_isSorted = true;
170 for (const Delegation& del : dels) {
171 this->insertImpl(del.preference, del.name);
172 }
173}
174
175bool
176DelegationList::insert(uint64_t preference, const Name& name,
177 InsertConflictResolution onConflict)
178{
179 switch (onConflict) {
180 case INS_REPLACE:
181 this->eraseImpl(nullopt, name);
182 this->insertImpl(preference, name);
183 return true;
184 case INS_APPEND:
185 this->insertImpl(preference, name);
186 return true;
187 case INS_SKIP:
188 if (!std::any_of(m_dels.begin(), m_dels.end(),
189 [name] (const Delegation& del) { return del.name == name; })) {
190 this->insertImpl(preference, name);
191 return true;
192 }
193 return false;
194 }
195 BOOST_ASSERT_MSG(false, "Unknown onConflict");
196 return false;
197}
198
199void
200DelegationList::insertImpl(uint64_t preference, const Name& name)
201{
202 if (!m_isSorted) {
203 m_dels.push_back({preference, name});
204 return;
205 }
206
207 Delegation del{preference, name};
208 auto pos = std::upper_bound(m_dels.begin(), m_dels.end(), del);
209 m_dels.insert(pos, del);
210}
211
212size_t
213DelegationList::eraseImpl(optional<uint64_t> preference, const Name& name)
214{
215 size_t nErased = 0;
216 for (auto i = m_dels.begin(); i != m_dels.end();) {
217 if ((!preference || i->preference == *preference) &&
218 i->name == name) {
219 ++nErased;
220 i = m_dels.erase(i);
221 }
222 else {
223 ++i;
224 }
225 }
226 return nErased;
227}
228
229bool
230operator==(const DelegationList& lhs, const DelegationList& rhs)
231{
232 return lhs.m_dels == rhs.m_dels;
233}
234
235std::ostream&
236operator<<(std::ostream& os, const DelegationList& dl)
237{
238 os << '[';
239 std::copy(dl.begin(), dl.end(), make_ostream_joiner(os, ','));
240 return os << ']';
241}
242
243} // namespace ndn