Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, 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 | |
| 21 | #ifndef SYNC_LOG_H |
| 22 | #define SYNC_LOG_H |
| 23 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 24 | #include "db-helper.hpp" |
Alexander Afanasyev | beee0b4 | 2013-01-16 18:25:08 -0800 | [diff] [blame] | 25 | #include <boost/thread/shared_mutex.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 26 | #include <ccnx-name.h> |
| 27 | #include <map> |
| 28 | #include <sync-state.pb.hpp> |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 29 | |
| 30 | typedef boost::shared_ptr<SyncStateMsg> SyncStateMsgPtr; |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 32 | class SyncLog : public DbHelper |
| 33 | { |
| 34 | public: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 35 | SyncLog(const boost::filesystem::path& path, const Ccnx::Name& localName); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 37 | /** |
| 38 | * @brief Get local username |
| 39 | */ |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 40 | inline const Ccnx::Name& |
| 41 | GetLocalName() const; |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 43 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 44 | GetNextLocalSeqNo(); // side effect: local seq_no will be increased |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 46 | // done |
| 47 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 48 | UpdateDeviceSeqNo(const Ccnx::Name& name, sqlite3_int64 seqNo); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 50 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 51 | UpdateLocalSeqNo(sqlite3_int64 seqNo); |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 53 | Ccnx::Name |
| 54 | LookupLocator(const Ccnx::Name& deviceName); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 56 | Ccnx::Name |
| 57 | LookupLocalLocator(); |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 58 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 59 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 60 | UpdateLocator(const Ccnx::Name& deviceName, const Ccnx::Name& locator); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 62 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 63 | UpdateLocalLocator(const Ccnx::Name& locator); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 65 | // done |
| 66 | /** |
| 67 | * Create an entry in SyncLog and SyncStateNodes corresponding to the current state of SyncNodes |
| 68 | */ |
| 69 | HashPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 70 | RememberStateInStateLog(); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 71 | |
| 72 | // done |
| 73 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 74 | LookupSyncLog(const std::string& stateHash); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 75 | |
| 76 | // done |
| 77 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 78 | LookupSyncLog(const Hash& stateHash); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 79 | |
| 80 | // How difference is exposed will be determined later by the actual protocol |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 81 | SyncStateMsgPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 82 | FindStateDifferences(const std::string& oldHash, const std::string& newHash, |
| 83 | bool includeOldSeq = false); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 85 | SyncStateMsgPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 86 | FindStateDifferences(const Hash& oldHash, const Hash& newHash, bool includeOldSeq = false); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 87 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 88 | //-------- only used in test ----------------- |
| 89 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 90 | SeqNo(const Ccnx::Name& name); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 92 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 93 | LogSize(); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 95 | protected: |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 96 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 97 | UpdateDeviceSeqNo(sqlite3_int64 deviceId, sqlite3_int64 seqNo); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 98 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 99 | protected: |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 100 | Ndnx::Name m_localName; |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 102 | sqlite3_int64 m_localDeviceId; |
Alexander Afanasyev | beee0b4 | 2013-01-16 18:25:08 -0800 | [diff] [blame] | 103 | |
| 104 | typedef boost::mutex Mutex; |
| 105 | typedef boost::unique_lock<Mutex> WriteLock; |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 106 | |
| 107 | Mutex m_stateUpdateMutex; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 110 | typedef boost::shared_ptr<SyncLog> SyncLogPtr; |
| 111 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 112 | const Ccnx::Name& |
| 113 | SyncLog::GetLocalName() const |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame] | 114 | { |
| 115 | return m_localName; |
| 116 | } |
| 117 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 118 | #endif // SYNC_LOG_H |