blob: 7bbb60c521a070ef664278db5572e5209e4caf02 [file] [log] [blame]
Yingdi Yud514c172014-08-26 21:49:39 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento780e6462021-02-08 20:58:12 -05003 * Copyright (c) 2012-2021 University of California, Los Angeles
Yingdi Yud514c172014-08-26 21:49:39 -07004 *
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 Yud514c172014-08-26 21:49:39 -070028#include "leaf-container.hpp"
Ashlesh Gawande08784d42017-09-06 23:40:21 -050029
Ashlesh Gawande687cf922017-05-30 15:04:16 -050030#include <ndn-cxx/util/sha256.hpp>
Davide Pesavento780e6462021-02-08 20:58:12 -050031#include <tuple>
Yingdi Yud514c172014-08-26 21:49:39 -070032
33namespace chronosync {
34
35class State;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050036using StatePtr = shared_ptr<State>;
37using ConstStatePtr = shared_ptr<const State>;
Yingdi Yud514c172014-08-26 21:49:39 -070038
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 */
46class State
47{
48public:
49 class Error : public std::runtime_error
50 {
51 public:
Davide Pesavento780e6462021-02-08 20:58:12 -050052 using std::runtime_error::runtime_error;
Yingdi Yud514c172014-08-26 21:49:39 -070053 };
54
55 virtual
56 ~State();
57
58 /**
59 * @brief Add or update leaf to the sync tree
60 *
61 * @param info session name of the leaf
62 * @param seq sequence number of the leaf
63 * @return 3-tuple (isInserted, isUpdated, oldSeqNo)
64 */
Ashlesh Gawande08784d42017-09-06 23:40:21 -050065 std::tuple<bool, bool, SeqNo>
Yingdi Yud514c172014-08-26 21:49:39 -070066 update(const Name& info, const SeqNo& seq);
67
68 /**
69 * @brief Get state leaves
70 */
71 const LeafContainer&
72 getLeaves() const
73 {
74 return m_leaves;
75 }
76
Ashlesh Gawande08784d42017-09-06 23:40:21 -050077 ConstBufferPtr
Yingdi Yud514c172014-08-26 21:49:39 -070078 getRootDigest() const;
79
80 /**
81 * @brief Reset the sync tree, remove all state leaves
82 */
83 void
84 reset();
85
86 /**
87 * @brief Combine `this' state and the supplied state
88 *
89 * The combination result contains all leaves in two states.
90 * When leaves conflict, keep the one with largest seq.
91 *
92 * @param state another state to combine with
93 * @return Combined state
94 */
95 State&
96 operator+=(const State& state);
97
98 /**
99 * @brief Encode to a wire format
100 */
101 const Block&
102 wireEncode() const;
103
104 /**
105 * @brief Decode from the wire format
106 */
107 void
108 wireDecode(const Block& wire);
109
110protected:
Davide Pesavento780e6462021-02-08 20:58:12 -0500111 template<ndn::encoding::Tag T>
Yingdi Yud514c172014-08-26 21:49:39 -0700112 size_t
Davide Pesavento780e6462021-02-08 20:58:12 -0500113 wireEncode(ndn::encoding::EncodingImpl<T>& block) const;
Yingdi Yud514c172014-08-26 21:49:39 -0700114
115protected:
116 LeafContainer m_leaves;
117
118 mutable ndn::util::Sha256 m_digest;
119 mutable Block m_wire;
120};
121
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500122NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(State);
123
Yingdi Yud514c172014-08-26 21:49:39 -0700124} // namespace chronosync
125
126#endif // CHRONOSYNC_STATE_HPP