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-2018 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 
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, const std::exception& innerException)
31  : Error(what + ": "s + innerException.what())
32 {
33 }
34 
36  : m_isSorted(true)
37 {
38 }
39 
40 DelegationList::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 
48 DelegationList::DelegationList(const Block& block, bool wantSort)
49 {
50  this->wireDecode(block, wantSort);
51 }
52 
53 bool
54 DelegationList::isValidTlvType(uint32_t type)
55 {
56  switch (type) {
57  case tlv::Content:
59  return true;
60  default:
61  return false;
62  }
63 }
64 
65 template<encoding::Tag TAG>
66 size_t
67 DelegationList::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 
102 template size_t
103 DelegationList::wireEncode<encoding::EncoderTag>(EncodingBuffer&, uint32_t) const;
104 
105 template size_t
106 DelegationList::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, uint32_t) const;
107 
108 void
109 DelegationList::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 
159 void
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 
175 bool
176 DelegationList::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 
199 void
200 DelegationList::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 
212 size_t
213 DelegationList::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 
229 bool
230 operator==(const DelegationList& lhs, const DelegationList& rhs)
231 {
232  return lhs.m_dels == rhs.m_dels;
233 }
234 
235 std::ostream&
236 operator<<(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
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:322
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:333
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Error(const std::string &what, const std::exception &innerException)
represents a delegation
Definition: delegation.hpp:32
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:361
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
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
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:43
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:313
std::string to_string(const V &v)
Definition: backports.hpp:67
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
const nullopt_t nullopt((nullopt_t::init()))
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:249
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
EncodingImpl< EstimatorTag > EncodingEstimator
const_iterator begin() const noexcept