blob: d00a1d07f7f39580bae3b289fedf26e258f84913 [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-response.hpp"
21
22namespace repo {
23
24RepoCommandResponse&
25RepoCommandResponse::setStartBlockId(uint64_t startBlockId)
26{
27 m_startBlockId = startBlockId;
28 m_hasStartBlockId = true;
29 m_wire.reset();
30 return *this;
31}
32
33bool
34RepoCommandResponse::hasStartBlockId() const
35{
36 return m_hasStartBlockId;
37}
38
39RepoCommandResponse&
40RepoCommandResponse::setEndBlockId(uint64_t endBlockId)
41{
42 m_endBlockId = endBlockId;
43 m_hasEndBlockId = true;
44 m_wire.reset();
45 return *this;
46}
47
48bool
49RepoCommandResponse::hasEndBlockId() const
50{
51 return m_hasEndBlockId;
52}
53
54RepoCommandResponse&
55RepoCommandResponse::setProcessId(uint64_t processId)
56{
57 m_processId = processId;
58 m_hasProcessId = true;
59 m_wire.reset();
60 return *this;
61}
62
63bool
64RepoCommandResponse::hasProcessId() const
65{
66 return m_hasProcessId;
67}
68
69RepoCommandResponse&
70RepoCommandResponse::setCode(uint32_t statusCode)
71{
72 m_hasStatusCode = true;
73
74 RepoCommandResponse* response =
75 static_cast<RepoCommandResponse *> (&ndn::mgmt::ControlResponse::setCode(statusCode));
76 return *response;
77}
78
79bool
80RepoCommandResponse::hasStatusCode() const
81{
82 return m_hasStatusCode;
83}
84
85RepoCommandResponse&
86RepoCommandResponse::setInsertNum(uint64_t insertNum)
87{
88 m_insertNum = insertNum;
89 m_hasInsertNum = true;
90 m_wire.reset();
91 return *this;
92}
93
94bool
95RepoCommandResponse::hasInsertNum() const
96{
97 return m_hasInsertNum;
98}
99
100RepoCommandResponse&
101RepoCommandResponse::setDeleteNum(uint64_t deleteNum)
102{
103 m_deleteNum = deleteNum;
104 m_hasDeleteNum = true;
105 m_wire.reset();
106 return *this;
107}
108
109bool
110RepoCommandResponse::hasDeleteNum() const
111{
112 return m_hasDeleteNum;
113}
114
115const Block&
116RepoCommandResponse::wireEncode() const
117{
118 if (m_wire.hasWire())
119 return m_wire;
120
121 EncodingEstimator estimator;
122 size_t estimatedSize = wireEncode(estimator);
123
124 EncodingBuffer buffer(estimatedSize, 0);
125 wireEncode(buffer);
126
127 m_wire = buffer.block();
128 return m_wire;
129}
130
131
132template<ndn::encoding::Tag T>
133size_t
134RepoCommandResponse::wireEncode(EncodingImpl<T>& encoder) const
135{
136 size_t totalLength = 0;
137 size_t variableLength = 0;
138
139 if (m_hasDeleteNum) {
140 variableLength = encoder.prependNonNegativeInteger(m_deleteNum);
141 totalLength += variableLength;
142 totalLength += encoder.prependVarNumber(variableLength);
143 totalLength += encoder.prependVarNumber(tlv::DeleteNum);
144 }
145
146 if (m_hasInsertNum) {
147 variableLength = encoder.prependNonNegativeInteger(m_insertNum);
148 totalLength += variableLength;
149 totalLength += encoder.prependVarNumber(variableLength);
150 totalLength += encoder.prependVarNumber(tlv::InsertNum);
151 }
152
153 if (m_hasEndBlockId) {
154 variableLength = encoder.prependNonNegativeInteger(m_endBlockId);
155 totalLength += variableLength;
156 totalLength += encoder.prependVarNumber(variableLength);
157 totalLength += encoder.prependVarNumber(tlv::EndBlockId);
158 }
159
160 if (m_hasStartBlockId) {
161 variableLength = encoder.prependNonNegativeInteger(m_startBlockId);
162 totalLength += variableLength;
163 totalLength += encoder.prependVarNumber(variableLength);
164 totalLength += encoder.prependVarNumber(repo::tlv::StartBlockId);
165 }
166
167 if (m_hasStatusCode) {
168 variableLength = encoder.prependNonNegativeInteger(getCode());
169 totalLength += variableLength;
170 totalLength += encoder.prependVarNumber(variableLength);
171 totalLength += encoder.prependVarNumber(tlv::StatusCode);
172 }
173 else {
174 BOOST_THROW_EXCEPTION(Error("required field StatusCode is missing"));
175 }
176
177 if (m_hasProcessId) {
178 variableLength = encoder.prependNonNegativeInteger(m_processId);
179 totalLength += variableLength;
180 totalLength += encoder.prependVarNumber(variableLength);
181 totalLength += encoder.prependVarNumber(tlv::ProcessId);
182 }
183
184 totalLength += encoder.prependVarNumber(totalLength);
185 totalLength += encoder.prependVarNumber(tlv::RepoCommandResponse);
186 return totalLength;
187}
188
189void
190RepoCommandResponse::wireDecode(const Block& wire)
191{
192 m_hasStartBlockId = false;
193 m_hasEndBlockId = false;
194 m_hasProcessId = false;
195 m_hasStatusCode = false;
196 m_hasInsertNum = false;
197 m_hasDeleteNum = false;
198
199 m_wire = wire;
200
201 m_wire.parse();
202
203 Block::element_const_iterator val;
204
205 if (m_wire.type() != tlv::RepoCommandResponse)
206 BOOST_THROW_EXCEPTION(Error("RepoCommandResponse malformed"));
207
208 // StartBlockId
209 val = m_wire.find(tlv::StartBlockId);
210 if (val != m_wire.elements_end()) {
211 m_hasStartBlockId = true;
212 m_startBlockId = readNonNegativeInteger(*val);
213 }
214
215 // EndBlockId
216 val = m_wire.find(tlv::EndBlockId);
217 if (val != m_wire.elements_end()) {
218 m_hasEndBlockId = true;
219 m_endBlockId = readNonNegativeInteger(*val);
220 }
221
222 // ProcessId
223 val = m_wire.find(tlv::ProcessId);
224 if (val != m_wire.elements_end()) {
225 m_hasProcessId = true;
226 m_processId = readNonNegativeInteger(*val);
227 }
228
229 //StatusCode
230 val = m_wire.find(tlv::StatusCode);
231 if (val != m_wire.elements_end()) {
232 setCode(readNonNegativeInteger(*val));
233 }
234 else {
235 BOOST_THROW_EXCEPTION(Error("required field StatusCode is missing"));
236 }
237
238 // InsertNum
239 val = m_wire.find(tlv::InsertNum);
240 if (val != m_wire.elements_end()) {
241 m_hasInsertNum = true;
242 m_insertNum = readNonNegativeInteger(*val);
243 }
244
245 // DeleteNum
246 val = m_wire.find(tlv::DeleteNum);
247 if (val != m_wire.elements_end()) {
248 m_hasDeleteNum = true;
249 m_deleteNum = readNonNegativeInteger(*val);
250 }
251}
252
253NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RepoCommandResponse);
254
255std::ostream&
256operator<<(std::ostream& os, const RepoCommandResponse& repoCommandResponse)
257{
258 os << "RepoCommandResponse(";
259
260 if (repoCommandResponse.hasProcessId()) {
261 os << " ProcessId: " << repoCommandResponse.getProcessId();
262 }
263 // if (repoCommandResponse.hasStatusCode()) {
264 // os << " StatusCode: " << repoCommandResponse.getStatusCode();
265 // }
266 if (repoCommandResponse.hasStartBlockId()) {
267 os << " StartBlockId: " << repoCommandResponse.getStartBlockId();
268 }
269 if (repoCommandResponse.hasEndBlockId()) {
270 os << " EndBlockId: " << repoCommandResponse.getEndBlockId();
271 }
272 if (repoCommandResponse.hasInsertNum()) {
273 os << " InsertNum: " << repoCommandResponse.getInsertNum();
274 }
275 if (repoCommandResponse.hasDeleteNum()) {
276 os << " DeleteNum: " << repoCommandResponse.getDeleteNum();
277
278 }
279 os << " )";
280 return os;
281}
282
283} // namespace repo