blob: 4b67ed5fb75b5f183c24492c85fa032ec3786a87 [file] [log] [blame]
Yingdi Yube4aeed2015-03-23 13:28:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07003 * Copyright (c) 2014-2017, Regents of the University of California
Yingdi Yube4aeed2015-03-23 13:28:58 -07004 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07005 * 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 Yube4aeed2015-03-23 13:28:58 -07008 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07009 * 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 Yube4aeed2015-03-23 13:28:58 -070013 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070014 * 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 Yube4aeed2015-03-23 13:28:58 -070017 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070018 * 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 Yube4aeed2015-03-23 13:28:58 -070020 */
21
22#include "logger-response.hpp"
23#include "tlv.hpp"
24
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070025namespace ndn {
26namespace delorean {
Yingdi Yube4aeed2015-03-23 13:28:58 -070027
28LoggerResponse::LoggerResponse()
29 : m_code(-1)
30{
31}
32
33LoggerResponse::LoggerResponse(int32_t code, const std::string& msg)
34 : m_code(code)
35 , m_msg(msg)
36 , m_dataSeqNo(0)
37{
38}
39
40LoggerResponse::LoggerResponse(const NonNegativeInteger& seqNo)
41 : m_code(0)
42 , m_dataSeqNo(seqNo)
43{
44}
45
46template<ndn::encoding::Tag TAG>
47size_t
48LoggerResponse::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 Afanasyevb1ba9c92017-05-06 13:16:18 -070058 totalLength += prependNonNegativeIntegerBlock(block, tlv::DataSeqNo, m_dataSeqNo);
Yingdi Yube4aeed2015-03-23 13:28:58 -070059 }
Alexander Afanasyevb1ba9c92017-05-06 13:16:18 -070060 totalLength += prependNonNegativeIntegerBlock(block, tlv::ResultCode, m_code);
Yingdi Yube4aeed2015-03-23 13:28:58 -070061
62 totalLength += block.prependVarNumber(totalLength);
63 totalLength += block.prependVarNumber(tlv::LogResponse);
64
65 return totalLength;
66}
67
68template size_t
69LoggerResponse::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>&) const;
70
71template size_t
72LoggerResponse::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>&) const;
73
74
75const Block&
76LoggerResponse::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
91void
92LoggerResponse::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 Afanasyev49e2e4c2017-05-06 13:42:57 -0700132} // namespace delorean
133} // namespace ndn