blob: 1276904bb311b4c907bdcc6556c6208e6d685e40 [file] [log] [blame]
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shid9153ce2017-08-08 04:48:46 +00002/*
Davide Pesaventoe1789892017-02-26 15:50:52 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -07004 *
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 "link.hpp"
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070023
24namespace ndn {
25
26BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Link>));
27BOOST_CONCEPT_ASSERT((WireEncodable<Link>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070028BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Link>));
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070029BOOST_CONCEPT_ASSERT((WireDecodable<Link>));
30static_assert(std::is_base_of<Data::Error, Link::Error>::value,
31 "Link::Error should inherit from Data::Error");
32
Junxiao Shid21abd32017-06-30 02:56:40 +000033Link::Link() = default;
34
35Link::Link(const Block& wire, bool wantSort)
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070036{
Junxiao Shid21abd32017-06-30 02:56:40 +000037 this->wireDecode(wire, wantSort);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070038}
39
Junxiao Shid21abd32017-06-30 02:56:40 +000040Link::Link(const Name& name, std::initializer_list<Delegation> dels)
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070041 : Data(name)
Junxiao Shid21abd32017-06-30 02:56:40 +000042 , m_delList(dels)
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070043{
Junxiao Shid21abd32017-06-30 02:56:40 +000044 encodeContent();
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070045}
46
Junxiao Shid21abd32017-06-30 02:56:40 +000047void
48Link::encodeContent()
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070049{
Junxiao Shid21abd32017-06-30 02:56:40 +000050 setContentType(tlv::ContentType_Link);
51
52 if (m_delList.size() > 0) {
53 EncodingEstimator estimator;
54 size_t estimatedSize = m_delList.wireEncode(estimator, tlv::Content);
55
56 EncodingBuffer buffer(estimatedSize, 0);
57 m_delList.wireEncode(buffer, tlv::Content);
58
59 setContent(buffer.block());
60 }
61 else {
62 setContent(nullptr, 0);
63 }
Junxiao Shid21abd32017-06-30 02:56:40 +000064}
65
66void
67Link::wireDecode(const Block& wire, bool wantSort)
68{
69 Data::wireDecode(wire);
70
71 if (getContentType() != tlv::ContentType_Link) {
72 BOOST_THROW_EXCEPTION(Error("Expected ContentType Link"));
73 }
74
75 m_delList.wireDecode(getContent(), wantSort);
Junxiao Shid21abd32017-06-30 02:56:40 +000076}
77
78void
79Link::setDelegationList(const DelegationList& dels)
80{
81 m_delList = dels;
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070082 encodeContent();
83}
84
85void
86Link::addDelegation(uint32_t preference, const Name& name)
87{
Junxiao Shid21abd32017-06-30 02:56:40 +000088 m_delList.insert(preference, name, DelegationList::INS_REPLACE);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070089 encodeContent();
90}
91
92bool
93Link::removeDelegation(const Name& name)
94{
Junxiao Shid21abd32017-06-30 02:56:40 +000095 size_t nErased = m_delList.erase(name);
96 if (nErased > 0) {
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070097 encodeContent();
98 }
Junxiao Shid21abd32017-06-30 02:56:40 +000099 return nErased > 0;
100}
101
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700102} // namespace ndn