Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 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 "link.hpp" |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | |
| 26 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Link>)); |
| 27 | BOOST_CONCEPT_ASSERT((WireEncodable<Link>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 28 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Link>)); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 33 | Link::Link() = default; |
| 34 | |
| 35 | Link::Link(const Block& wire, bool wantSort) |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 36 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 37 | this->wireDecode(wire, wantSort); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 40 | Link::Link(const Name& name, std::initializer_list<Delegation> dels) |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 41 | : Data(name) |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 42 | , m_delList(dels) |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 43 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 44 | encodeContent(); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 47 | void |
| 48 | Link::encodeContent() |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 49 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 50 | 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 | } |
| 64 | |
| 65 | m_isDelSetDirty = true; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | Link::wireDecode(const Block& wire, bool wantSort) |
| 70 | { |
| 71 | Data::wireDecode(wire); |
| 72 | |
| 73 | if (getContentType() != tlv::ContentType_Link) { |
| 74 | BOOST_THROW_EXCEPTION(Error("Expected ContentType Link")); |
| 75 | } |
| 76 | |
| 77 | m_delList.wireDecode(getContent(), wantSort); |
| 78 | m_isDelSetDirty = true; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | Link::setDelegationList(const DelegationList& dels) |
| 83 | { |
| 84 | m_delList = dels; |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 85 | encodeContent(); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | Link::addDelegation(uint32_t preference, const Name& name) |
| 90 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 91 | m_delList.insert(preference, name, DelegationList::INS_REPLACE); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 92 | encodeContent(); |
| 93 | } |
| 94 | |
| 95 | bool |
| 96 | Link::removeDelegation(const Name& name) |
| 97 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 98 | size_t nErased = m_delList.erase(name); |
| 99 | if (nErased > 0) { |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 100 | encodeContent(); |
| 101 | } |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 102 | return nErased > 0; |
| 103 | } |
| 104 | |
| 105 | Link::PairInitializerListHelper::PairInitializerListHelper(std::initializer_list<std::pair<uint32_t, Name>> dels) |
| 106 | { |
| 107 | for (const auto& p : dels) { |
| 108 | m_delList.insert(p.first, p.second, DelegationList::INS_REPLACE); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | Link::Link(const Name& name, PairInitializerListHelper dels) |
| 113 | : Data(name) |
| 114 | , m_delList(std::move(dels.m_delList)) |
| 115 | { |
| 116 | encodeContent(); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | const Link::DelegationSet& |
| 120 | Link::getDelegations() const |
| 121 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 122 | if (m_isDelSetDirty) { |
| 123 | m_delSet.clear(); |
| 124 | for (const auto& del : m_delList) { |
| 125 | m_delSet.emplace(static_cast<uint32_t>(del.preference), del.name); |
| 126 | } |
| 127 | m_isDelSetDirty = false; |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 128 | } |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 129 | return m_delSet; |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 132 | Link::DelegationTuple |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 133 | Link::getDelegationFromWire(const Block& block, size_t index) |
| 134 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 135 | Delegation del = Link(block, false).getDelegationList().at(index); |
| 136 | return std::make_tuple(static_cast<uint32_t>(del.preference), del.name); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | ssize_t |
| 140 | Link::findDelegationFromWire(const Block& block, const Name& delegationName) |
| 141 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 142 | DelegationList dels = Link(block, false).getDelegationList(); |
| 143 | auto i = std::find_if(dels.begin(), dels.end(), |
| 144 | [delegationName] (const Delegation& del) { return del.name == delegationName; }); |
| 145 | return i == dels.end() ? -1 : std::distance(dels.begin(), i); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | ssize_t |
| 149 | Link::countDelegationsFromWire(const Block& block) |
| 150 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 151 | return Link(block, false).getDelegationList().size(); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | } // namespace ndn |