Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #ifndef ACTION_LOG_H |
| 23 | #define ACTION_LOG_H |
| 24 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 25 | #include "db-helper.h" |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 26 | #include "file-state.h" |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 27 | #include "sync-log.h" |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 28 | #include "action-item.pb.h" |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 29 | #include "file-item.pb.h" |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 30 | #include "ccnx-wrapper.h" |
| 31 | #include "ccnx-pco.h" |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 33 | #include <boost/tuple/tuple.hpp> |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 35 | class ActionLog; |
| 36 | typedef boost::shared_ptr<ActionLog> ActionLogPtr; |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 37 | typedef boost::shared_ptr<ActionItem> ActionItemPtr; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 39 | class ActionLog : public DbHelper |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 40 | { |
| 41 | public: |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 42 | typedef boost::function<void (std::string /*filename*/, Ccnx::Name /*device_name*/, sqlite3_int64 /*seq_no*/, |
| 43 | HashPtr /*hash*/, time_t /*m_time*/, int /*mode*/, int /*seg_num*/)> OnFileAddedOrChangedCallback; |
| 44 | |
| 45 | typedef boost::function<void (std::string /*filename*/)> OnFileRemovedCallback; |
| 46 | |
| 47 | public: |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 48 | ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path, |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 49 | SyncLogPtr syncLog, |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 50 | const std::string &sharedFolder, const std::string &appName, |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 51 | OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 53 | virtual ~ActionLog () { } |
| 54 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 55 | ////////////////////////// |
| 56 | // Local operations // |
| 57 | ////////////////////////// |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 58 | ActionItemPtr |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 59 | AddLocalActionUpdate (const std::string &filename, |
| 60 | const Hash &hash, |
| 61 | time_t wtime, |
| 62 | int mode, |
| 63 | int seg_num); |
| 64 | |
| 65 | // void |
| 66 | // AddActionMove (const std::string &oldFile, const std::string &newFile); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 68 | ActionItemPtr |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 69 | AddLocalActionDelete (const std::string &filename); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 71 | ////////////////////////// |
| 72 | // Remote operations // |
| 73 | ////////////////////////// |
| 74 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 75 | ActionItemPtr |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 76 | AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco); |
| 77 | |
| 78 | /** |
| 79 | * @brief Add remote action using just action's parsed content object |
| 80 | * |
| 81 | * This function extracts device name and sequence number from the content object's and calls the overloaded method |
| 82 | */ |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 83 | ActionItemPtr |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 84 | AddRemoteAction (Ccnx::PcoPtr actionPco); |
| 85 | |
| 86 | /////////////////////////// |
| 87 | // General operations // |
| 88 | /////////////////////////// |
| 89 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 90 | Ccnx::PcoPtr |
| 91 | LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno); |
| 92 | |
| 93 | Ccnx::PcoPtr |
| 94 | LookupActionPco (const Ccnx::Name &actionName); |
| 95 | |
| 96 | ActionItemPtr |
| 97 | LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno); |
| 98 | |
| 99 | ActionItemPtr |
| 100 | LookupAction (const Ccnx::Name &actionName); |
| 101 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 102 | FileItemPtr |
| 103 | LookupAction (const std::string &filename, sqlite3_int64 version, const Hash &filehash); |
Alexander Afanasyev | 7a64700 | 2013-01-30 11:54:52 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 105 | // |
| 106 | inline FileStatePtr |
| 107 | GetFileState (); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 109 | public: |
| 110 | // for test purposes |
| 111 | sqlite3_int64 |
| 112 | LogSize (); |
| 113 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 114 | private: |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 115 | boost::tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/> |
| 116 | GetLatestActionForFile (const std::string &filename); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 117 | |
| 118 | static void |
| 119 | apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 121 | private: |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 122 | SyncLogPtr m_syncLog; |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 123 | FileStatePtr m_fileState; |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 125 | Ccnx::CcnxWrapperPtr m_ccnx; |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 126 | std::string m_sharedFolderName; |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 127 | std::string m_appName; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 128 | |
| 129 | OnFileAddedOrChangedCallback m_onFileAddedOrChanged; |
| 130 | OnFileRemovedCallback m_onFileRemoved; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 133 | namespace Error { |
| 134 | struct ActionLog : virtual boost::exception, virtual std::exception { }; |
| 135 | } |
| 136 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 137 | inline FileStatePtr |
| 138 | ActionLog::GetFileState () |
| 139 | { |
| 140 | return m_fileState; |
| 141 | } |
| 142 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 144 | #endif // ACTION_LOG_H |