blob: 4494ecad51107deac3a53ca3911a1c20e0bef47d [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento11904062022-04-14 22:33:28 -04003 * Copyright (c) 2014-2022, Regents of the University of California.
weijia yuan82cf9142018-10-21 12:25:02 -07004 *
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
weijia yuan82cf9142018-10-21 12:25:02 -070022#include <ndn-cxx/encoding/block-helpers.hpp>
weijia yuan82cf9142018-10-21 12:25:02 -070023
24namespace repo {
25
26RepoCommandParameter&
27RepoCommandParameter::setName(const Name& name)
28{
29 m_name = name;
30 m_hasFields[REPO_PARAMETER_NAME] = true;
31 m_wire.reset();
32 return *this;
33}
34
35RepoCommandParameter&
weijia yuan82cf9142018-10-21 12:25:02 -070036RepoCommandParameter::setStartBlockId(uint64_t startBlockId)
37{
38 m_startBlockId = startBlockId;
39 m_hasFields[REPO_PARAMETER_START_BLOCK_ID] = true;
40 m_wire.reset();
41 return *this;
42}
43
44RepoCommandParameter&
45RepoCommandParameter::setEndBlockId(uint64_t endBlockId)
46{
47 m_endBlockId = endBlockId;
48 m_hasFields[REPO_PARAMETER_END_BLOCK_ID] = true;
49 m_wire.reset();
50 return *this;
51}
52
53RepoCommandParameter&
54RepoCommandParameter::setProcessId(uint64_t processId)
55{
56 m_processId = processId;
57 m_hasFields[REPO_PARAMETER_PROCESS_ID] = true;
58 m_wire.reset();
59 return *this;
60}
61
62RepoCommandParameter&
Davide Pesavento11904062022-04-14 22:33:28 -040063RepoCommandParameter::setInterestLifetime(time::milliseconds interestLifetime)
weijia yuan82cf9142018-10-21 12:25:02 -070064{
65 m_interestLifetime = interestLifetime;
66 m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME] = true;
67 m_wire.reset();
68 return *this;
69}
70
71template<ndn::encoding::Tag T>
72size_t
Davide Pesavento11904062022-04-14 22:33:28 -040073RepoCommandParameter::wireEncode(ndn::EncodingImpl<T>& encoder) const
weijia yuan82cf9142018-10-21 12:25:02 -070074{
75 size_t totalLength = 0;
76 size_t variableLength = 0;
77
78 if (m_hasFields[REPO_PARAMETER_PROCESS_ID]) {
79 variableLength = encoder.prependNonNegativeInteger(m_processId);
80 totalLength += variableLength;
81 totalLength += encoder.prependVarNumber(variableLength);
82 totalLength += encoder.prependVarNumber(tlv::ProcessId);
83 }
84
85 if (m_hasFields[REPO_PARAMETER_END_BLOCK_ID]) {
86 variableLength = encoder.prependNonNegativeInteger(m_endBlockId);
87 totalLength += variableLength;
88 totalLength += encoder.prependVarNumber(variableLength);
89 totalLength += encoder.prependVarNumber(tlv::EndBlockId);
90 }
91
92 if (m_hasFields[REPO_PARAMETER_START_BLOCK_ID]) {
93 variableLength = encoder.prependNonNegativeInteger(m_startBlockId);
94 totalLength += variableLength;
95 totalLength += encoder.prependVarNumber(variableLength);
96 totalLength += encoder.prependVarNumber(tlv::StartBlockId);
97 }
98
weijia yuan82cf9142018-10-21 12:25:02 -070099 if (m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME]) {
100 variableLength = encoder.prependNonNegativeInteger(m_interestLifetime.count());
101 totalLength += variableLength;
102 totalLength += encoder.prependVarNumber(variableLength);
103 totalLength += encoder.prependVarNumber(tlv::InterestLifetime);
104 }
105
weijia yuan82cf9142018-10-21 12:25:02 -0700106 if (m_hasFields[REPO_PARAMETER_NAME]) {
107 totalLength += getName().wireEncode(encoder);
108 }
109
110 totalLength += encoder.prependVarNumber(totalLength);
111 totalLength += encoder.prependVarNumber(tlv::RepoCommandParameter);
112 return totalLength;
113}
114
115NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RepoCommandParameter);
116
117Block
118RepoCommandParameter::wireEncode() const
119{
120 if (m_wire.hasWire())
121 return m_wire;
122
Davide Pesavento11904062022-04-14 22:33:28 -0400123 ndn::EncodingEstimator estimator;
weijia yuan82cf9142018-10-21 12:25:02 -0700124 size_t estimatedSize = wireEncode(estimator);
125
Davide Pesavento11904062022-04-14 22:33:28 -0400126 ndn::EncodingBuffer buffer(estimatedSize, 0);
weijia yuan82cf9142018-10-21 12:25:02 -0700127 wireEncode(buffer);
128
129 m_wire = buffer.block();
130 return m_wire;
131}
132
133void
134RepoCommandParameter::wireDecode(const Block& wire)
135{
Davide Pesavento11904062022-04-14 22:33:28 -0400136 if (wire.type() != tlv::RepoCommandParameter) {
137 NDN_THROW(Error("RepoCommandParameter", wire.type()));
138 }
weijia yuan82cf9142018-10-21 12:25:02 -0700139
Davide Pesavento11904062022-04-14 22:33:28 -0400140 m_wire = wire;
weijia yuan82cf9142018-10-21 12:25:02 -0700141 m_wire.parse();
142
weijia yuan82cf9142018-10-21 12:25:02 -0700143 // Name
Davide Pesavento11904062022-04-14 22:33:28 -0400144 auto val = m_wire.find(tlv::Name);
weijia yuan82cf9142018-10-21 12:25:02 -0700145 if (val != m_wire.elements_end())
146 {
147 m_hasFields[REPO_PARAMETER_NAME] = true;
148 m_name.wireDecode(m_wire.get(tlv::Name));
149 }
150
weijia yuan82cf9142018-10-21 12:25:02 -0700151 // StartBlockId
152 val = m_wire.find(tlv::StartBlockId);
153 if (val != m_wire.elements_end())
154 {
155 m_hasFields[REPO_PARAMETER_START_BLOCK_ID] = true;
156 m_startBlockId = readNonNegativeInteger(*val);
157 }
158
159 // EndBlockId
160 val = m_wire.find(tlv::EndBlockId);
161 if (val != m_wire.elements_end())
162 {
163 m_hasFields[REPO_PARAMETER_END_BLOCK_ID] = true;
164 m_endBlockId = readNonNegativeInteger(*val);
165 }
166
167 // ProcessId
168 val = m_wire.find(tlv::ProcessId);
169 if (val != m_wire.elements_end())
170 {
171 m_hasFields[REPO_PARAMETER_PROCESS_ID] = true;
172 m_processId = readNonNegativeInteger(*val);
173 }
174
Davide Pesavento11904062022-04-14 22:33:28 -0400175 // InterestLifetime
weijia yuan82cf9142018-10-21 12:25:02 -0700176 val = m_wire.find(tlv::InterestLifetime);
177 if (val != m_wire.elements_end())
178 {
179 m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME] = true;
Davide Pesavento11904062022-04-14 22:33:28 -0400180 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
weijia yuan82cf9142018-10-21 12:25:02 -0700181 }
182}
183
Davide Pesavento11904062022-04-14 22:33:28 -0400184} // namespace repo