Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2018, The University of Memphis |
| 4 | * |
| 5 | * This file is part of PSync. |
| 6 | * See AUTHORS.md for complete list of PSync authors and contributors. |
| 7 | * |
| 8 | * PSync 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 | * PSync 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 | * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
| 20 | #include "state.hpp" |
| 21 | |
| 22 | namespace psync { |
| 23 | |
| 24 | State::State(const ndn::Block& block) |
| 25 | { |
| 26 | wireDecode(block); |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | State::addContent(const ndn::Name& prefix) |
| 31 | { |
| 32 | m_content.emplace_back(prefix); |
| 33 | } |
| 34 | |
| 35 | const ndn::Block& |
| 36 | State::wireEncode() const |
| 37 | { |
| 38 | if (m_wire.hasWire()) { |
| 39 | return m_wire; |
| 40 | } |
| 41 | |
| 42 | ndn::EncodingEstimator estimator; |
| 43 | size_t estimatedSize = wireEncode(estimator); |
| 44 | |
| 45 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 46 | wireEncode(buffer); |
| 47 | |
| 48 | m_wire = buffer.block(); |
| 49 | return m_wire; |
| 50 | } |
| 51 | |
| 52 | template<ndn::encoding::Tag TAG> |
| 53 | size_t |
| 54 | State::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 55 | { |
| 56 | size_t totalLength = 0; |
| 57 | |
| 58 | for (std::vector<ndn::Name>::const_reverse_iterator it = m_content.rbegin(); |
| 59 | it != m_content.rend(); ++it) { |
| 60 | totalLength += it->wireEncode(block); |
| 61 | } |
| 62 | |
| 63 | totalLength += block.prependVarNumber(totalLength); |
| 64 | totalLength += block.prependVarNumber(tlv::PSyncContent); |
| 65 | |
| 66 | return totalLength; |
| 67 | } |
| 68 | |
| 69 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(State); |
| 70 | |
| 71 | void |
| 72 | State::wireDecode(const ndn::Block& wire) |
| 73 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame^] | 74 | auto blockType = wire.type(); |
| 75 | |
| 76 | if (blockType != tlv::PSyncContent && blockType != ndn::tlv::Content) { |
| 77 | BOOST_THROW_EXCEPTION(ndn::tlv::Error("Expected Content/PSyncContent Block, but Block is of a different type: #" + |
| 78 | ndn::to_string(blockType))); |
| 79 | return; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | wire.parse(); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame^] | 83 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 84 | m_wire = wire; |
| 85 | |
| 86 | auto it = m_wire.elements_begin(); |
| 87 | |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame^] | 88 | if (it == m_wire.elements_end()) { |
| 89 | return; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame^] | 92 | if (blockType == tlv::PSyncContent) { |
| 93 | while(it != m_wire.elements_end()) { |
| 94 | if (it->type() == ndn::tlv::Name) { |
| 95 | m_content.emplace_back(*it); |
| 96 | } |
| 97 | else { |
| 98 | BOOST_THROW_EXCEPTION(ndn::tlv::Error("Expected Name Block, but Block is of a different type: #" + |
| 99 | ndn::to_string(it->type()))); |
| 100 | } |
| 101 | ++it; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 102 | } |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame^] | 103 | } |
| 104 | else if (blockType == ndn::tlv::Content) { |
| 105 | it->parse(); |
| 106 | for (auto val = it->elements_begin(); val != it->elements_end(); ++val) { |
| 107 | if (val->type() == ndn::tlv::Name) { |
| 108 | m_content.emplace_back(*val); |
| 109 | } |
| 110 | else { |
| 111 | BOOST_THROW_EXCEPTION(ndn::tlv::Error("Expected Name Block, but Block is of a different type: #" + |
| 112 | ndn::to_string(val->type()))); |
| 113 | } |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | std::ostream& |
| 119 | operator<<(std::ostream& os, const State& state) |
| 120 | { |
| 121 | std::vector<ndn::Name> content = state.getContent(); |
| 122 | |
| 123 | os << "["; |
| 124 | std::copy(content.begin(), content.end(), ndn::make_ostream_joiner(os, ", ")); |
| 125 | os << "]"; |
| 126 | |
| 127 | return os; |
| 128 | } |
| 129 | |
| 130 | } // namespace psync |