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 "list-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, ListParam::Error>::value, |
| 30 | "ListParam::Error must inherit from tlv::Error"); |
| 31 | |
| 32 | const std::string ListParam::VERB("list"); |
| 33 | |
| 34 | ListParam::ListParam() |
| 35 | : m_originType(TYPE_USER) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | ListParam::ListParam(const pib::Type originType, const Name& originName) |
| 40 | : m_originType(originType) |
| 41 | , m_originName(originName) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | ListParam::ListParam(const Block& wire) |
| 46 | { |
| 47 | wireDecode(wire); |
| 48 | } |
| 49 | |
| 50 | const Name& |
| 51 | ListParam::getOriginName() const |
| 52 | { |
| 53 | if (m_originType == TYPE_ID || m_originType == TYPE_KEY || m_originType == TYPE_CERT) |
| 54 | return m_originName; |
| 55 | else |
| 56 | throw Error("ListParam::getOriginName: origin name does not exist"); |
| 57 | } |
| 58 | |
| 59 | template<bool T> |
| 60 | size_t |
| 61 | ListParam::wireEncode(EncodingImpl<T>& block) const |
| 62 | { |
| 63 | size_t totalLength = 0; |
| 64 | |
| 65 | switch (m_originType) { |
| 66 | case TYPE_ID: |
| 67 | case TYPE_KEY: |
| 68 | case TYPE_CERT: |
| 69 | { |
| 70 | totalLength += m_originName.wireEncode(block); |
| 71 | break; |
| 72 | } |
| 73 | case TYPE_USER: |
| 74 | break; |
| 75 | default: |
| 76 | throw Error("ListParam::wireEncode: unsupported PibType: " + |
| 77 | boost::lexical_cast<std::string>(m_originType)); |
| 78 | } |
| 79 | |
| 80 | totalLength += prependNonNegativeIntegerBlock(block, tlv::pib::Type, m_originType); |
| 81 | totalLength += block.prependVarNumber(totalLength); |
| 82 | totalLength += block.prependVarNumber(tlv::pib::ListParam); |
| 83 | |
| 84 | return totalLength; |
| 85 | } |
| 86 | |
| 87 | template size_t |
| 88 | ListParam::wireEncode<true>(EncodingImpl<true>& block) const; |
| 89 | |
| 90 | template size_t |
| 91 | ListParam::wireEncode<false>(EncodingImpl<false>& block) const; |
| 92 | |
| 93 | const Block& |
| 94 | ListParam::wireEncode() const |
| 95 | { |
| 96 | if (m_wire.hasWire()) |
| 97 | return m_wire; |
| 98 | |
| 99 | EncodingEstimator estimator; |
| 100 | size_t estimatedSize = wireEncode(estimator); |
| 101 | |
| 102 | EncodingBuffer buffer(estimatedSize, 0); |
| 103 | wireEncode(buffer); |
| 104 | |
| 105 | m_wire = buffer.block(); |
| 106 | return m_wire; |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | ListParam::wireDecode(const Block& wire) |
| 111 | { |
| 112 | if (!wire.hasWire()) { |
| 113 | throw Error("The supplied block does not contain wire format"); |
| 114 | } |
| 115 | |
| 116 | m_wire = wire; |
| 117 | m_wire.parse(); |
| 118 | |
| 119 | if (m_wire.type() != tlv::pib::ListParam) |
| 120 | throw Error("Unexpected TLV type when decoding ListParam"); |
| 121 | |
| 122 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 123 | |
| 124 | // the first block must be PibType |
| 125 | if (it != m_wire.elements_end() && it->type() == tlv::pib::Type) { |
| 126 | m_originType = static_cast<pib::Type>(readNonNegativeInteger(*it)); |
| 127 | it++; |
| 128 | } |
| 129 | else |
| 130 | throw Error("ListParam requires the first sub-TLV to be PibType"); |
| 131 | |
| 132 | switch (m_originType) { |
| 133 | case TYPE_ID: |
| 134 | case TYPE_KEY: |
| 135 | case TYPE_CERT: |
| 136 | { |
| 137 | if (it != m_wire.elements_end()) { |
| 138 | // the second block, if exists, must be Name |
| 139 | m_originName.wireDecode(*it); |
| 140 | return; |
| 141 | } |
| 142 | else { |
| 143 | throw Error("ListParam requires the second sub-TLV to be Name"); |
| 144 | } |
| 145 | } |
| 146 | case TYPE_USER: |
| 147 | return; |
| 148 | default: |
| 149 | throw Error("ListParam::wireDecode: unsupported PibType: " + |
| 150 | boost::lexical_cast<std::string>(m_originType)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } // namespace pib |
| 155 | } // namespace ndn |