NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
delegation-list.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * 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 
24 namespace ndn {
25 
26 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<DelegationList>));
28 BOOST_CONCEPT_ASSERT((WireDecodable<DelegationList>));
29 
30 DelegationList::Error::Error(const std::string& what)
31  : tlv::Error(what)
32 {
33 }
34 
35 DelegationList::Error::Error(const std::string& what, const std::exception& innerException)
36  : Error(what + std::string(": ") + innerException.what())
37 {
38 }
39 
41  : m_isSorted(true)
42 {
43 }
44 
45 DelegationList::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 
53 DelegationList::DelegationList(const Block& block, bool wantSort)
54 {
55  this->wireDecode(block, wantSort);
56 }
57 
58 bool
59 DelegationList::isValidTlvType(uint32_t type)
60 {
61  switch (type) {
62  case tlv::Content:
64  return true;
65  default:
66  return false;
67  }
68 }
69 
70 template<encoding::Tag TAG>
71 size_t
72 DelegationList::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 
107 template size_t
108 DelegationList::wireEncode<encoding::EncoderTag>(EncodingBuffer&, uint32_t) const;
109 
110 template size_t
111 DelegationList::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, uint32_t) const;
112 
113 void
114 DelegationList::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 
164 void
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 
180 bool
181 DelegationList::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 
204 void
205 DelegationList::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 
217 size_t
218 DelegationList::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 
234 bool
235 operator==(const DelegationList& lhs, const DelegationList& rhs)
236 {
237  return lhs.m_dels == rhs.m_dels;
238 }
239 
240 std::ostream&
241 operator<<(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
constexpr nullopt_t nullopt
Copyright (c) 2011-2015 Regents of the University of California.
existing delegation(s) with the same name are replaced with the new delegation
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
new delegation is not inserted if an existing delegation has the same name
DelegationList()
construct an empty DelegationList
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:274
STL namespace.
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:335
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
represents a delegation
Definition: delegation.hpp:32
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
ostream_joiner< typename std::decay< DelimT >::type, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:347
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:60
void wireDecode(const Block &block, bool wantSort=true)
decode a DelegationList
const_iterator end() const noexcept
Error(const std::string &what)
size_t wireEncode(EncodingImpl< TAG > &encoder, uint32_t type=tlv::ForwardingHint) const
encode into wire format
size_t size() const noexcept
Represents an absolute name.
Definition: name.hpp:42
bool insert(uint64_t preference, const Name &name, InsertConflictResolution onConflict=INS_REPLACE)
insert Delegation
InsertConflictResolution
what to do when inserting a duplicate name
void sort()
sort the delegation list
represents a list of Delegations
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:265
std::string to_string(const V &v)
Definition: backports.hpp:84
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
multiple delegations with the same name are kept in the DelegationList
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
EncodingImpl< EstimatorTag > EncodingEstimator
const_iterator begin() const noexcept