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 | |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 29 | State::~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 Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 40 | std::tuple<bool, bool, SeqNo> |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 41 | State::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 Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 58 | [=] (LeafPtr& leaf) { leaf->setSeq(seq); } ); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 59 | return make_tuple(false, true, old); |
| 60 | } |
| 61 | } |
| 62 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 63 | ConstBufferPtr |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 64 | State::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 | |
| 78 | void |
| 79 | State::reset() |
| 80 | { |
| 81 | m_leaves.clear(); |
| 82 | } |
| 83 | |
| 84 | State& |
| 85 | State::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 Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 96 | template<encoding::Tag T> |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 97 | size_t |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 98 | State::wireEncode(encoding::EncodingImpl<T>& block) const |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 99 | { |
| 100 | size_t totalLength = 0; |
| 101 | |
| 102 | BOOST_REVERSE_FOREACH (ConstLeafPtr leaf, m_leaves.get<ordered>()) |
| 103 | { |
| 104 | size_t entryLength = 0; |
Alexander Afanasyev | edaef7c | 2015-06-29 17:08:01 -0700 | [diff] [blame] | 105 | entryLength += prependNonNegativeIntegerBlock(block, tlv::SeqNo, leaf->getSeq()); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 106 | 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 Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 118 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(State); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 119 | |
| 120 | const Block& |
| 121 | State::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 | |
| 136 | void |
| 137 | State::wireDecode(const Block& wire) |
| 138 | { |
| 139 | if (!wire.hasWire()) |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 140 | BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format")); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 141 | |
| 142 | if (wire.type() != tlv::SyncReply) |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 143 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding SyncReply: " + |
| 144 | boost::lexical_cast<std::string>(m_wire.type()))); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 145 | |
| 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 Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 161 | BOOST_THROW_EXCEPTION(Error("No seqNo when decoding SyncReply")); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | } // namespace chronosync |