blob: babbd69b67d5c5d0f328bc918106c20e068b5ee2 [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 Afanasyev6f70a0f2013-01-02 20:44:09 -080025#include <sync-state.pb.h>
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070026#include <ndnx-name.h>
Zhenkai Zhue851b952013-01-13 22:29:57 -080027#include <map>
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080028#include <boost/thread/shared_mutex.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 Afanasyev1dd37ed2013-08-14 18:08:09 -070035 SyncLog (const boost::filesystem::path &path, const Ndnx::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 Afanasyev1dd37ed2013-08-14 18:08:09 -070040 inline const Ndnx::Name &
Alexander Afanasyev7326a252013-01-20 23:43:25 -080041 GetLocalName () const;
42
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080043 sqlite3_int64
44 GetNextLocalSeqNo (); // side effect: local seq_no will be increased
45
Alexander Afanasyeva199f972013-01-02 19:37:26 -080046 // done
47 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070048 UpdateDeviceSeqNo (const Ndnx::Name &name, sqlite3_int64 seqNo);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080049
Alexander Afanasyev7326a252013-01-20 23:43:25 -080050 void
51 UpdateLocalSeqNo (sqlite3_int64 seqNo);
52
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070053 Ndnx::Name
54 LookupLocator (const Ndnx::Name &deviceName);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080055
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070056 Ndnx::Name
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080057 LookupLocalLocator ();
58
Zhenkai Zhue851b952013-01-13 22:29:57 -080059 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070060 UpdateLocator (const Ndnx::Name &deviceName, const Ndnx::Name &locator);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080061
Alexander Afanasyevdac84922013-01-20 23:32:17 -080062 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070063 UpdateLocalLocator (const Ndnx::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
70 RememberStateInStateLog ();
71
72 // done
73 sqlite3_int64
74 LookupSyncLog (const std::string &stateHash);
75
76 // done
77 sqlite3_int64
78 LookupSyncLog (const Hash &stateHash);
79
80 // How difference is exposed will be determined later by the actual protocol
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080081 SyncStateMsgPtr
Zhenkai Zhu085aae72013-01-17 21:09:01 -080082 FindStateDifferences (const std::string &oldHash, const std::string &newHash, bool includeOldSeq = false);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080083
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080084 SyncStateMsgPtr
Alexander Afanasyevdac84922013-01-20 23:32:17 -080085 FindStateDifferences (const Hash &oldHash, const Hash &newHash, bool includeOldSeq = false);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080086
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080087 //-------- only used in test -----------------
88 sqlite3_int64
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070089 SeqNo(const Ndnx::Name &name);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080090
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080091 sqlite3_int64
92 LogSize ();
93
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080094protected:
Zhenkai Zhue851b952013-01-13 22:29:57 -080095 void
96 UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080097
Zhenkai Zhue851b952013-01-13 22:29:57 -080098protected:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070099 Ndnx::Name m_localName;
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800100
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800101 sqlite3_int64 m_localDeviceId;
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800102
103 typedef boost::mutex Mutex;
104 typedef boost::unique_lock<Mutex> WriteLock;
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800105
106 Mutex m_stateUpdateMutex;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800107};
108
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800109typedef boost::shared_ptr<SyncLog> SyncLogPtr;
110
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700111const Ndnx::Name &
Alexander Afanasyev7326a252013-01-20 23:43:25 -0800112SyncLog::GetLocalName () const
113{
114 return m_localName;
115}
116
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800117#endif // SYNC_LOG_H