blob: 1f5c63b417a4645645febf3c9b6189bb4f32089b [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 "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