Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 4 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 11 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 15 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 18 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 19 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "delete-param.hpp" |
| 23 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 24 | #include <boost/lexical_cast.hpp> |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace pib { |
| 28 | |
| 29 | static_assert(std::is_base_of<tlv::Error, DeleteParam::Error>::value, |
| 30 | "DeleteParam::Error must inherit from tlv::Error"); |
| 31 | |
| 32 | const std::string DeleteParam::VERB("delete"); |
| 33 | |
| 34 | DeleteParam::DeleteParam() |
| 35 | : m_targetType(TYPE_DEFAULT) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | DeleteParam::DeleteParam(const Name& name, pib::Type type) |
| 40 | : m_targetType(type) |
| 41 | , m_targetName(name) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | DeleteParam::DeleteParam(const Block& wire) |
| 46 | { |
| 47 | wireDecode(wire); |
| 48 | } |
| 49 | |
| 50 | template<bool T> |
| 51 | size_t |
| 52 | DeleteParam::wireEncode(EncodingImpl<T>& block) const |
| 53 | { |
| 54 | if (m_targetType != TYPE_ID && |
| 55 | m_targetType != TYPE_KEY && |
| 56 | m_targetType != TYPE_CERT && |
| 57 | m_targetType != TYPE_USER) |
| 58 | throw Error("DeleteParam::wireEncode: Wrong Type value: " + |
| 59 | boost::lexical_cast<std::string>(m_targetType)); |
| 60 | |
| 61 | size_t totalLength = 0; |
| 62 | |
| 63 | totalLength += m_targetName.wireEncode(block); |
| 64 | totalLength += prependNonNegativeIntegerBlock(block, tlv::pib::Type, m_targetType); |
| 65 | totalLength += block.prependVarNumber(totalLength); |
| 66 | totalLength += block.prependVarNumber(tlv::pib::DeleteParam); |
| 67 | |
| 68 | return totalLength; |
| 69 | } |
| 70 | |
| 71 | template size_t |
| 72 | DeleteParam::wireEncode<true>(EncodingImpl<true>& block) const; |
| 73 | |
| 74 | template size_t |
| 75 | DeleteParam::wireEncode<false>(EncodingImpl<false>& block) const; |
| 76 | |
| 77 | const Block& |
| 78 | DeleteParam::wireEncode() const |
| 79 | { |
| 80 | if (m_wire.hasWire()) |
| 81 | return m_wire; |
| 82 | |
| 83 | EncodingEstimator estimator; |
| 84 | size_t estimatedSize = wireEncode(estimator); |
| 85 | |
| 86 | EncodingBuffer buffer(estimatedSize, 0); |
| 87 | wireEncode(buffer); |
| 88 | |
| 89 | m_wire = buffer.block(); |
| 90 | return m_wire; |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | DeleteParam::wireDecode(const Block& wire) |
| 95 | { |
| 96 | if (!wire.hasWire()) { |
| 97 | throw Error("The supplied block does not contain wire format"); |
| 98 | } |
| 99 | |
| 100 | m_wire = wire; |
| 101 | m_wire.parse(); |
| 102 | |
| 103 | if (m_wire.type() != tlv::pib::DeleteParam) |
| 104 | throw Error("Unexpected TLV type when decoding DeleteParam"); |
| 105 | |
| 106 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 107 | |
| 108 | // the first block must be Type |
| 109 | if (it != m_wire.elements_end() && it->type() == tlv::pib::Type) { |
| 110 | m_targetType = static_cast<pib::Type>(readNonNegativeInteger(*it)); |
| 111 | it++; |
| 112 | } |
| 113 | else |
| 114 | throw Error("DeleteParam requires the first sub-TLV to be Type"); |
| 115 | |
| 116 | // the second block must be Name |
| 117 | if (it != m_wire.elements_end() && it->type() == tlv::Name) { |
| 118 | m_targetName.wireDecode(*it); |
| 119 | it++; |
| 120 | } |
| 121 | else |
| 122 | throw Error("DeleteParam requires the second sub-TLV to be Name"); |
| 123 | |
| 124 | if (it != m_wire.elements_end()) |
| 125 | throw Error("DeleteParam must not contain more than two sub-TLVs"); |
| 126 | } |
| 127 | |
| 128 | } // namespace pib |
| 129 | } // namespace ndn |