Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
| 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, |
| 60 | bind(&Leaf::setSeq, _1, seq)); |
| 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; |
| 107 | entryLength += prependNonNegativeIntegerBlock(block, tlv::SeqNo, leaf->getSeq()); |
| 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 | |
| 120 | template size_t |
| 121 | State::wireEncode<true>(ndn::EncodingImpl<true>& block) const; |
| 122 | |
| 123 | template size_t |
| 124 | State::wireEncode<false>(ndn::EncodingImpl<false>& block) const; |
| 125 | |
| 126 | const Block& |
| 127 | State::wireEncode() const |
| 128 | { |
| 129 | if (m_wire.hasWire()) |
| 130 | return m_wire; |
| 131 | |
| 132 | ndn::EncodingEstimator estimator; |
| 133 | size_t estimatedSize = wireEncode(estimator); |
| 134 | |
| 135 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 136 | wireEncode(buffer); |
| 137 | |
| 138 | m_wire = buffer.block(); |
| 139 | return m_wire; |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | State::wireDecode(const Block& wire) |
| 144 | { |
| 145 | if (!wire.hasWire()) |
| 146 | throw Error("The supplied block does not contain wire format"); |
| 147 | |
| 148 | if (wire.type() != tlv::SyncReply) |
| 149 | throw Error("Unexpected TLV type when decoding SyncReply: " + |
| 150 | boost::lexical_cast<std::string>(m_wire.type())); |
| 151 | |
| 152 | wire.parse(); |
| 153 | m_wire = wire; |
| 154 | |
| 155 | for (Block::element_const_iterator it = wire.elements_begin(); |
| 156 | it != wire.elements_end(); it++) { |
| 157 | if (it->type() == tlv::StateLeaf) { |
| 158 | it->parse(); |
| 159 | |
| 160 | Block::element_const_iterator val = it->elements_begin(); |
| 161 | Name info(*val); |
| 162 | val++; |
| 163 | |
| 164 | if (val != it->elements_end()) |
| 165 | update(info, readNonNegativeInteger(*val)); |
| 166 | else |
| 167 | throw Error("No seqNo when decoding SyncReply"); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } // namespace chronosync |