ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2023 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#include <algorithm>
25
26namespace ndn {
27
28Link::Link() = default;
29
30Link::Link(const Block& wire)
31{
32 this->wireDecode(wire);
33}
34
35Link::Link(const Name& name, std::initializer_list<Name> delegations)
36 : Data(name)
37 , m_delegations(delegations)
38{
39 encodeContent();
40}
41
42void
43Link::encodeContent()
44{
46 setContent(makeNestedBlock(tlv::Content, m_delegations.begin(), m_delegations.end()));
47}
48
49void
51{
52 Data::wireDecode(wire);
53
55 NDN_THROW(Error("Expecting ContentType Link, got " + to_string(getContentType())));
56 }
57
58 // LinkContent = CONTENT-TYPE TLV-LENGTH 1*Name
59
60 m_delegations.clear();
61 auto content = getContent();
62 content.parse();
63 for (const auto& del : content.elements()) {
64 if (del.type() == tlv::Name) {
65 m_delegations.emplace_back(del);
66 }
67 else if (tlv::isCriticalType(del.type())) {
68 NDN_THROW(Error("Unexpected TLV-TYPE " + to_string(del.type()) + " while decoding LinkContent"));
69 }
70 }
71}
72
73void
74Link::setDelegationList(std::vector<Name> delegations)
75{
76 m_delegations = std::move(delegations);
77 encodeContent();
78}
79
80bool
82{
83 if (std::find(m_delegations.begin(), m_delegations.end(), name) != m_delegations.end()) {
84 return false;
85 }
86
87 m_delegations.push_back(name);
88 encodeContent();
89 return true;
90}
91
92bool
94{
95 auto last = std::remove(m_delegations.begin(), m_delegations.end(), name);
96 if (last == m_delegations.end()) {
97 return false;
98 }
99
100 m_delegations.erase(last, m_delegations.end());
101 encodeContent();
102 return true;
103}
104
105} // 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:118
Data & setContent(const Block &block)
Set Content from a Block.
Definition data.cpp:239
const Block & getContent() const noexcept
Get the Content element.
Definition data.hpp:188
uint32_t getContentType() const noexcept
Return the value of ContentType.
Definition data.hpp:312
Data & setContentType(uint32_t type)
Set the ContentType.
Definition data.cpp:337
Represents an absolute name.
Definition name.hpp:45
#define NDN_THROW(e)
Definition exception.hpp:56
@ 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