blob: 41c8f6a19b1b3bf20331439dd7bc69f699bffbb3 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 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 "delete-param.hpp"
23#include <ndn-cxx/encoding/block-helpers.hpp>
24#include <boost/lexical_cast.hpp>
25
26namespace ndn {
27namespace pib {
28
29static_assert(std::is_base_of<tlv::Error, DeleteParam::Error>::value,
30 "DeleteParam::Error must inherit from tlv::Error");
31
32const std::string DeleteParam::VERB("delete");
33
34DeleteParam::DeleteParam()
35 : m_targetType(TYPE_DEFAULT)
36{
37}
38
39DeleteParam::DeleteParam(const Name& name, pib::Type type)
40 : m_targetType(type)
41 , m_targetName(name)
42{
43}
44
45DeleteParam::DeleteParam(const Block& wire)
46{
47 wireDecode(wire);
48}
49
50template<bool T>
51size_t
52DeleteParam::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
71template size_t
72DeleteParam::wireEncode<true>(EncodingImpl<true>& block) const;
73
74template size_t
75DeleteParam::wireEncode<false>(EncodingImpl<false>& block) const;
76
77const Block&
78DeleteParam::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
93void
94DeleteParam::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