blob: 263ec83bd9d0d8f9b32ea86c071642179601692d [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wang51837ad2016-12-25 14:43:53 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyeva199f972013-01-02 19:37:26 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyeva199f972013-01-02 19:37:26 -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 Afanasyeva199f972013-01-02 19:37:26 -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 Afanasyeva199f972013-01-02 19:37:26 -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 Afanasyeva199f972013-01-02 19:37:26 -080019 */
20
Lijing Wang51837ad2016-12-25 14:43:53 -080021#ifndef CHRONOSHARE_SRC_ACTION_LOG_HPP
22#define CHRONOSHARE_SRC_ACTION_LOG_HPP
Alexander Afanasyeva199f972013-01-02 19:37:26 -080023
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "db-helper.hpp"
25#include "file-state.hpp"
26#include "sync-log.hpp"
Lijing Wang51837ad2016-12-25 14:43:53 -080027#include "core/chronoshare-common.hpp"
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080028
Lijing Wang51837ad2016-12-25 14:43:53 -080029#include "action-item.pb.h"
30#include "file-item.pb.h"
31
32#include <ndn-cxx/face.hpp>
33#include <ndn-cxx/security/key-chain.hpp>
34
35namespace ndn {
36namespace chronoshare {
Alexander Afanasyeva199f972013-01-02 19:37:26 -080037
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080038class ActionLog;
Lijing Wang51837ad2016-12-25 14:43:53 -080039typedef shared_ptr<ActionLog> ActionLogPtr;
40typedef shared_ptr<ActionItem> ActionItemPtr;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080041
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080042class ActionLog : public DbHelper
Alexander Afanasyeva199f972013-01-02 19:37:26 -080043{
44public:
Lijing Wang51837ad2016-12-25 14:43:53 -080045 class Error : public DbHelper::Error
46 {
47 public:
48 explicit Error(const std::string& what)
49 : DbHelper::Error(what)
50 {
51 }
52 };
53
54public:
55 typedef function<void(std::string /*filename*/, Name /*device_name*/, sqlite3_int64 /*seq_no*/,
56 ConstBufferPtr /*hash*/, time_t /*m_time*/, int /*mode*/, int /*seg_num*/)>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080057 OnFileAddedOrChangedCallback;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080058
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059 typedef boost::function<void(std::string /*filename*/)> OnFileRemovedCallback;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080060
61public:
Lijing Wang51837ad2016-12-25 14:43:53 -080062 ActionLog(Face& face, const boost::filesystem::path& path, SyncLogPtr syncLog,
Lijing Wang8e56d082016-12-25 14:45:23 -080063 const std::string& sharedFolder, const name::Component& appName,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080064 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080065
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080066 virtual ~ActionLog()
67 {
68 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080069
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080070 //////////////////////////
71 // Local operations //
72 //////////////////////////
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080073 ActionItemPtr
Lijing Wang51837ad2016-12-25 14:43:53 -080074 AddLocalActionUpdate(const std::string& filename, const Buffer& hash, time_t wtime, int mode,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080075 int seg_num);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080076
77 // void
Lijing Wang51837ad2016-12-25 14:43:53 -080078 // AddActionMove(const std::string &oldFile, const std::string &newFile);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080079
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080080 ActionItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080081 AddLocalActionDelete(const std::string& filename);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080082
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080083 //////////////////////////
84 // Remote operations //
85 //////////////////////////
86
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080087 ActionItemPtr
Lijing Wang51837ad2016-12-25 14:43:53 -080088 AddRemoteAction(const Name& deviceName, sqlite3_int64 seqno, shared_ptr<Data> actionData);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080089
90 /**
91 * @brief Add remote action using just action's parsed content object
92 *
Lijing Wang51837ad2016-12-25 14:43:53 -080093 * This function extracts device name and sequence number from the content object's and calls the
94 * overloaded method
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080095 */
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080096 ActionItemPtr
Lijing Wang51837ad2016-12-25 14:43:53 -080097 AddRemoteAction(shared_ptr<Data> actionData);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080098
99 ///////////////////////////
100 // General operations //
101 ///////////////////////////
102
Lijing Wang51837ad2016-12-25 14:43:53 -0800103 shared_ptr<Data>
104 LookupActionData(const Name& deviceName, sqlite3_int64 seqno);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800105
Lijing Wang51837ad2016-12-25 14:43:53 -0800106 shared_ptr<Data>
107 LookupActionData(const Name& actionName);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800108
109 ActionItemPtr
Lijing Wang51837ad2016-12-25 14:43:53 -0800110 LookupAction(const Name& deviceName, sqlite3_int64 seqno);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800111
112 ActionItemPtr
Lijing Wang51837ad2016-12-25 14:43:53 -0800113 LookupAction(const Name& actionName);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800114
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800115 FileItemPtr
Lijing Wang51837ad2016-12-25 14:43:53 -0800116 LookupAction(const std::string& filename, sqlite3_int64 version, const Buffer& filehash);
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800117
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800118 /**
Lijing Wang51837ad2016-12-25 14:43:53 -0800119 * @brief Lookup up to [limit] actions starting [offset] in decreasing order(by timestamp) and
120 * calling visitor(device_name,seqno,action) for each action
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800121 */
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800122 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800123 LookupActionsInFolderRecursively(
Lijing Wang51837ad2016-12-25 14:43:53 -0800124 const function<void(const Name& name, sqlite3_int64 seq_no, const ActionItem&)>& visitor,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800125 const std::string& folder, int offset = 0, int limit = -1);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800126
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800127 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800128 LookupActionsForFile(
Lijing Wang51837ad2016-12-25 14:43:53 -0800129 const function<void(const Name& name, sqlite3_int64 seq_no, const ActionItem&)>& visitor,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800130 const std::string& file, int offset = 0, int limit = -1);
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800131
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800132 void
Lijing Wang51837ad2016-12-25 14:43:53 -0800133 LookupRecentFileActions(const function<void(const std::string&, int, int)>& visitor, int limit = 5);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800134
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800135 //
136 inline FileStatePtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800137 GetFileState();
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800138
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800139public:
140 // for test purposes
141 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800142 LogSize();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800143
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800144private:
Lijing Wang51837ad2016-12-25 14:43:53 -0800145 std::tuple<sqlite3_int64 /*version*/, BufferPtr /*device name*/, sqlite3_int64 /*seq_no*/>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800146 GetLatestActionForFile(const std::string& filename);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800147
148 static void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800149 apply_action_xFun(sqlite3_context* context, int argc, sqlite3_value** argv);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800150
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800151private:
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800152 SyncLogPtr m_syncLog;
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800153 FileStatePtr m_fileState;
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800154
Lijing Wang51837ad2016-12-25 14:43:53 -0800155 // Face& m_face;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800156 std::string m_sharedFolderName;
Lijing Wang8e56d082016-12-25 14:45:23 -0800157 name::Component m_appName;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800158
159 OnFileAddedOrChangedCallback m_onFileAddedOrChanged;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800160 OnFileRemovedCallback m_onFileRemoved;
Lijing Wang51837ad2016-12-25 14:43:53 -0800161 KeyChain m_keyChain;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800162};
163
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800164inline FileStatePtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800165ActionLog::GetFileState()
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800166{
167 return m_fileState;
168}
169
Lijing Wang51837ad2016-12-25 14:43:53 -0800170} // namespace chronoshare
171} // namespace ndn
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800172
Lijing Wang51837ad2016-12-25 14:43:53 -0800173#endif // CHRONOSHARE_SRC_ACTION_LOG_HPP