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