blob: e21d100ae51e66c12babe02194529b8978e2d12a [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/*
Junxiao Shi688a6412017-06-22 10:12:07 +00003 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
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
30DelegationList::Error::Error(const std::string& what)
31 : tlv::Error(what)
32{
33}
34
35DelegationList::Error::Error(const std::string& what, const std::exception& innerException)
36 : Error(what + std::string(": ") + innerException.what())
37{
38}
39
40DelegationList::DelegationList()
41 : m_isSorted(true)
42{
43}
44
Junxiao Shid21abd32017-06-30 02:56:40 +000045DelegationList::DelegationList(std::initializer_list<Delegation> dels)
46 : m_isSorted(true)
47{
48 for (const Delegation& del : dels) {
49 this->insert(del, INS_REPLACE);
50 }
51}
52
Junxiao Shi688a6412017-06-22 10:12:07 +000053DelegationList::DelegationList(const Block& block, bool wantSort)
54{
55 this->wireDecode(block, wantSort);
56}
57
58bool
59DelegationList::isValidTlvType(uint32_t type)
60{
61 switch (type) {
62 case tlv::Content:
63 case tlv::ForwardingHint:
64 return true;
65 default:
66 return false;
67 }
68}
69
70template<encoding::Tag TAG>
71size_t
72DelegationList::wireEncode(EncodingImpl<TAG>& encoder, uint32_t type) const
73{
74 if (!isValidTlvType(type)) {
75 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid TLV-TYPE " + to_string(type) +
76 " when encoding DelegationList"));
77 }
78
79 if (this->size() == 0) {
80 BOOST_THROW_EXCEPTION(Error("Empty DelegationList"));
81 }
82
83 // LinkContent ::= (type) TLV-LENGTH
84 // Delegation+
85
86 // Delegation ::= LINK-DELEGATION-TYPE TLV-LENGTH
87 // Preference
88 // Name
89
90 // Preference ::= LINK-PREFERENCE-TYPE TLV-LENGTH
91 // nonNegativeInteger
92
93 size_t totalLen = 0;
94 for (auto i = m_dels.rbegin(); i != m_dels.rend(); ++i) {
95 size_t delLen = 0;
96 delLen += i->name.wireEncode(encoder);
97 delLen += prependNonNegativeIntegerBlock(encoder, tlv::LinkPreference, i->preference);
98 delLen += encoder.prependVarNumber(delLen);
99 delLen += encoder.prependVarNumber(tlv::LinkDelegation);
100 totalLen += delLen;
101 }
102 totalLen += encoder.prependVarNumber(totalLen);
103 totalLen += encoder.prependVarNumber(type);
104 return totalLen;
105}
106
107template size_t
Davide Pesavento88a0d812017-08-19 21:31:42 -0400108DelegationList::wireEncode<encoding::EncoderTag>(EncodingBuffer&, uint32_t) const;
Junxiao Shi688a6412017-06-22 10:12:07 +0000109
110template size_t
Davide Pesavento88a0d812017-08-19 21:31:42 -0400111DelegationList::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, uint32_t) const;
Junxiao Shi688a6412017-06-22 10:12:07 +0000112
113void
114DelegationList::wireDecode(const Block& block, bool wantSort)
115{
116 if (!isValidTlvType(block.type())) {
117 BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(block.type()) +
118 " when decoding DelegationList"));
119 }
120
121 m_isSorted = wantSort;
122 m_dels.clear();
123
124 block.parse();
125 for (const auto& del : block.elements()) {
126 if (del.type() != tlv::LinkDelegation) {
127 BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(del.type()) +
128 " when decoding Delegation"));
129 }
130 del.parse();
131
132 auto val = del.elements_begin();
133 if (val == del.elements_end() || val->type() != tlv::LinkPreference) {
134 BOOST_THROW_EXCEPTION(Error("Missing Preference field in Delegation"));
135 }
136 uint64_t preference = 0;
137 try {
138 preference = readNonNegativeInteger(*val);
139 }
140 catch (const tlv::Error& inner) {
141 BOOST_THROW_EXCEPTION(Error("Invalid Preference field in Delegation", inner));
142 }
143
144 ++val;
145 if (val == del.elements_end() || val->type() != tlv::Name) {
146 BOOST_THROW_EXCEPTION(Error("Missing Name field in Delegation"));
147 }
148 Name name;
149 try {
150 name.wireDecode(*val);
151 }
152 catch (const tlv::Error& inner) {
153 BOOST_THROW_EXCEPTION(Error("Invalid Name field in Delegation", inner));
154 }
155
156 this->insertImpl(preference, name);
157 }
158
159 if (this->size() == 0) {
160 BOOST_THROW_EXCEPTION(Error("Empty DelegationList"));
161 }
162}
163
164void
165DelegationList::sort()
166{
167 if (m_isSorted) {
168 return;
169 }
170
171 std::vector<Delegation> dels;
172 dels.swap(m_dels);
173
174 m_isSorted = true;
175 for (const Delegation& del : dels) {
176 this->insertImpl(del.preference, del.name);
177 }
178}
179
180bool
181DelegationList::insert(uint64_t preference, const Name& name,
182 InsertConflictResolution onConflict)
183{
184 switch (onConflict) {
185 case INS_REPLACE:
186 this->eraseImpl(nullopt, name);
187 this->insertImpl(preference, name);
188 return true;
189 case INS_APPEND:
190 this->insertImpl(preference, name);
191 return true;
192 case INS_SKIP:
193 if (!std::any_of(m_dels.begin(), m_dels.end(),
194 [name] (const Delegation& del) { return del.name == name; })) {
195 this->insertImpl(preference, name);
196 return true;
197 }
198 return false;
199 }
200 BOOST_ASSERT_MSG(false, "Unknown onConflict");
201 return false;
202}
203
204void
205DelegationList::insertImpl(uint64_t preference, const Name& name)
206{
207 if (!m_isSorted) {
208 m_dels.push_back({preference, name});
209 return;
210 }
211
212 Delegation del{preference, name};
213 auto pos = std::upper_bound(m_dels.begin(), m_dels.end(), del);
214 m_dels.insert(pos, del);
215}
216
217size_t
218DelegationList::eraseImpl(optional<uint64_t> preference, const Name& name)
219{
220 size_t nErased = 0;
221 for (auto i = m_dels.begin(); i != m_dels.end();) {
222 if ((!preference || i->preference == *preference) &&
223 i->name == name) {
224 ++nErased;
225 i = m_dels.erase(i);
226 }
227 else {
228 ++i;
229 }
230 }
231 return nErased;
232}
233
234bool
235operator==(const DelegationList& lhs, const DelegationList& rhs)
236{
237 return lhs.m_dels == rhs.m_dels;
238}
239
240std::ostream&
241operator<<(std::ostream& os, const DelegationList& dl)
242{
243 os << '[';
244 std::copy(dl.begin(), dl.end(), make_ostream_joiner(os, ','));
245 return os << ']';
246}
247
248} // namespace ndn