Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 21 | #ifndef CHRONOSHARE_SRC_SYNC_LOG_HPP |
| 22 | #define CHRONOSHARE_SRC_SYNC_LOG_HPP |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 24 | #include "db-helper.hpp" |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 25 | #include "sync-state.pb.h" |
| 26 | #include "core/chronoshare-common.hpp" |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 27 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/name.hpp> |
| 29 | |
| 30 | #include <map> |
| 31 | |
| 32 | // @todo Replace with std::thread |
| 33 | #include <boost/thread.hpp> |
| 34 | #include <boost/thread/shared_mutex.hpp> |
| 35 | |
| 36 | namespace ndn { |
| 37 | namespace chronoshare { |
| 38 | |
| 39 | typedef shared_ptr<SyncStateMsg> SyncStateMsgPtr; |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 41 | class SyncLog : public DbHelper |
| 42 | { |
| 43 | public: |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 44 | class Error : public DbHelper::Error |
| 45 | { |
| 46 | public: |
| 47 | explicit |
| 48 | Error(const std::string& what) |
| 49 | : DbHelper::Error(what) |
| 50 | { |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | SyncLog(const boost::filesystem::path& path, const Name& localName); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 56 | /** |
| 57 | * @brief Get local username |
| 58 | */ |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 59 | const Name& |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 60 | GetLocalName() const; |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 62 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 63 | GetNextLocalSeqNo(); // side effect: local seq_no will be increased |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 65 | // done |
| 66 | void |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 67 | UpdateDeviceSeqNo(const Name& name, sqlite3_int64 seqNo); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 69 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 70 | UpdateLocalSeqNo(sqlite3_int64 seqNo); |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 71 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 72 | Name |
| 73 | LookupLocator(const Name& deviceName); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 74 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 75 | Name |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | LookupLocalLocator(); |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 77 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 78 | void |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 79 | UpdateLocator(const Name& deviceName, const Name& locator); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 81 | void |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 82 | UpdateLocalLocator(const Name& locator); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 84 | // done |
| 85 | /** |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 86 | * Create an 1ntry in SyncLog and SyncStateNodes corresponding to the current state of SyncNodes |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 87 | */ |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 88 | ConstBufferPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 89 | RememberStateInStateLog(); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 90 | |
| 91 | // done |
| 92 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | LookupSyncLog(const std::string& stateHash); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 94 | |
| 95 | // done |
| 96 | sqlite3_int64 |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 97 | LookupSyncLog(const Buffer& stateHash); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 98 | |
| 99 | // How difference is exposed will be determined later by the actual protocol |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 100 | SyncStateMsgPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 101 | FindStateDifferences(const std::string& oldHash, const std::string& newHash, |
| 102 | bool includeOldSeq = false); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 104 | SyncStateMsgPtr |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 105 | FindStateDifferences(const Buffer& oldHash, const Buffer& newHash, bool includeOldSeq = false); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 106 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 107 | //-------- only used in test ----------------- |
| 108 | sqlite3_int64 |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 109 | SeqNo(const Name& name); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 111 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 112 | LogSize(); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 114 | protected: |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 115 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 116 | UpdateDeviceSeqNo(sqlite3_int64 deviceId, sqlite3_int64 seqNo); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 117 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 118 | protected: |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 119 | Name m_localName; |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 121 | sqlite3_int64 m_localDeviceId; |
Alexander Afanasyev | beee0b4 | 2013-01-16 18:25:08 -0800 | [diff] [blame] | 122 | |
| 123 | typedef boost::mutex Mutex; |
| 124 | typedef boost::unique_lock<Mutex> WriteLock; |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 125 | |
| 126 | Mutex m_stateUpdateMutex; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 129 | typedef shared_ptr<SyncLog> SyncLogPtr; |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 130 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 131 | inline const Name& |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 132 | SyncLog::GetLocalName() const |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 133 | { |
| 134 | return m_localName; |
| 135 | } |
| 136 | |
Lijing Wang | e84adea | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 137 | } // namespace chronoshare |
| 138 | } // namespace ndn |
| 139 | |
| 140 | #endif // CHRONOSHARE_SRC_SYNC_LOG_HPP |