blob: 65d36744dc9ccdd52204344e5885066a5b11cc8b [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wange84adea2015-05-31 16:25:16 -07003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyev71b43e72012-12-27 01:03:43 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev71b43e72012-12-27 01:03:43 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Afanasyev71b43e72012-12-27 01:03:43 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Afanasyev71b43e72012-12-27 01:03:43 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Afanasyev71b43e72012-12-27 01:03:43 -080019 */
20
Lijing Wange84adea2015-05-31 16:25:16 -070021#ifndef CHRONOSHARE_SRC_SYNC_LOG_HPP
22#define CHRONOSHARE_SRC_SYNC_LOG_HPP
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080023
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "db-helper.hpp"
Lijing Wange84adea2015-05-31 16:25:16 -070025#include "sync-state.pb.h"
26#include "core/chronoshare-common.hpp"
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080027
Lijing Wange84adea2015-05-31 16:25:16 -070028#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
36namespace ndn {
37namespace chronoshare {
38
39typedef shared_ptr<SyncStateMsg> SyncStateMsgPtr;
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080040
Alexander Afanasyeva199f972013-01-02 19:37:26 -080041class SyncLog : public DbHelper
42{
43public:
Lijing Wange84adea2015-05-31 16:25:16 -070044 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 Afanasyev71b43e72012-12-27 01:03:43 -080055
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080056 /**
57 * @brief Get local username
58 */
Lijing Wange84adea2015-05-31 16:25:16 -070059 const Name&
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080060 GetLocalName() const;
Alexander Afanasyev7326a252013-01-20 23:43:25 -080061
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080062 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080063 GetNextLocalSeqNo(); // side effect: local seq_no will be increased
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080064
Alexander Afanasyeva199f972013-01-02 19:37:26 -080065 // done
66 void
Lijing Wange84adea2015-05-31 16:25:16 -070067 UpdateDeviceSeqNo(const Name& name, sqlite3_int64 seqNo);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080068
Alexander Afanasyev7326a252013-01-20 23:43:25 -080069 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080070 UpdateLocalSeqNo(sqlite3_int64 seqNo);
Alexander Afanasyev7326a252013-01-20 23:43:25 -080071
Lijing Wange84adea2015-05-31 16:25:16 -070072 Name
73 LookupLocator(const Name& deviceName);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080074
Lijing Wange84adea2015-05-31 16:25:16 -070075 Name
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 LookupLocalLocator();
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080077
Zhenkai Zhue851b952013-01-13 22:29:57 -080078 void
Lijing Wange84adea2015-05-31 16:25:16 -070079 UpdateLocator(const Name& deviceName, const Name& locator);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080080
Alexander Afanasyevdac84922013-01-20 23:32:17 -080081 void
Lijing Wange84adea2015-05-31 16:25:16 -070082 UpdateLocalLocator(const Name& locator);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080083
Alexander Afanasyeva199f972013-01-02 19:37:26 -080084 // done
85 /**
Lijing Wange84adea2015-05-31 16:25:16 -070086 * Create an 1ntry in SyncLog and SyncStateNodes corresponding to the current state of SyncNodes
Alexander Afanasyeva199f972013-01-02 19:37:26 -080087 */
Lijing Wange84adea2015-05-31 16:25:16 -070088 ConstBufferPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080089 RememberStateInStateLog();
Alexander Afanasyeva199f972013-01-02 19:37:26 -080090
91 // done
92 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080093 LookupSyncLog(const std::string& stateHash);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080094
95 // done
96 sqlite3_int64
Lijing Wange84adea2015-05-31 16:25:16 -070097 LookupSyncLog(const Buffer& stateHash);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080098
99 // How difference is exposed will be determined later by the actual protocol
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800100 SyncStateMsgPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101 FindStateDifferences(const std::string& oldHash, const std::string& newHash,
102 bool includeOldSeq = false);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800103
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -0800104 SyncStateMsgPtr
Lijing Wange84adea2015-05-31 16:25:16 -0700105 FindStateDifferences(const Buffer& oldHash, const Buffer& newHash, bool includeOldSeq = false);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800106
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800107 //-------- only used in test -----------------
108 sqlite3_int64
Lijing Wange84adea2015-05-31 16:25:16 -0700109 SeqNo(const Name& name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800110
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800111 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112 LogSize();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800113
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800114protected:
Zhenkai Zhue851b952013-01-13 22:29:57 -0800115 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800116 UpdateDeviceSeqNo(sqlite3_int64 deviceId, sqlite3_int64 seqNo);
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800117
Zhenkai Zhue851b952013-01-13 22:29:57 -0800118protected:
Lijing Wange84adea2015-05-31 16:25:16 -0700119 Name m_localName;
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800120
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800121 sqlite3_int64 m_localDeviceId;
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800122
123 typedef boost::mutex Mutex;
124 typedef boost::unique_lock<Mutex> WriteLock;
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800125
126 Mutex m_stateUpdateMutex;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800127};
128
Lijing Wange84adea2015-05-31 16:25:16 -0700129typedef shared_ptr<SyncLog> SyncLogPtr;
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800130
Lijing Wange84adea2015-05-31 16:25:16 -0700131inline const Name&
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800132SyncLog::GetLocalName() const
Alexander Afanasyev7326a252013-01-20 23:43:25 -0800133{
134 return m_localName;
135}
136
Lijing Wange84adea2015-05-31 16:25:16 -0700137} // namespace chronoshare
138} // namespace ndn
139
140#endif // CHRONOSHARE_SRC_SYNC_LOG_HPP