blob: b89509b637898f46d5e380d53b816c00b6ecbb7c [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
Yingdi Yud514c172014-08-26 21:49:39 -070029State::~State()
30{
31}
32
33/**
34 * @brief Add or update leaf to the sync tree
35 *
36 * @param info session name of the leaf
37 * @param seq sequence number of the leaf
38 * @return 3-tuple (isInserted, isUpdated, oldSeqNo)
39 */
Ashlesh Gawande08784d42017-09-06 23:40:21 -050040std::tuple<bool, bool, SeqNo>
Yingdi Yud514c172014-08-26 21:49:39 -070041State::update(const Name& info, const SeqNo& seq)
42{
43 m_wire.reset();
44
45 LeafContainer::iterator leaf = m_leaves.find(info);
46
47 if (leaf == m_leaves.end()) {
48 m_leaves.insert(make_shared<Leaf>(info, cref(seq)));
49 return make_tuple(true, false, 0);
50 }
51 else {
52 if ((*leaf)->getSeq() == seq || seq < (*leaf)->getSeq()) {
53 return make_tuple(false, false, 0);
54 }
55
56 SeqNo old = (*leaf)->getSeq();
57 m_leaves.modify(leaf,
Yingdi Yu906c2ea2014-10-31 11:24:50 -070058 [=] (LeafPtr& leaf) { leaf->setSeq(seq); } );
Yingdi Yud514c172014-08-26 21:49:39 -070059 return make_tuple(false, true, old);
60 }
61}
62
Ashlesh Gawande08784d42017-09-06 23:40:21 -050063ConstBufferPtr
Yingdi Yud514c172014-08-26 21:49:39 -070064State::getRootDigest() const
65{
66 m_digest.reset();
67
68 BOOST_FOREACH (ConstLeafPtr leaf, m_leaves.get<ordered>())
69 {
70 BOOST_ASSERT(leaf != 0);
71 m_digest.update(leaf->getDigest()->buf(), leaf->getDigest()->size());
72 }
73
74 return m_digest.computeDigest();
75}
76
77
78void
79State::reset()
80{
81 m_leaves.clear();
82}
83
84State&
85State::operator+=(const State& state)
86{
87 BOOST_FOREACH (ConstLeafPtr leaf, state.getLeaves())
88 {
89 BOOST_ASSERT(leaf != 0);
90 update(leaf->getSessionName(), leaf->getSeq());
91 }
92
93 return *this;
94}
95
Ashlesh Gawande08784d42017-09-06 23:40:21 -050096template<encoding::Tag T>
Yingdi Yud514c172014-08-26 21:49:39 -070097size_t
Ashlesh Gawande08784d42017-09-06 23:40:21 -050098State::wireEncode(encoding::EncodingImpl<T>& block) const
Yingdi Yud514c172014-08-26 21:49:39 -070099{
100 size_t totalLength = 0;
101
102 BOOST_REVERSE_FOREACH (ConstLeafPtr leaf, m_leaves.get<ordered>())
103 {
104 size_t entryLength = 0;
Alexander Afanasyevedaef7c2015-06-29 17:08:01 -0700105 entryLength += prependNonNegativeIntegerBlock(block, tlv::SeqNo, leaf->getSeq());
Yingdi Yud514c172014-08-26 21:49:39 -0700106 entryLength += leaf->getSessionName().wireEncode(block);
107 entryLength += block.prependVarNumber(entryLength);
108 entryLength += block.prependVarNumber(tlv::StateLeaf);
109 totalLength += entryLength;
110 }
111
112 totalLength += block.prependVarNumber(totalLength);
113 totalLength += block.prependVarNumber(tlv::SyncReply);
114
115 return totalLength;
116}
117
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500118NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(State);
Yingdi Yud514c172014-08-26 21:49:39 -0700119
120const Block&
121State::wireEncode() const
122{
123 if (m_wire.hasWire())
124 return m_wire;
125
126 ndn::EncodingEstimator estimator;
127 size_t estimatedSize = wireEncode(estimator);
128
129 ndn::EncodingBuffer buffer(estimatedSize, 0);
130 wireEncode(buffer);
131
132 m_wire = buffer.block();
133 return m_wire;
134}
135
136void
137State::wireDecode(const Block& wire)
138{
139 if (!wire.hasWire())
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800140 BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
Yingdi Yud514c172014-08-26 21:49:39 -0700141
142 if (wire.type() != tlv::SyncReply)
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800143 BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding SyncReply: " +
144 boost::lexical_cast<std::string>(m_wire.type())));
Yingdi Yud514c172014-08-26 21:49:39 -0700145
146 wire.parse();
147 m_wire = wire;
148
149 for (Block::element_const_iterator it = wire.elements_begin();
150 it != wire.elements_end(); it++) {
151 if (it->type() == tlv::StateLeaf) {
152 it->parse();
153
154 Block::element_const_iterator val = it->elements_begin();
155 Name info(*val);
156 val++;
157
158 if (val != it->elements_end())
159 update(info, readNonNegativeInteger(*val));
160 else
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800161 BOOST_THROW_EXCEPTION(Error("No seqNo when decoding SyncReply"));
Yingdi Yud514c172014-08-26 21:49:39 -0700162 }
163 }
164}
165
166} // namespace chronosync