blob: 2f4de5cb58bf045faa228ee534b5e234e92cb3bb [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, Regents of the University of California.
4 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng 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.
11 *
12 * repo-ng 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.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "repo-command-parameter.hpp"
21
22#include <ndn-cxx/encoding/encoding-buffer.hpp>
23#include <ndn-cxx/encoding/block-helpers.hpp>
weijia yuan82cf9142018-10-21 12:25:02 -070024#include <ndn-cxx/mgmt/control-parameters.hpp>
weijia yuan3aa8d2b2018-03-06 15:35:57 -080025#include <ndn-cxx/name.hpp>
weijia yuan82cf9142018-10-21 12:25:02 -070026
27namespace repo {
28
29RepoCommandParameter&
30RepoCommandParameter::setName(const Name& name)
31{
32 m_name = name;
33 m_hasFields[REPO_PARAMETER_NAME] = true;
34 m_wire.reset();
35 return *this;
36}
37
38RepoCommandParameter&
weijia yuan82cf9142018-10-21 12:25:02 -070039RepoCommandParameter::setStartBlockId(uint64_t startBlockId)
40{
41 m_startBlockId = startBlockId;
42 m_hasFields[REPO_PARAMETER_START_BLOCK_ID] = true;
43 m_wire.reset();
44 return *this;
45}
46
47RepoCommandParameter&
48RepoCommandParameter::setEndBlockId(uint64_t endBlockId)
49{
50 m_endBlockId = endBlockId;
51 m_hasFields[REPO_PARAMETER_END_BLOCK_ID] = true;
52 m_wire.reset();
53 return *this;
54}
55
56RepoCommandParameter&
57RepoCommandParameter::setProcessId(uint64_t processId)
58{
59 m_processId = processId;
60 m_hasFields[REPO_PARAMETER_PROCESS_ID] = true;
61 m_wire.reset();
62 return *this;
63}
64
65RepoCommandParameter&
weijia yuan82cf9142018-10-21 12:25:02 -070066RepoCommandParameter::setInterestLifetime(milliseconds interestLifetime)
67{
68 m_interestLifetime = interestLifetime;
69 m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME] = true;
70 m_wire.reset();
71 return *this;
72}
73
74template<ndn::encoding::Tag T>
75size_t
76RepoCommandParameter::wireEncode(EncodingImpl<T>& encoder) const
77{
78 size_t totalLength = 0;
79 size_t variableLength = 0;
80
81 if (m_hasFields[REPO_PARAMETER_PROCESS_ID]) {
82 variableLength = encoder.prependNonNegativeInteger(m_processId);
83 totalLength += variableLength;
84 totalLength += encoder.prependVarNumber(variableLength);
85 totalLength += encoder.prependVarNumber(tlv::ProcessId);
86 }
87
88 if (m_hasFields[REPO_PARAMETER_END_BLOCK_ID]) {
89 variableLength = encoder.prependNonNegativeInteger(m_endBlockId);
90 totalLength += variableLength;
91 totalLength += encoder.prependVarNumber(variableLength);
92 totalLength += encoder.prependVarNumber(tlv::EndBlockId);
93 }
94
95 if (m_hasFields[REPO_PARAMETER_START_BLOCK_ID]) {
96 variableLength = encoder.prependNonNegativeInteger(m_startBlockId);
97 totalLength += variableLength;
98 totalLength += encoder.prependVarNumber(variableLength);
99 totalLength += encoder.prependVarNumber(tlv::StartBlockId);
100 }
101
weijia yuan82cf9142018-10-21 12:25:02 -0700102 if (m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME]) {
103 variableLength = encoder.prependNonNegativeInteger(m_interestLifetime.count());
104 totalLength += variableLength;
105 totalLength += encoder.prependVarNumber(variableLength);
106 totalLength += encoder.prependVarNumber(tlv::InterestLifetime);
107 }
108
weijia yuan82cf9142018-10-21 12:25:02 -0700109 if (m_hasFields[REPO_PARAMETER_NAME]) {
110 totalLength += getName().wireEncode(encoder);
111 }
112
113 totalLength += encoder.prependVarNumber(totalLength);
114 totalLength += encoder.prependVarNumber(tlv::RepoCommandParameter);
115 return totalLength;
116}
117
118NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RepoCommandParameter);
119
120Block
121RepoCommandParameter::wireEncode() const
122{
123 if (m_wire.hasWire())
124 return m_wire;
125
126 EncodingEstimator estimator;
127 size_t estimatedSize = wireEncode(estimator);
128
129 EncodingBuffer buffer(estimatedSize, 0);
130 wireEncode(buffer);
131
132 m_wire = buffer.block();
133 return m_wire;
134}
135
136void
137RepoCommandParameter::wireDecode(const Block& wire)
138{
139 m_wire = wire;
140
141 m_wire.parse();
142
143 if (m_wire.type() != tlv::RepoCommandParameter)
144 BOOST_THROW_EXCEPTION(Error("Requested decoding of RepoCommandParameter, but Block is of different type"));
145
146 // Name
147 Block::element_const_iterator val = m_wire.find(tlv::Name);
148 if (val != m_wire.elements_end())
149 {
150 m_hasFields[REPO_PARAMETER_NAME] = true;
151 m_name.wireDecode(m_wire.get(tlv::Name));
152 }
153
weijia yuan82cf9142018-10-21 12:25:02 -0700154 // StartBlockId
155 val = m_wire.find(tlv::StartBlockId);
156 if (val != m_wire.elements_end())
157 {
158 m_hasFields[REPO_PARAMETER_START_BLOCK_ID] = true;
159 m_startBlockId = readNonNegativeInteger(*val);
160 }
161
162 // EndBlockId
163 val = m_wire.find(tlv::EndBlockId);
164 if (val != m_wire.elements_end())
165 {
166 m_hasFields[REPO_PARAMETER_END_BLOCK_ID] = true;
167 m_endBlockId = readNonNegativeInteger(*val);
168 }
169
170 // ProcessId
171 val = m_wire.find(tlv::ProcessId);
172 if (val != m_wire.elements_end())
173 {
174 m_hasFields[REPO_PARAMETER_PROCESS_ID] = true;
175 m_processId = readNonNegativeInteger(*val);
176 }
177
weijia yuan82cf9142018-10-21 12:25:02 -0700178 // InterestLifeTime
179 val = m_wire.find(tlv::InterestLifetime);
180 if (val != m_wire.elements_end())
181 {
182 m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME] = true;
183 m_interestLifetime = milliseconds(readNonNegativeInteger(*val));
184 }
185}
186
187std::ostream&
188operator<<(std::ostream& os, const RepoCommandParameter& repoCommandParameter)
189{
190 os << "RepoCommandParameter(";
191
192 // Name
193 if (repoCommandParameter.hasName()) {
194 os << " Name: " << repoCommandParameter.getName();
195 }
196 if (repoCommandParameter.hasStartBlockId()) {
197 // StartBlockId
198 os << " StartBlockId: " << repoCommandParameter.getStartBlockId();
199 }
200 // EndBlockId
201 if (repoCommandParameter.hasEndBlockId()) {
202 os << " EndBlockId: " << repoCommandParameter.getEndBlockId();
203 }
204 // ProcessId
205 if (repoCommandParameter.hasProcessId()) {
206 os << " ProcessId: " << repoCommandParameter.getProcessId();
207 }
weijia yuan82cf9142018-10-21 12:25:02 -0700208 // InterestLifetime
209 if (repoCommandParameter.hasProcessId()) {
210 os << " InterestLifetime: " << repoCommandParameter.getInterestLifetime();
211 }
212 os << " )";
213 return os;
214}
215
216} // namespace repo