Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2012-2017 University of California, Los Angeles |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 4 | * |
| 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 | |
| 27 | namespace chronosync { |
| 28 | |
Davide Pesavento | 5473abe | 2017-10-09 01:35:33 -0400 | [diff] [blame] | 29 | State::~State() = default; |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 30 | |
| 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 Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 38 | std::tuple<bool, bool, SeqNo> |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 39 | State::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 Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 56 | [=] (LeafPtr& leaf) { leaf->setSeq(seq); } ); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 57 | return make_tuple(false, true, old); |
| 58 | } |
| 59 | } |
| 60 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 61 | ConstBufferPtr |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 62 | State::getRootDigest() const |
| 63 | { |
| 64 | m_digest.reset(); |
| 65 | |
| 66 | BOOST_FOREACH (ConstLeafPtr leaf, m_leaves.get<ordered>()) |
| 67 | { |
| 68 | BOOST_ASSERT(leaf != 0); |
Davide Pesavento | 5473abe | 2017-10-09 01:35:33 -0400 | [diff] [blame] | 69 | m_digest.update(leaf->getDigest()->data(), leaf->getDigest()->size()); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return m_digest.computeDigest(); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | void |
| 77 | State::reset() |
| 78 | { |
| 79 | m_leaves.clear(); |
| 80 | } |
| 81 | |
| 82 | State& |
| 83 | State::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 Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 94 | template<encoding::Tag T> |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 95 | size_t |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 96 | State::wireEncode(encoding::EncodingImpl<T>& block) const |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 97 | { |
| 98 | size_t totalLength = 0; |
| 99 | |
| 100 | BOOST_REVERSE_FOREACH (ConstLeafPtr leaf, m_leaves.get<ordered>()) |
| 101 | { |
| 102 | size_t entryLength = 0; |
Alexander Afanasyev | edaef7c | 2015-06-29 17:08:01 -0700 | [diff] [blame] | 103 | entryLength += prependNonNegativeIntegerBlock(block, tlv::SeqNo, leaf->getSeq()); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 104 | 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 Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 116 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(State); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 117 | |
| 118 | const Block& |
| 119 | State::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 | |
| 134 | void |
| 135 | State::wireDecode(const Block& wire) |
| 136 | { |
| 137 | if (!wire.hasWire()) |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 138 | BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format")); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 139 | |
| 140 | if (wire.type() != tlv::SyncReply) |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 141 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding SyncReply: " + |
| 142 | boost::lexical_cast<std::string>(m_wire.type()))); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 143 | |
| 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 Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 159 | BOOST_THROW_EXCEPTION(Error("No seqNo when decoding SyncReply")); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | } // namespace chronosync |