Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 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. |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 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. |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 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> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef SYNC_STATE_H |
| 25 | #define SYNC_STATE_H |
| 26 | |
| 27 | #include "sync-state-leaf-container.h" |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 28 | #include <boost/exception/all.hpp> |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 29 | #include "boost/tuple/tuple.hpp" |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 30 | #include "sync-state.pb.h" |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 32 | /** |
| 33 | * \defgroup sync SYNC protocol |
| 34 | * |
| 35 | * Implementation of SYNC protocol |
| 36 | */ |
| 37 | namespace Sync { |
| 38 | |
Zhenkai Zhu | 396473c | 2012-09-25 17:02:54 -0700 | [diff] [blame] | 39 | /** |
| 40 | * \ingroup sync |
| 41 | * @brief this prefix will be used for the dummy node which increases its sequence number whenever |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 42 | * a remove operation happens; this is to prevent the reversion of root digest when we prune |
Zhenkai Zhu | 396473c | 2012-09-25 17:02:54 -0700 | [diff] [blame] | 43 | * a branch, i.e. help the root digest to be forward only |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 44 | * No corresponding data msg would be published and no attempt would be made to retrieve the |
Zhenkai Zhu | 396473c | 2012-09-25 17:02:54 -0700 | [diff] [blame] | 45 | * data msg |
| 46 | */ |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame] | 47 | const std::string forwarderPrefix = "/d0n0t18ak/t0ps8cr8t"; |
Zhenkai Zhu | 396473c | 2012-09-25 17:02:54 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 49 | class State; |
| 50 | typedef boost::shared_ptr<State> StatePtr; |
| 51 | typedef boost::shared_ptr<State> StateConstPtr; |
| 52 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 53 | /** |
| 54 | * \ingroup sync |
| 55 | * @brief Container for state leaves and definition of the abstract interface to work with State objects |
| 56 | */ |
| 57 | class State |
| 58 | { |
| 59 | public: |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 60 | virtual ~State () { }; |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 62 | /** |
| 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 Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 68 | virtual boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 69 | update (NameInfoConstPtr info, const SeqNo &seq) = 0; |
| 70 | |
| 71 | /** |
| 72 | * @brief Remove leaf from the state tree |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 73 | * @param info name of the leaf |
| 74 | */ |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 75 | virtual bool |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 76 | remove (NameInfoConstPtr info) = 0; |
| 77 | |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 78 | /** |
| 79 | * @brief Get state leaves |
| 80 | */ |
| 81 | const LeafContainer & |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 82 | getLeaves () const |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 83 | { return m_leaves; } |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 84 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 85 | protected: |
| 86 | LeafContainer m_leaves; |
| 87 | }; |
| 88 | |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 91 | * @brief Formats a protobuf SyncStateMsg msg |
| 92 | * @param oss output SyncStateMsg msg |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 93 | * @param state state |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 94 | * @returns output SyncStateMsg msg |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 95 | */ |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 96 | SyncStateMsg & |
| 97 | operator << (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 | */ |
| 106 | SyncStateMsg & |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 107 | operator >> (SyncStateMsg &issm, State &state); |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 109 | namespace Error { |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 110 | /** |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 111 | * @brief Will be thrown when data cannot be properly decoded to SyncStateMsg |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 112 | */ |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 113 | struct SyncStateMsgDecodingFailure : virtual boost::exception, virtual std::exception { }; |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 114 | } |
Alexander Afanasyev | 64d5069 | 2012-03-07 20:48:35 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 116 | } // Sync |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 117 | |
| 118 | #endif // SYNC_STATE_H |