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 | |
| 29 | using boost::make_tuple; |
| 30 | |
| 31 | State::~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 | */ |
| 42 | boost::tuple<bool, bool, SeqNo> |
| 43 | State::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 Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 60 | [=] (LeafPtr& leaf) { leaf->setSeq(seq); } ); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 61 | return make_tuple(false, true, old); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | ndn::ConstBufferPtr |
| 66 | State::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 | |
| 80 | void |
| 81 | State::reset() |
| 82 | { |
| 83 | m_leaves.clear(); |
| 84 | } |
| 85 | |
| 86 | State& |
| 87 | State::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 | |
| 98 | template<bool T> |
| 99 | size_t |
| 100 | State::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 Afanasyev | edaef7c | 2015-06-29 17:08:01 -0700 | [diff] [blame] | 107 | entryLength += prependNonNegativeIntegerBlock(block, tlv::SeqNo, leaf->getSeq()); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 108 | 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 Gawande | 097bb44 | 2017-05-31 13:38:00 -0500 | [diff] [blame] | 120 | //! \relates State |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 121 | template size_t |
| 122 | State::wireEncode<true>(ndn::EncodingImpl<true>& block) const; |
| 123 | |
Ashlesh Gawande | 097bb44 | 2017-05-31 13:38:00 -0500 | [diff] [blame] | 124 | //! \relates State |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 125 | template size_t |
| 126 | State::wireEncode<false>(ndn::EncodingImpl<false>& block) const; |
| 127 | |
| 128 | const Block& |
| 129 | State::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 | |
| 144 | void |
| 145 | State::wireDecode(const Block& wire) |
| 146 | { |
| 147 | if (!wire.hasWire()) |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 148 | BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format")); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 149 | |
| 150 | if (wire.type() != tlv::SyncReply) |
Alexander Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 151 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding SyncReply: " + |
| 152 | boost::lexical_cast<std::string>(m_wire.type()))); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 153 | |
| 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 Afanasyev | e9eda8a | 2017-03-09 14:40:03 -0800 | [diff] [blame] | 169 | BOOST_THROW_EXCEPTION(Error("No seqNo when decoding SyncReply")); |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | } // namespace chronosync |