blob: cb604e664cac8c0285ed2488b098dbb3d925f898 [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 "list-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, ListParam::Error>::value,
30 "ListParam::Error must inherit from tlv::Error");
31
32const std::string ListParam::VERB("list");
33
34ListParam::ListParam()
35 : m_originType(TYPE_USER)
36{
37}
38
39ListParam::ListParam(const pib::Type originType, const Name& originName)
40 : m_originType(originType)
41 , m_originName(originName)
42{
43}
44
45ListParam::ListParam(const Block& wire)
46{
47 wireDecode(wire);
48}
49
50const Name&
51ListParam::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
59template<bool T>
60size_t
61ListParam::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
87template size_t
88ListParam::wireEncode<true>(EncodingImpl<true>& block) const;
89
90template size_t
91ListParam::wireEncode<false>(EncodingImpl<false>& block) const;
92
93const Block&
94ListParam::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
109void
110ListParam::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