blob: 4119c959131b7270f3344a8f704082bd2f8f79ae [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * 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.
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * 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.
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * 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>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080022 */
23
24#ifndef SYNC_STATE_H
25#define SYNC_STATE_H
26
27#include "sync-state-leaf-container.h"
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080028#include <boost/exception/all.hpp>
Alexander Afanasyev750d1872012-03-12 15:33:56 -070029#include "boost/tuple/tuple.hpp"
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070030#include "sync-state.pb.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080031
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080032/**
33 * \defgroup sync SYNC protocol
34 *
35 * Implementation of SYNC protocol
36 */
37namespace Sync {
38
Zhenkai Zhu396473c2012-09-25 17:02:54 -070039/**
40 * \ingroup sync
41 * @brief this prefix will be used for the dummy node which increases its sequence number whenever
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070042 * a remove operation happens; this is to prevent the reversion of root digest when we prune
Zhenkai Zhu396473c2012-09-25 17:02:54 -070043 * a branch, i.e. help the root digest to be forward only
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070044 * No corresponding data msg would be published and no attempt would be made to retrieve the
Zhenkai Zhu396473c2012-09-25 17:02:54 -070045 * data msg
46 */
Zhenkai Zhua2e0b082012-09-26 10:34:15 -070047const std::string forwarderPrefix = "/d0n0t18ak/t0ps8cr8t";
Zhenkai Zhu396473c2012-09-25 17:02:54 -070048
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070049class State;
50typedef boost::shared_ptr<State> StatePtr;
51typedef boost::shared_ptr<State> StateConstPtr;
52
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080053/**
54 * \ingroup sync
55 * @brief Container for state leaves and definition of the abstract interface to work with State objects
56 */
57class State
58{
59public:
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080060 virtual ~State () { };
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070061
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080062 /**
63 * @brief Add or update leaf to the state tree
64 *
65 * @param info name of the leaf
66 * @param seq sequence number of the leaf
67 */
Alexander Afanasyev750d1872012-03-12 15:33:56 -070068 virtual boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080069 update (NameInfoConstPtr info, const SeqNo &seq) = 0;
70
71 /**
72 * @brief Remove leaf from the state tree
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080073 * @param info name of the leaf
74 */
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070075 virtual bool
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080076 remove (NameInfoConstPtr info) = 0;
77
Alexander Afanasyeva5625322012-03-06 00:03:41 -080078 /**
79 * @brief Get state leaves
80 */
81 const LeafContainer &
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070082 getLeaves () const
Alexander Afanasyeva5625322012-03-06 00:03:41 -080083 { return m_leaves; }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070084
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080085protected:
86 LeafContainer m_leaves;
87};
88
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080089
90/**
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070091 * @brief Formats a protobuf SyncStateMsg msg
92 * @param oss output SyncStateMsg msg
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080093 * @param state state
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070094 * @returns output SyncStateMsg msg
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080095 */
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070096SyncStateMsg &
97operator << (SyncStateMsg &ossm, const State &state);
98
99
100/**
101 * @brief Parse a protobuf SyncStateMsg msg
102 * @param iss input SyncStateMsg msg
103 * @param state state
104 * @returns SyncStateMsg msg
105 */
106SyncStateMsg &
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700107operator >> (SyncStateMsg &issm, State &state);
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800108
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800109namespace Error {
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800110/**
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700111 * @brief Will be thrown when data cannot be properly decoded to SyncStateMsg
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800112 */
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700113struct SyncStateMsgDecodingFailure : virtual boost::exception, virtual std::exception { };
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800114}
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800115
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800116} // Sync
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800117
118#endif // SYNC_STATE_H