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