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-2020, 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 {
26 namespace detail {
27 
28 State::State(const ndn::Block& block)
29 {
30  wireDecode(block);
31 }
32 
33 void
34 State::addContent(const ndn::Name& prefix)
35 {
36  m_wire.reset();
37  m_content.emplace_back(prefix);
38 }
39 
40 const ndn::Block&
42 {
43  if (m_wire.hasWire()) {
44  return m_wire;
45  }
46 
47  ndn::EncodingEstimator estimator;
48  size_t estimatedSize = wireEncode(estimator);
49 
50  ndn::EncodingBuffer buffer(estimatedSize, 0);
51  wireEncode(buffer);
52 
53  m_wire = buffer.block();
54  return m_wire;
55 }
56 
57 template<ndn::encoding::Tag TAG>
58 size_t
59 State::wireEncode(ndn::EncodingImpl<TAG>& block) const
60 {
61  size_t totalLength = 0;
62 
63  for (auto it = m_content.rbegin(); it != m_content.rend(); ++it) {
64  totalLength += it->wireEncode(block);
65  }
66 
67  totalLength += block.prependVarNumber(totalLength);
68  totalLength += block.prependVarNumber(tlv::PSyncContent);
69 
70  return totalLength;
71 }
72 
74 
75 void
76 State::wireDecode(const ndn::Block& wire)
77 {
78  if (wire.type() != tlv::PSyncContent) {
79  NDN_THROW(ndn::tlv::Error("Expected PSyncContent element, but TLV has type " +
80  ndn::to_string(wire.type())));
81  }
82 
83  m_content.clear();
84 
85  wire.parse();
86  m_wire = wire;
87 
88  for (auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
89  if (it->type() == ndn::tlv::Name) {
90  m_content.emplace_back(*it);
91  }
92  else {
93  NDN_THROW(ndn::tlv::Error("Expected Name element, but TLV has type " +
94  ndn::to_string(it->type())));
95  }
96  }
97 }
98 
99 std::ostream&
100 operator<<(std::ostream& os, const State& state)
101 {
102  auto content = state.getContent();
103 
104  os << "[";
105  std::copy(content.begin(), content.end(), ndn::make_ostream_joiner(os, ", "));
106  os << "]";
107 
108  return os;
109 }
110 
111 } // namespace detail
112 } // namespace psync
std::ostream & operator<<(std::ostream &os, const IBLT &iblt)
Definition: iblt.cpp:259
const ndn::Block & wireEncode() const
Definition: state.cpp:41
Definition: common.hpp:33
void addContent(const ndn::Name &prefix)
Definition: state.cpp:34
void wireDecode(const ndn::Block &wire)
Definition: state.cpp:76
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(State)
const std::vector< ndn::Name > & getContent() const
Definition: state.hpp:48