Yingdi Yu | be4aeed | 2015-03-23 13:28:58 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of NSL (NDN Signature Logger). |
| 6 | * See AUTHORS.md for complete list of NSL authors and contributors. |
| 7 | * |
| 8 | * NSL 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 | * NSL 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 | * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of nsl authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "logger-response.hpp" |
| 23 | #include "tlv.hpp" |
| 24 | |
| 25 | namespace nsl { |
| 26 | |
| 27 | LoggerResponse::LoggerResponse() |
| 28 | : m_code(-1) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | LoggerResponse::LoggerResponse(int32_t code, const std::string& msg) |
| 33 | : m_code(code) |
| 34 | , m_msg(msg) |
| 35 | , m_dataSeqNo(0) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | LoggerResponse::LoggerResponse(const NonNegativeInteger& seqNo) |
| 40 | : m_code(0) |
| 41 | , m_dataSeqNo(seqNo) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | template<ndn::encoding::Tag TAG> |
| 46 | size_t |
| 47 | LoggerResponse::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 48 | { |
| 49 | size_t totalLength = 0; |
| 50 | |
| 51 | if (m_code != 0) { |
| 52 | totalLength += block.prependByteArrayBlock(tlv::ResultMsg, |
| 53 | reinterpret_cast<const uint8_t*>(m_msg.c_str()), |
| 54 | m_msg.size()); |
| 55 | } |
| 56 | else { |
| 57 | totalLength += ndn::prependNonNegativeIntegerBlock(block, tlv::DataSeqNo, m_dataSeqNo); |
| 58 | } |
| 59 | totalLength += ndn::prependNonNegativeIntegerBlock(block, tlv::ResultCode, m_code); |
| 60 | |
| 61 | totalLength += block.prependVarNumber(totalLength); |
| 62 | totalLength += block.prependVarNumber(tlv::LogResponse); |
| 63 | |
| 64 | return totalLength; |
| 65 | } |
| 66 | |
| 67 | template size_t |
| 68 | LoggerResponse::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>&) const; |
| 69 | |
| 70 | template size_t |
| 71 | LoggerResponse::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>&) const; |
| 72 | |
| 73 | |
| 74 | const Block& |
| 75 | LoggerResponse::wireEncode() const |
| 76 | { |
| 77 | if (m_wire.hasWire()) |
| 78 | return m_wire; |
| 79 | |
| 80 | ndn::EncodingEstimator estimator; |
| 81 | size_t estimatedSize = wireEncode(estimator); |
| 82 | |
| 83 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 84 | wireEncode(buffer); |
| 85 | |
| 86 | m_wire = buffer.block(); |
| 87 | return m_wire; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | LoggerResponse::wireDecode(const Block& wire) |
| 92 | { |
| 93 | if (!wire.hasWire()) { |
| 94 | throw Error("The supplied block does not contain wire format"); |
| 95 | } |
| 96 | |
| 97 | m_wire = wire; |
| 98 | m_wire.parse(); |
| 99 | |
| 100 | if (m_wire.type() != tlv::LogResponse) |
| 101 | throw tlv::Error("Unexpected TLV type when decoding log response"); |
| 102 | |
| 103 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 104 | |
| 105 | // the first block must be result code |
| 106 | if (it != m_wire.elements_end() && it->type() == tlv::ResultCode) { |
| 107 | m_code = readNonNegativeInteger(*it); |
| 108 | it++; |
| 109 | } |
| 110 | else |
| 111 | throw Error("The first sub-TLV is not ResultCode"); |
| 112 | |
| 113 | // the second block could be result msg |
| 114 | if (it == m_wire.elements_end()) |
| 115 | return; |
| 116 | else if (it->type() == tlv::ResultMsg) { |
| 117 | m_msg = std::string(reinterpret_cast<const char*>(it->value()), it->value_size()); |
| 118 | it++; |
| 119 | } |
| 120 | else if (it->type() == tlv::DataSeqNo) { |
| 121 | m_dataSeqNo = readNonNegativeInteger(*it); |
| 122 | it++; |
| 123 | } |
| 124 | else |
| 125 | throw Error("The second sub-TLV is not ResultMsg"); |
| 126 | |
| 127 | if (it != m_wire.elements_end()) |
| 128 | throw Error("No more sub-TLV in log response"); |
| 129 | } |
| 130 | |
| 131 | } // namespace nsl |