Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -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 | a199f97 | 2013-01-02 19:37:26 -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 | a199f97 | 2013-01-02 19:37:26 -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 | a199f97 | 2013-01-02 19:37:26 -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 | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 21 | #ifndef CHRONOSHARE_SRC_ACTION_LOG_HPP |
| 22 | #define CHRONOSHARE_SRC_ACTION_LOG_HPP |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 24 | #include "db-helper.hpp" |
| 25 | #include "file-state.hpp" |
| 26 | #include "sync-log.hpp" |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 27 | #include "core/chronoshare-common.hpp" |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 28 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 29 | #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 | |
| 35 | namespace ndn { |
| 36 | namespace chronoshare { |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 38 | class ActionLog; |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 39 | typedef shared_ptr<ActionLog> ActionLogPtr; |
| 40 | typedef shared_ptr<ActionItem> ActionItemPtr; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 42 | class ActionLog : public DbHelper |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 43 | { |
| 44 | public: |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 45 | class Error : public DbHelper::Error |
| 46 | { |
| 47 | public: |
| 48 | explicit Error(const std::string& what) |
| 49 | : DbHelper::Error(what) |
| 50 | { |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | public: |
| 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | OnFileAddedOrChangedCallback; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | typedef boost::function<void(std::string /*filename*/)> OnFileRemovedCallback; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 60 | |
| 61 | public: |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 62 | ActionLog(Face& face, const boost::filesystem::path& path, SyncLogPtr syncLog, |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 63 | const std::string& sharedFolder, const name::Component& appName, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 64 | OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 66 | virtual ~ActionLog() |
| 67 | { |
| 68 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 70 | ////////////////////////// |
| 71 | // Local operations // |
| 72 | ////////////////////////// |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 73 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 74 | AddLocalActionUpdate(const std::string& filename, const Buffer& hash, time_t wtime, int mode, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 75 | int seg_num); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 76 | |
| 77 | // void |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 78 | // AddActionMove(const std::string &oldFile, const std::string &newFile); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 80 | ActionItemPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 81 | AddLocalActionDelete(const std::string& filename); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 83 | ////////////////////////// |
| 84 | // Remote operations // |
| 85 | ////////////////////////// |
| 86 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 87 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 88 | AddRemoteAction(const Name& deviceName, sqlite3_int64 seqno, shared_ptr<Data> actionData); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * @brief Add remote action using just action's parsed content object |
| 92 | * |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 93 | * This function extracts device name and sequence number from the content object's and calls the |
| 94 | * overloaded method |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 95 | */ |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 96 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 97 | AddRemoteAction(shared_ptr<Data> actionData); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 98 | |
| 99 | /////////////////////////// |
| 100 | // General operations // |
| 101 | /////////////////////////// |
| 102 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 103 | shared_ptr<Data> |
| 104 | LookupActionData(const Name& deviceName, sqlite3_int64 seqno); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 105 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 106 | shared_ptr<Data> |
| 107 | LookupActionData(const Name& actionName); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 108 | |
| 109 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 110 | LookupAction(const Name& deviceName, sqlite3_int64 seqno); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 111 | |
| 112 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 113 | LookupAction(const Name& actionName); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 115 | FileItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 116 | LookupAction(const std::string& filename, sqlite3_int64 version, const Buffer& filehash); |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 118 | /** |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 119 | * @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 Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 121 | */ |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 122 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 123 | LookupActionsInFolderRecursively( |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 124 | const function<void(const Name& name, sqlite3_int64 seq_no, const ActionItem&)>& visitor, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 125 | const std::string& folder, int offset = 0, int limit = -1); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 127 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 128 | LookupActionsForFile( |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 129 | const function<void(const Name& name, sqlite3_int64 seq_no, const ActionItem&)>& visitor, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 130 | const std::string& file, int offset = 0, int limit = -1); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 131 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 132 | void |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 133 | LookupRecentFileActions(const function<void(const std::string&, int, int)>& visitor, int limit = 5); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 135 | // |
| 136 | inline FileStatePtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 137 | GetFileState(); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 139 | public: |
| 140 | // for test purposes |
| 141 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 142 | LogSize(); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 144 | private: |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 145 | std::tuple<sqlite3_int64 /*version*/, BufferPtr /*device name*/, sqlite3_int64 /*seq_no*/> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 146 | GetLatestActionForFile(const std::string& filename); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 147 | |
| 148 | static void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 149 | apply_action_xFun(sqlite3_context* context, int argc, sqlite3_value** argv); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 150 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 151 | private: |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 152 | SyncLogPtr m_syncLog; |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 153 | FileStatePtr m_fileState; |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 154 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 155 | // Face& m_face; |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 156 | std::string m_sharedFolderName; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 157 | name::Component m_appName; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 158 | |
| 159 | OnFileAddedOrChangedCallback m_onFileAddedOrChanged; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 160 | OnFileRemovedCallback m_onFileRemoved; |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 161 | KeyChain m_keyChain; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 164 | inline FileStatePtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 165 | ActionLog::GetFileState() |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 166 | { |
| 167 | return m_fileState; |
| 168 | } |
| 169 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 170 | } // namespace chronoshare |
| 171 | } // namespace ndn |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 172 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame] | 173 | #endif // CHRONOSHARE_SRC_ACTION_LOG_HPP |