link.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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 "ndn-cxx/link.hpp"
23 
24 namespace ndn {
25 
26 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Link>));
27 BOOST_CONCEPT_ASSERT((WireEncodable<Link>));
28 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Link>));
29 BOOST_CONCEPT_ASSERT((WireDecodable<Link>));
30 static_assert(std::is_base_of<Data::Error, Link::Error>::value,
31  "Link::Error should inherit from Data::Error");
32 
33 Link::Link() = default;
34 
35 Link::Link(const Block& wire)
36 {
37  this->wireDecode(wire);
38 }
39 
40 Link::Link(const Name& name, std::initializer_list<Name> delegations)
41  : Data(name)
42  , m_delegations(delegations)
43 {
44  encodeContent();
45 }
46 
47 void
48 Link::encodeContent()
49 {
51 
52  if (m_delegations.empty()) {
53  setContent(span<uint8_t>{});
54  }
55  else {
56  setContent(makeNestedBlock(tlv::Content, m_delegations.begin(), m_delegations.end()));
57  }
58 }
59 
60 void
61 Link::wireDecode(const Block& wire)
62 {
63  Data::wireDecode(wire);
64 
66  NDN_THROW(Error("Expecting ContentType Link, got " + to_string(getContentType())));
67  }
68 
69  // LinkContent = CONTENT-TYPE TLV-LENGTH 1*Name
70 
71  m_delegations.clear();
72  auto content = getContent();
73  content.parse();
74  for (const auto& del : content.elements()) {
75  if (del.type() == tlv::Name) {
76  m_delegations.emplace_back(del);
77  }
78  else if (tlv::isCriticalType(del.type())) {
79  NDN_THROW(Error("Unexpected TLV-TYPE " + to_string(del.type()) + " while decoding LinkContent"));
80  }
81  }
82 }
83 
84 void
85 Link::setDelegationList(std::vector<Name> delegations)
86 {
87  m_delegations = std::move(delegations);
88  encodeContent();
89 }
90 
91 bool
93 {
94  if (std::find(m_delegations.begin(), m_delegations.end(), name) != m_delegations.end()) {
95  return false;
96  }
97 
98  m_delegations.push_back(name);
99  encodeContent();
100  return true;
101 }
102 
103 bool
105 {
106  auto last = std::remove(m_delegations.begin(), m_delegations.end(), name);
107  if (last == m_delegations.end()) {
108  return false;
109  }
110 
111  m_delegations.erase(last, m_delegations.end());
112  encodeContent();
113  return true;
114 }
115 
116 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
Represents a Data packet.
Definition: data.hpp:39
void wireDecode(const Block &wire)
Decode from wire.
Definition: data.cpp:125
Data & setContent(const Block &block)
Set Content from a Block.
Definition: data.cpp:246
uint32_t getContentType() const
Definition: data.hpp:285
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:180
Data & setContentType(uint32_t type)
Definition: data.cpp:334
Represents an absolute name.
Definition: name.hpp:44
#define NDN_THROW(e)
Definition: exception.hpp:61
Block makeNestedBlock(uint32_t type, const U &value)
Create a TLV block containing a nested TLV element.
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:31
@ Name
Definition: tlv.hpp:71
@ Content
Definition: tlv.hpp:93
constexpr bool isCriticalType(uint32_t type) noexcept
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
Definition: tlv.hpp:162
@ ContentType_Link
another name that identifies the actual data content
Definition: tlv.hpp:146
Definition: data.cpp:25