Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California |
Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 5 | * This file is part of NDN DeLorean, An Authentication System for Data Archives in |
| 6 | * Named Data Networking. See AUTHORS.md for complete list of NDN DeLorean authors |
| 7 | * and contributors. |
Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 8 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 9 | * NDN DeLorean is free software: you can redistribute it and/or modify it under |
| 10 | * the terms of the GNU General Public License as published by the Free Software |
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
| 12 | * version. |
Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 13 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 14 | * NDN DeLorean is distributed in the hope that it will be useful, but WITHOUT ANY |
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 16 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 17 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License along with NDN |
| 19 | * DeLorean, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "logger-response.hpp" |
| 23 | #include "tlv.hpp" |
| 24 | |
Alexander Afanasyev | 49e2e4c | 2017-05-06 13:42:57 -0700 | [diff] [blame^] | 25 | namespace ndn { |
| 26 | namespace delorean { |
Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 27 | |
| 28 | LoggerResponse::LoggerResponse() |
| 29 | : m_code(-1) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | LoggerResponse::LoggerResponse(int32_t code, const std::string& msg) |
| 34 | : m_code(code) |
| 35 | , m_msg(msg) |
| 36 | , m_dataSeqNo(0) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | LoggerResponse::LoggerResponse(const NonNegativeInteger& seqNo) |
| 41 | : m_code(0) |
| 42 | , m_dataSeqNo(seqNo) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | template<ndn::encoding::Tag TAG> |
| 47 | size_t |
| 48 | LoggerResponse::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 49 | { |
| 50 | size_t totalLength = 0; |
| 51 | |
| 52 | if (m_code != 0) { |
| 53 | totalLength += block.prependByteArrayBlock(tlv::ResultMsg, |
| 54 | reinterpret_cast<const uint8_t*>(m_msg.c_str()), |
| 55 | m_msg.size()); |
| 56 | } |
| 57 | else { |
Alexander Afanasyev | b1ba9c9 | 2017-05-06 13:16:18 -0700 | [diff] [blame] | 58 | totalLength += prependNonNegativeIntegerBlock(block, tlv::DataSeqNo, m_dataSeqNo); |
Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 59 | } |
Alexander Afanasyev | b1ba9c9 | 2017-05-06 13:16:18 -0700 | [diff] [blame] | 60 | totalLength += prependNonNegativeIntegerBlock(block, tlv::ResultCode, m_code); |
Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame] | 61 | |
| 62 | totalLength += block.prependVarNumber(totalLength); |
| 63 | totalLength += block.prependVarNumber(tlv::LogResponse); |
| 64 | |
| 65 | return totalLength; |
| 66 | } |
| 67 | |
| 68 | template size_t |
| 69 | LoggerResponse::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>&) const; |
| 70 | |
| 71 | template size_t |
| 72 | LoggerResponse::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>&) const; |
| 73 | |
| 74 | |
| 75 | const Block& |
| 76 | LoggerResponse::wireEncode() const |
| 77 | { |
| 78 | if (m_wire.hasWire()) |
| 79 | return m_wire; |
| 80 | |
| 81 | ndn::EncodingEstimator estimator; |
| 82 | size_t estimatedSize = wireEncode(estimator); |
| 83 | |
| 84 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 85 | wireEncode(buffer); |
| 86 | |
| 87 | m_wire = buffer.block(); |
| 88 | return m_wire; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | LoggerResponse::wireDecode(const Block& wire) |
| 93 | { |
| 94 | if (!wire.hasWire()) { |
| 95 | throw Error("The supplied block does not contain wire format"); |
| 96 | } |
| 97 | |
| 98 | m_wire = wire; |
| 99 | m_wire.parse(); |
| 100 | |
| 101 | if (m_wire.type() != tlv::LogResponse) |
| 102 | throw tlv::Error("Unexpected TLV type when decoding log response"); |
| 103 | |
| 104 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 105 | |
| 106 | // the first block must be result code |
| 107 | if (it != m_wire.elements_end() && it->type() == tlv::ResultCode) { |
| 108 | m_code = readNonNegativeInteger(*it); |
| 109 | it++; |
| 110 | } |
| 111 | else |
| 112 | throw Error("The first sub-TLV is not ResultCode"); |
| 113 | |
| 114 | // the second block could be result msg |
| 115 | if (it == m_wire.elements_end()) |
| 116 | return; |
| 117 | else if (it->type() == tlv::ResultMsg) { |
| 118 | m_msg = std::string(reinterpret_cast<const char*>(it->value()), it->value_size()); |
| 119 | it++; |
| 120 | } |
| 121 | else if (it->type() == tlv::DataSeqNo) { |
| 122 | m_dataSeqNo = readNonNegativeInteger(*it); |
| 123 | it++; |
| 124 | } |
| 125 | else |
| 126 | throw Error("The second sub-TLV is not ResultMsg"); |
| 127 | |
| 128 | if (it != m_wire.elements_end()) |
| 129 | throw Error("No more sub-TLV in log response"); |
| 130 | } |
| 131 | |
Alexander Afanasyev | 49e2e4c | 2017-05-06 13:42:57 -0700 | [diff] [blame^] | 132 | } // namespace delorean |
| 133 | } // namespace ndn |