blob: e757a0cb92f24d06314cd2032b308b8a19120ad5 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, 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
21#ifndef SYNC_LOG_H
22#define SYNC_LOG_H
23
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "db-helper.hpp"
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080025#include <boost/thread/shared_mutex.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080026#include <ccnx-name.h>
27#include <map>
28#include <sync-state.pb.hpp>
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080029
30typedef boost::shared_ptr<SyncStateMsg> SyncStateMsgPtr;
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080031
Alexander Afanasyeva199f972013-01-02 19:37:26 -080032class SyncLog : public DbHelper
33{
34public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080035 SyncLog(const boost::filesystem::path& path, const Ccnx::Name& localName);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080036
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080037 /**
38 * @brief Get local username
39 */
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080040 inline const Ccnx::Name&
41 GetLocalName() const;
Alexander Afanasyev7326a252013-01-20 23:43:25 -080042
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080043 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080044 GetNextLocalSeqNo(); // side effect: local seq_no will be increased
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080045
Alexander Afanasyeva199f972013-01-02 19:37:26 -080046 // done
47 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048 UpdateDeviceSeqNo(const Ccnx::Name& name, sqlite3_int64 seqNo);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080049
Alexander Afanasyev7326a252013-01-20 23:43:25 -080050 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080051 UpdateLocalSeqNo(sqlite3_int64 seqNo);
Alexander Afanasyev7326a252013-01-20 23:43:25 -080052
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080053 Ccnx::Name
54 LookupLocator(const Ccnx::Name& deviceName);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080055
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056 Ccnx::Name
57 LookupLocalLocator();
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080058
Zhenkai Zhue851b952013-01-13 22:29:57 -080059 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080060 UpdateLocator(const Ccnx::Name& deviceName, const Ccnx::Name& locator);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080061
Alexander Afanasyevdac84922013-01-20 23:32:17 -080062 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080063 UpdateLocalLocator(const Ccnx::Name& locator);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080064
Alexander Afanasyeva199f972013-01-02 19:37:26 -080065 // done
66 /**
67 * Create an entry in SyncLog and SyncStateNodes corresponding to the current state of SyncNodes
68 */
69 HashPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080070 RememberStateInStateLog();
Alexander Afanasyeva199f972013-01-02 19:37:26 -080071
72 // done
73 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080074 LookupSyncLog(const std::string& stateHash);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080075
76 // done
77 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078 LookupSyncLog(const Hash& stateHash);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080079
80 // How difference is exposed will be determined later by the actual protocol
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080081 SyncStateMsgPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080082 FindStateDifferences(const std::string& oldHash, const std::string& newHash,
83 bool includeOldSeq = false);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080084
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080085 SyncStateMsgPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080086 FindStateDifferences(const Hash& oldHash, const Hash& newHash, bool includeOldSeq = false);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080087
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080088 //-------- only used in test -----------------
89 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080090 SeqNo(const Ccnx::Name& name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080091
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080092 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080093 LogSize();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080094
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080095protected:
Zhenkai Zhue851b952013-01-13 22:29:57 -080096 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 UpdateDeviceSeqNo(sqlite3_int64 deviceId, sqlite3_int64 seqNo);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080098
Zhenkai Zhue851b952013-01-13 22:29:57 -080099protected:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700100 Ndnx::Name m_localName;
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800101
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800102 sqlite3_int64 m_localDeviceId;
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800103
104 typedef boost::mutex Mutex;
105 typedef boost::unique_lock<Mutex> WriteLock;
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800106
107 Mutex m_stateUpdateMutex;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800108};
109
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800110typedef boost::shared_ptr<SyncLog> SyncLogPtr;
111
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112const Ccnx::Name&
113SyncLog::GetLocalName() const
Alexander Afanasyev7326a252013-01-20 23:43:25 -0800114{
115 return m_localName;
116}
117
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800118#endif // SYNC_LOG_H