blob: e5d0de2173cbbd6038348052bca6e822fc4a58f4 [file] [log] [blame]
Yingdi Yud514c172014-08-26 21:49:39 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawande687cf922017-05-30 15:04:16 -05003 * Copyright (c) 2012-2017 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#include "tlv.hpp"
30
Ashlesh Gawande687cf922017-05-30 15:04:16 -050031#include <ndn-cxx/util/sha256.hpp>
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:
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 Gawande08784d42017-09-06 23:40:21 -050069 std::tuple<bool, bool, SeqNo>
Yingdi Yud514c172014-08-26 21:49:39 -070070 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 Gawande08784d42017-09-06 23:40:21 -050081 ConstBufferPtr
Yingdi Yud514c172014-08-26 21:49:39 -070082 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
114protected:
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500115 template<encoding::Tag T>
Yingdi Yud514c172014-08-26 21:49:39 -0700116 size_t
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500117 wireEncode(encoding::EncodingImpl<T>& block) const;
Yingdi Yud514c172014-08-26 21:49:39 -0700118
119protected:
120 LeafContainer m_leaves;
121
122 mutable ndn::util::Sha256 m_digest;
123 mutable Block m_wire;
124};
125
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500126NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(State);
127
Yingdi Yud514c172014-08-26 21:49:39 -0700128} // namespace chronosync
129
130#endif // CHRONOSYNC_STATE_HPP