state.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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 Lesser 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser 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 "PSync/detail/state.hpp"
21 
22 #include <ndn-cxx/util/exception.hpp>
23 #include <ndn-cxx/util/ostream-joiner.hpp>
24 
25 namespace psync::detail {
26 
27 State::State(const ndn::Block& block)
28 {
29  wireDecode(block);
30 }
31 
32 void
33 State::addContent(const ndn::Name& prefix)
34 {
35  m_wire.reset();
36  m_content.emplace_back(prefix);
37 }
38 
39 const ndn::Block&
41 {
42  if (m_wire.hasWire()) {
43  return m_wire;
44  }
45 
46  ndn::EncodingEstimator estimator;
47  size_t estimatedSize = wireEncode(estimator);
48 
49  ndn::EncodingBuffer buffer(estimatedSize, 0);
50  wireEncode(buffer);
51 
52  m_wire = buffer.block();
53  return m_wire;
54 }
55 
56 template<ndn::encoding::Tag TAG>
57 size_t
58 State::wireEncode(ndn::EncodingImpl<TAG>& block) const
59 {
60  size_t totalLength = 0;
61 
62  for (auto it = m_content.rbegin(); it != m_content.rend(); ++it) {
63  totalLength += it->wireEncode(block);
64  }
65 
66  totalLength += block.prependVarNumber(totalLength);
67  totalLength += block.prependVarNumber(tlv::PSyncContent);
68 
69  return totalLength;
70 }
71 
73 
74 void
75 State::wireDecode(const ndn::Block& wire)
76 {
77  if (wire.type() != tlv::PSyncContent) {
78  NDN_THROW(ndn::tlv::Error("PSyncContent", wire.type()));
79  }
80 
81  m_content.clear();
82 
83  wire.parse();
84  m_wire = wire;
85 
86  for (auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
87  if (it->type() == ndn::tlv::Name) {
88  m_content.emplace_back(*it);
89  }
90  else {
91  NDN_THROW(ndn::tlv::Error("Name", it->type()));
92  }
93  }
94 }
95 
96 std::ostream&
97 operator<<(std::ostream& os, const State& state)
98 {
99  auto content = state.getContent();
100 
101  os << "[";
102  std::copy(content.begin(), content.end(), ndn::make_ostream_joiner(os, ", "));
103  os << "]";
104 
105  return os;
106 }
107 
108 } // namespace psync::detail
const std::vector< ndn::Name > & getContent() const
Definition: state.hpp:47
void wireDecode(const ndn::Block &wire)
Definition: state.cpp:75
void addContent(const ndn::Name &prefix)
Definition: state.cpp:33
const ndn::Block & wireEncode() const
Definition: state.cpp:40
std::ostream & operator<<(std::ostream &os, const IBLT &iblt)
Definition: iblt.cpp:158
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(State)
@ PSyncContent
Definition: state.hpp:28