blob: 06be21ea6b5a856e32f72e3007ca40af5d09218b [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu0a312e52015-07-22 13:14:53 -07003 * Copyright (c) 2014-2015, Regents of the University of California.
Yingdi Yu77627ab2015-07-21 16:13:49 -07004 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07005 * 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 Yu77627ab2015-07-21 16:13:49 -07007 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07008 * 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 Yu77627ab2015-07-21 16:13:49 -070011 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070012 * 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 Yu77627ab2015-07-21 16:13:49 -070015 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070016 * 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 Yu77627ab2015-07-21 16:13:49 -070018 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070019 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu77627ab2015-07-21 16:13:49 -070020 */
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