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