blob: c6b3eadf0539e902e9f39ebdba0d981a110c3cf5 [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
Davide Pesavento5473abe2017-10-09 01:35:33 -040029State::~State() = default;
Yingdi Yud514c172014-08-26 21:49:39 -070030
31/**
32 * @brief Add or update leaf to the sync tree
33 *
34 * @param info session name of the leaf
35 * @param seq sequence number of the leaf
36 * @return 3-tuple (isInserted, isUpdated, oldSeqNo)
37 */
Ashlesh Gawande08784d42017-09-06 23:40:21 -050038std::tuple<bool, bool, SeqNo>
Yingdi Yud514c172014-08-26 21:49:39 -070039State::update(const Name& info, const SeqNo& seq)
40{
41 m_wire.reset();
42
43 LeafContainer::iterator leaf = m_leaves.find(info);
44
45 if (leaf == m_leaves.end()) {
46 m_leaves.insert(make_shared<Leaf>(info, cref(seq)));
47 return make_tuple(true, false, 0);
48 }
49 else {
50 if ((*leaf)->getSeq() == seq || seq < (*leaf)->getSeq()) {
51 return make_tuple(false, false, 0);
52 }
53
54 SeqNo old = (*leaf)->getSeq();
55 m_leaves.modify(leaf,
Yingdi Yu906c2ea2014-10-31 11:24:50 -070056 [=] (LeafPtr& leaf) { leaf->setSeq(seq); } );
Yingdi Yud514c172014-08-26 21:49:39 -070057 return make_tuple(false, true, old);
58 }
59}
60
Ashlesh Gawande08784d42017-09-06 23:40:21 -050061ConstBufferPtr
Yingdi Yud514c172014-08-26 21:49:39 -070062State::getRootDigest() const
63{
64 m_digest.reset();
65
66 BOOST_FOREACH (ConstLeafPtr leaf, m_leaves.get<ordered>())
67 {
68 BOOST_ASSERT(leaf != 0);
Davide Pesavento5473abe2017-10-09 01:35:33 -040069 m_digest.update(leaf->getDigest()->data(), leaf->getDigest()->size());
Yingdi Yud514c172014-08-26 21:49:39 -070070 }
71
72 return m_digest.computeDigest();
73}
74
75
76void
77State::reset()
78{
79 m_leaves.clear();
80}
81
82State&
83State::operator+=(const State& state)
84{
85 BOOST_FOREACH (ConstLeafPtr leaf, state.getLeaves())
86 {
87 BOOST_ASSERT(leaf != 0);
88 update(leaf->getSessionName(), leaf->getSeq());
89 }
90
91 return *this;
92}
93
Ashlesh Gawande08784d42017-09-06 23:40:21 -050094template<encoding::Tag T>
Yingdi Yud514c172014-08-26 21:49:39 -070095size_t
Ashlesh Gawande08784d42017-09-06 23:40:21 -050096State::wireEncode(encoding::EncodingImpl<T>& block) const
Yingdi Yud514c172014-08-26 21:49:39 -070097{
98 size_t totalLength = 0;
99
100 BOOST_REVERSE_FOREACH (ConstLeafPtr leaf, m_leaves.get<ordered>())
101 {
102 size_t entryLength = 0;
Alexander Afanasyevedaef7c2015-06-29 17:08:01 -0700103 entryLength += prependNonNegativeIntegerBlock(block, tlv::SeqNo, leaf->getSeq());
Yingdi Yud514c172014-08-26 21:49:39 -0700104 entryLength += leaf->getSessionName().wireEncode(block);
105 entryLength += block.prependVarNumber(entryLength);
106 entryLength += block.prependVarNumber(tlv::StateLeaf);
107 totalLength += entryLength;
108 }
109
110 totalLength += block.prependVarNumber(totalLength);
111 totalLength += block.prependVarNumber(tlv::SyncReply);
112
113 return totalLength;
114}
115
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500116NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(State);
Yingdi Yud514c172014-08-26 21:49:39 -0700117
118const Block&
119State::wireEncode() const
120{
121 if (m_wire.hasWire())
122 return m_wire;
123
124 ndn::EncodingEstimator estimator;
125 size_t estimatedSize = wireEncode(estimator);
126
127 ndn::EncodingBuffer buffer(estimatedSize, 0);
128 wireEncode(buffer);
129
130 m_wire = buffer.block();
131 return m_wire;
132}
133
134void
135State::wireDecode(const Block& wire)
136{
137 if (!wire.hasWire())
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800138 BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
Yingdi Yud514c172014-08-26 21:49:39 -0700139
140 if (wire.type() != tlv::SyncReply)
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800141 BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding SyncReply: " +
142 boost::lexical_cast<std::string>(m_wire.type())));
Yingdi Yud514c172014-08-26 21:49:39 -0700143
144 wire.parse();
145 m_wire = wire;
146
147 for (Block::element_const_iterator it = wire.elements_begin();
148 it != wire.elements_end(); it++) {
149 if (it->type() == tlv::StateLeaf) {
150 it->parse();
151
152 Block::element_const_iterator val = it->elements_begin();
153 Name info(*val);
154 val++;
155
156 if (val != it->elements_end())
157 update(info, readNonNegativeInteger(*val));
158 else
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800159 BOOST_THROW_EXCEPTION(Error("No seqNo when decoding SyncReply"));
Yingdi Yud514c172014-08-26 21:49:39 -0700160 }
161 }
162}
163
164} // namespace chronosync