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/ostream-joiner.hpp>
23 
24 namespace psync {
25 
26 State::State(const ndn::Block& block)
27 {
28  wireDecode(block);
29 }
30 
31 void
32 State::addContent(const ndn::Name& prefix)
33 {
34  m_content.emplace_back(prefix);
35 }
36 
37 const ndn::Block&
39 {
40  if (m_wire.hasWire()) {
41  return m_wire;
42  }
43 
44  ndn::EncodingEstimator estimator;
45  size_t estimatedSize = wireEncode(estimator);
46 
47  ndn::EncodingBuffer buffer(estimatedSize, 0);
48  wireEncode(buffer);
49 
50  m_wire = buffer.block();
51  return m_wire;
52 }
53 
54 template<ndn::encoding::Tag TAG>
55 size_t
56 State::wireEncode(ndn::EncodingImpl<TAG>& block) const
57 {
58  size_t totalLength = 0;
59 
60  for (std::vector<ndn::Name>::const_reverse_iterator it = m_content.rbegin();
61  it != m_content.rend(); ++it) {
62  totalLength += it->wireEncode(block);
63  }
64 
65  totalLength += block.prependVarNumber(totalLength);
66  totalLength += block.prependVarNumber(tlv::PSyncContent);
67 
68  return totalLength;
69 }
70 
72 
73 void
74 State::wireDecode(const ndn::Block& wire)
75 {
76  auto blockType = wire.type();
77  if (blockType != tlv::PSyncContent) {
78  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Expected PSyncContent Block, but Block is of type: #" +
79  ndn::to_string(blockType)));
80  }
81 
82  wire.parse();
83  m_wire = wire;
84 
85  for (auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
86  if (it->type() == ndn::tlv::Name) {
87  m_content.emplace_back(*it);
88  }
89  else {
90  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Expected Name Block, but Block is of type: #" +
91  ndn::to_string(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
void addContent(const ndn::Name &prefix)
Definition: state.cpp:32
const ndn::Block & wireEncode() const
Definition: state.cpp:38
const std::vector< ndn::Name > & getContent() const
Definition: state.hpp:46
std::ostream & operator<<(std::ostream &out, const BloomFilter &bf)
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(State)
void wireDecode(const ndn::Block &wire)
Definition: state.cpp:74
State()=default