Junxiao Shi | 688a641 | 2017-06-22 10:12:07 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 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 "delegation-list.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | |
| 26 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<DelegationList>)); |
| 27 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<DelegationList>)); |
| 28 | BOOST_CONCEPT_ASSERT((WireDecodable<DelegationList>)); |
| 29 | |
| 30 | DelegationList::Error::Error(const std::string& what) |
| 31 | : tlv::Error(what) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | DelegationList::Error::Error(const std::string& what, const std::exception& innerException) |
| 36 | : Error(what + std::string(": ") + innerException.what()) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | DelegationList::DelegationList() |
| 41 | : m_isSorted(true) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | DelegationList::DelegationList(const Block& block, bool wantSort) |
| 46 | { |
| 47 | this->wireDecode(block, wantSort); |
| 48 | } |
| 49 | |
| 50 | bool |
| 51 | DelegationList::isValidTlvType(uint32_t type) |
| 52 | { |
| 53 | switch (type) { |
| 54 | case tlv::Content: |
| 55 | case tlv::ForwardingHint: |
| 56 | return true; |
| 57 | default: |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | template<encoding::Tag TAG> |
| 63 | size_t |
| 64 | DelegationList::wireEncode(EncodingImpl<TAG>& encoder, uint32_t type) const |
| 65 | { |
| 66 | if (!isValidTlvType(type)) { |
| 67 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid TLV-TYPE " + to_string(type) + |
| 68 | " when encoding DelegationList")); |
| 69 | } |
| 70 | |
| 71 | if (this->size() == 0) { |
| 72 | BOOST_THROW_EXCEPTION(Error("Empty DelegationList")); |
| 73 | } |
| 74 | |
| 75 | // LinkContent ::= (type) TLV-LENGTH |
| 76 | // Delegation+ |
| 77 | |
| 78 | // Delegation ::= LINK-DELEGATION-TYPE TLV-LENGTH |
| 79 | // Preference |
| 80 | // Name |
| 81 | |
| 82 | // Preference ::= LINK-PREFERENCE-TYPE TLV-LENGTH |
| 83 | // nonNegativeInteger |
| 84 | |
| 85 | size_t totalLen = 0; |
| 86 | for (auto i = m_dels.rbegin(); i != m_dels.rend(); ++i) { |
| 87 | size_t delLen = 0; |
| 88 | delLen += i->name.wireEncode(encoder); |
| 89 | delLen += prependNonNegativeIntegerBlock(encoder, tlv::LinkPreference, i->preference); |
| 90 | delLen += encoder.prependVarNumber(delLen); |
| 91 | delLen += encoder.prependVarNumber(tlv::LinkDelegation); |
| 92 | totalLen += delLen; |
| 93 | } |
| 94 | totalLen += encoder.prependVarNumber(totalLen); |
| 95 | totalLen += encoder.prependVarNumber(type); |
| 96 | return totalLen; |
| 97 | } |
| 98 | |
| 99 | template size_t |
| 100 | DelegationList::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&, uint32_t) const; |
| 101 | |
| 102 | template size_t |
| 103 | DelegationList::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&, uint32_t) const; |
| 104 | |
| 105 | void |
| 106 | DelegationList::wireDecode(const Block& block, bool wantSort) |
| 107 | { |
| 108 | if (!isValidTlvType(block.type())) { |
| 109 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(block.type()) + |
| 110 | " when decoding DelegationList")); |
| 111 | } |
| 112 | |
| 113 | m_isSorted = wantSort; |
| 114 | m_dels.clear(); |
| 115 | |
| 116 | block.parse(); |
| 117 | for (const auto& del : block.elements()) { |
| 118 | if (del.type() != tlv::LinkDelegation) { |
| 119 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(del.type()) + |
| 120 | " when decoding Delegation")); |
| 121 | } |
| 122 | del.parse(); |
| 123 | |
| 124 | auto val = del.elements_begin(); |
| 125 | if (val == del.elements_end() || val->type() != tlv::LinkPreference) { |
| 126 | BOOST_THROW_EXCEPTION(Error("Missing Preference field in Delegation")); |
| 127 | } |
| 128 | uint64_t preference = 0; |
| 129 | try { |
| 130 | preference = readNonNegativeInteger(*val); |
| 131 | } |
| 132 | catch (const tlv::Error& inner) { |
| 133 | BOOST_THROW_EXCEPTION(Error("Invalid Preference field in Delegation", inner)); |
| 134 | } |
| 135 | |
| 136 | ++val; |
| 137 | if (val == del.elements_end() || val->type() != tlv::Name) { |
| 138 | BOOST_THROW_EXCEPTION(Error("Missing Name field in Delegation")); |
| 139 | } |
| 140 | Name name; |
| 141 | try { |
| 142 | name.wireDecode(*val); |
| 143 | } |
| 144 | catch (const tlv::Error& inner) { |
| 145 | BOOST_THROW_EXCEPTION(Error("Invalid Name field in Delegation", inner)); |
| 146 | } |
| 147 | |
| 148 | this->insertImpl(preference, name); |
| 149 | } |
| 150 | |
| 151 | if (this->size() == 0) { |
| 152 | BOOST_THROW_EXCEPTION(Error("Empty DelegationList")); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | DelegationList::sort() |
| 158 | { |
| 159 | if (m_isSorted) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | std::vector<Delegation> dels; |
| 164 | dels.swap(m_dels); |
| 165 | |
| 166 | m_isSorted = true; |
| 167 | for (const Delegation& del : dels) { |
| 168 | this->insertImpl(del.preference, del.name); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | bool |
| 173 | DelegationList::insert(uint64_t preference, const Name& name, |
| 174 | InsertConflictResolution onConflict) |
| 175 | { |
| 176 | switch (onConflict) { |
| 177 | case INS_REPLACE: |
| 178 | this->eraseImpl(nullopt, name); |
| 179 | this->insertImpl(preference, name); |
| 180 | return true; |
| 181 | case INS_APPEND: |
| 182 | this->insertImpl(preference, name); |
| 183 | return true; |
| 184 | case INS_SKIP: |
| 185 | if (!std::any_of(m_dels.begin(), m_dels.end(), |
| 186 | [name] (const Delegation& del) { return del.name == name; })) { |
| 187 | this->insertImpl(preference, name); |
| 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | BOOST_ASSERT_MSG(false, "Unknown onConflict"); |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | DelegationList::insertImpl(uint64_t preference, const Name& name) |
| 198 | { |
| 199 | if (!m_isSorted) { |
| 200 | m_dels.push_back({preference, name}); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | Delegation del{preference, name}; |
| 205 | auto pos = std::upper_bound(m_dels.begin(), m_dels.end(), del); |
| 206 | m_dels.insert(pos, del); |
| 207 | } |
| 208 | |
| 209 | size_t |
| 210 | DelegationList::eraseImpl(optional<uint64_t> preference, const Name& name) |
| 211 | { |
| 212 | size_t nErased = 0; |
| 213 | for (auto i = m_dels.begin(); i != m_dels.end();) { |
| 214 | if ((!preference || i->preference == *preference) && |
| 215 | i->name == name) { |
| 216 | ++nErased; |
| 217 | i = m_dels.erase(i); |
| 218 | } |
| 219 | else { |
| 220 | ++i; |
| 221 | } |
| 222 | } |
| 223 | return nErased; |
| 224 | } |
| 225 | |
| 226 | bool |
| 227 | operator==(const DelegationList& lhs, const DelegationList& rhs) |
| 228 | { |
| 229 | return lhs.m_dels == rhs.m_dels; |
| 230 | } |
| 231 | |
| 232 | std::ostream& |
| 233 | operator<<(std::ostream& os, const DelegationList& dl) |
| 234 | { |
| 235 | os << '['; |
| 236 | std::copy(dl.begin(), dl.end(), make_ostream_joiner(os, ',')); |
| 237 | return os << ']'; |
| 238 | } |
| 239 | |
| 240 | } // namespace ndn |