weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 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 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 22 | #include <ndn-cxx/encoding/block-helpers.hpp> |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 23 | |
| 24 | namespace repo { |
| 25 | |
| 26 | RepoCommandParameter& |
| 27 | RepoCommandParameter::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 | |
| 35 | RepoCommandParameter& |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 36 | RepoCommandParameter::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 | |
| 44 | RepoCommandParameter& |
| 45 | RepoCommandParameter::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 | |
| 53 | RepoCommandParameter& |
| 54 | RepoCommandParameter::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 | |
| 62 | RepoCommandParameter& |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 63 | RepoCommandParameter::setInterestLifetime(time::milliseconds interestLifetime) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 64 | { |
| 65 | m_interestLifetime = interestLifetime; |
| 66 | m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME] = true; |
| 67 | m_wire.reset(); |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | template<ndn::encoding::Tag T> |
| 72 | size_t |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 73 | RepoCommandParameter::wireEncode(ndn::EncodingImpl<T>& encoder) const |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 74 | { |
| 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 yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 99 | 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 yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 106 | 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 | |
| 115 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RepoCommandParameter); |
| 116 | |
| 117 | Block |
| 118 | RepoCommandParameter::wireEncode() const |
| 119 | { |
| 120 | if (m_wire.hasWire()) |
| 121 | return m_wire; |
| 122 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 123 | ndn::EncodingEstimator estimator; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 124 | size_t estimatedSize = wireEncode(estimator); |
| 125 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 126 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 127 | wireEncode(buffer); |
| 128 | |
| 129 | m_wire = buffer.block(); |
| 130 | return m_wire; |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | RepoCommandParameter::wireDecode(const Block& wire) |
| 135 | { |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 136 | if (wire.type() != tlv::RepoCommandParameter) { |
| 137 | NDN_THROW(Error("RepoCommandParameter", wire.type())); |
| 138 | } |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 139 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 140 | m_wire = wire; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 141 | m_wire.parse(); |
| 142 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 143 | // Name |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 144 | auto val = m_wire.find(tlv::Name); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 145 | 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 yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 151 | // 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 Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 175 | // InterestLifetime |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 176 | val = m_wire.find(tlv::InterestLifetime); |
| 177 | if (val != m_wire.elements_end()) |
| 178 | { |
| 179 | m_hasFields[REPO_PARAMETER_INTEREST_LIFETIME] = true; |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 180 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val)); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 184 | } // namespace repo |