blob: 26a8944671830bbb91866c12168f55537572ea57 [file] [log] [blame]
Yingdi Yud514c172014-08-26 21:49:39 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -08003 * Copyright (c) 2012-2017 University of California, Los Angeles
Yingdi Yud514c172014-08-26 21:49:39 -07004 *
5 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
7 *
8 * ChronoSync 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, either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * ChronoSync 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 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
20 * @author Chaoyi Bian <bcy@pku.edu.cn>
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22 * @author Yingdi Yu <yingdi@cs.ucla.edu>
23 */
24
25#include "state.hpp"
26
27namespace chronosync {
28
29using boost::make_tuple;
30
31State::~State()
32{
33}
34
35/**
36 * @brief Add or update leaf to the sync tree
37 *
38 * @param info session name of the leaf
39 * @param seq sequence number of the leaf
40 * @return 3-tuple (isInserted, isUpdated, oldSeqNo)
41 */
42boost::tuple<bool, bool, SeqNo>
43State::update(const Name& info, const SeqNo& seq)
44{
45 m_wire.reset();
46
47 LeafContainer::iterator leaf = m_leaves.find(info);
48
49 if (leaf == m_leaves.end()) {
50 m_leaves.insert(make_shared<Leaf>(info, cref(seq)));
51 return make_tuple(true, false, 0);
52 }
53 else {
54 if ((*leaf)->getSeq() == seq || seq < (*leaf)->getSeq()) {
55 return make_tuple(false, false, 0);
56 }
57
58 SeqNo old = (*leaf)->getSeq();
59 m_leaves.modify(leaf,
Yingdi Yu906c2ea2014-10-31 11:24:50 -070060 [=] (LeafPtr& leaf) { leaf->setSeq(seq); } );
Yingdi Yud514c172014-08-26 21:49:39 -070061 return make_tuple(false, true, old);
62 }
63}
64
65ndn::ConstBufferPtr
66State::getRootDigest() const
67{
68 m_digest.reset();
69
70 BOOST_FOREACH (ConstLeafPtr leaf, m_leaves.get<ordered>())
71 {
72 BOOST_ASSERT(leaf != 0);
73 m_digest.update(leaf->getDigest()->buf(), leaf->getDigest()->size());
74 }
75
76 return m_digest.computeDigest();
77}
78
79
80void
81State::reset()
82{
83 m_leaves.clear();
84}
85
86State&
87State::operator+=(const State& state)
88{
89 BOOST_FOREACH (ConstLeafPtr leaf, state.getLeaves())
90 {
91 BOOST_ASSERT(leaf != 0);
92 update(leaf->getSessionName(), leaf->getSeq());
93 }
94
95 return *this;
96}
97
98template<bool T>
99size_t
100State::wireEncode(ndn::EncodingImpl<T>& block) const
101{
102 size_t totalLength = 0;
103
104 BOOST_REVERSE_FOREACH (ConstLeafPtr leaf, m_leaves.get<ordered>())
105 {
106 size_t entryLength = 0;
Alexander Afanasyevedaef7c2015-06-29 17:08:01 -0700107 entryLength += prependNonNegativeIntegerBlock(block, tlv::SeqNo, leaf->getSeq());
Yingdi Yud514c172014-08-26 21:49:39 -0700108 entryLength += leaf->getSessionName().wireEncode(block);
109 entryLength += block.prependVarNumber(entryLength);
110 entryLength += block.prependVarNumber(tlv::StateLeaf);
111 totalLength += entryLength;
112 }
113
114 totalLength += block.prependVarNumber(totalLength);
115 totalLength += block.prependVarNumber(tlv::SyncReply);
116
117 return totalLength;
118}
119
Ashlesh Gawande097bb442017-05-31 13:38:00 -0500120//! \relates State
Yingdi Yud514c172014-08-26 21:49:39 -0700121template size_t
122State::wireEncode<true>(ndn::EncodingImpl<true>& block) const;
123
Ashlesh Gawande097bb442017-05-31 13:38:00 -0500124//! \relates State
Yingdi Yud514c172014-08-26 21:49:39 -0700125template size_t
126State::wireEncode<false>(ndn::EncodingImpl<false>& block) const;
127
128const Block&
129State::wireEncode() const
130{
131 if (m_wire.hasWire())
132 return m_wire;
133
134 ndn::EncodingEstimator estimator;
135 size_t estimatedSize = wireEncode(estimator);
136
137 ndn::EncodingBuffer buffer(estimatedSize, 0);
138 wireEncode(buffer);
139
140 m_wire = buffer.block();
141 return m_wire;
142}
143
144void
145State::wireDecode(const Block& wire)
146{
147 if (!wire.hasWire())
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800148 BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
Yingdi Yud514c172014-08-26 21:49:39 -0700149
150 if (wire.type() != tlv::SyncReply)
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800151 BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding SyncReply: " +
152 boost::lexical_cast<std::string>(m_wire.type())));
Yingdi Yud514c172014-08-26 21:49:39 -0700153
154 wire.parse();
155 m_wire = wire;
156
157 for (Block::element_const_iterator it = wire.elements_begin();
158 it != wire.elements_end(); it++) {
159 if (it->type() == tlv::StateLeaf) {
160 it->parse();
161
162 Block::element_const_iterator val = it->elements_begin();
163 Name info(*val);
164 val++;
165
166 if (val != it->elements_end())
167 update(info, readNonNegativeInteger(*val));
168 else
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800169 BOOST_THROW_EXCEPTION(Error("No seqNo when decoding SyncReply"));
Yingdi Yud514c172014-08-26 21:49:39 -0700170 }
171 }
172}
173
174} // namespace chronosync