Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [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 | #ifndef CHRONOSYNC_STATE_HPP |
| 26 | #define CHRONOSYNC_STATE_HPP |
| 27 | |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 28 | #include "leaf-container.hpp" |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 29 | #include "tlv.hpp" |
| 30 | |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/util/sha256.hpp> |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 32 | |
| 33 | namespace chronosync { |
| 34 | |
| 35 | class State; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 36 | using StatePtr = shared_ptr<State>; |
| 37 | using ConstStatePtr = shared_ptr<const State>; |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * @brief Abstraction of state tree. |
| 41 | * |
| 42 | * State is used to represent sync tree, it is also the base class of DiffState, |
| 43 | * which represent the diff between two states. Due to the second usage, State |
| 44 | * should be copyable. |
| 45 | */ |
| 46 | class State |
| 47 | { |
| 48 | public: |
| 49 | class Error : public std::runtime_error |
| 50 | { |
| 51 | public: |
| 52 | explicit |
| 53 | Error(const std::string& what) |
| 54 | : std::runtime_error(what) |
| 55 | { |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | virtual |
| 60 | ~State(); |
| 61 | |
| 62 | /** |
| 63 | * @brief Add or update leaf to the sync tree |
| 64 | * |
| 65 | * @param info session name of the leaf |
| 66 | * @param seq sequence number of the leaf |
| 67 | * @return 3-tuple (isInserted, isUpdated, oldSeqNo) |
| 68 | */ |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 69 | std::tuple<bool, bool, SeqNo> |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 70 | update(const Name& info, const SeqNo& seq); |
| 71 | |
| 72 | /** |
| 73 | * @brief Get state leaves |
| 74 | */ |
| 75 | const LeafContainer& |
| 76 | getLeaves() const |
| 77 | { |
| 78 | return m_leaves; |
| 79 | } |
| 80 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 81 | ConstBufferPtr |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 82 | getRootDigest() const; |
| 83 | |
| 84 | /** |
| 85 | * @brief Reset the sync tree, remove all state leaves |
| 86 | */ |
| 87 | void |
| 88 | reset(); |
| 89 | |
| 90 | /** |
| 91 | * @brief Combine `this' state and the supplied state |
| 92 | * |
| 93 | * The combination result contains all leaves in two states. |
| 94 | * When leaves conflict, keep the one with largest seq. |
| 95 | * |
| 96 | * @param state another state to combine with |
| 97 | * @return Combined state |
| 98 | */ |
| 99 | State& |
| 100 | operator+=(const State& state); |
| 101 | |
| 102 | /** |
| 103 | * @brief Encode to a wire format |
| 104 | */ |
| 105 | const Block& |
| 106 | wireEncode() const; |
| 107 | |
| 108 | /** |
| 109 | * @brief Decode from the wire format |
| 110 | */ |
| 111 | void |
| 112 | wireDecode(const Block& wire); |
| 113 | |
| 114 | protected: |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 115 | template<encoding::Tag T> |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 116 | size_t |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 117 | wireEncode(encoding::EncodingImpl<T>& block) const; |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 118 | |
| 119 | protected: |
| 120 | LeafContainer m_leaves; |
| 121 | |
| 122 | mutable ndn::util::Sha256 m_digest; |
| 123 | mutable Block m_wire; |
| 124 | }; |
| 125 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 126 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(State); |
| 127 | |
Yingdi Yu | d514c17 | 2014-08-26 21:49:39 -0700 | [diff] [blame] | 128 | } // namespace chronosync |
| 129 | |
| 130 | #endif // CHRONOSYNC_STATE_HPP |