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