Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 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 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #ifndef DISPATCHER_H |
| 23 | #define DISPATCHER_H |
| 24 | |
| 25 | #include "action-log.h" |
| 26 | #include "sync-core.h" |
| 27 | #include "ccnx-wrapper.h" |
| 28 | #include "executor.h" |
| 29 | #include "object-db.h" |
| 30 | #include "object-manager.h" |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 31 | #include "content-server.h" |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame^] | 32 | #include "state-server.h" |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 33 | #include "fetch-manager.h" |
| 34 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 35 | #include <boost/function.hpp> |
| 36 | #include <boost/filesystem.hpp> |
| 37 | #include <boost/shared_ptr.hpp> |
| 38 | #include <map> |
| 39 | |
| 40 | typedef boost::shared_ptr<ActionItem> ActionItemPtr; |
| 41 | |
| 42 | // TODO: |
| 43 | // This class lacks a permanent table to store the files in fetching process |
| 44 | // and fetch the missing pieces for those in the table after the application launches |
| 45 | class Dispatcher |
| 46 | { |
| 47 | public: |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 48 | // sharedFolder is the name to be used in NDN name; |
| 49 | // rootDir is the shared folder dir in local file system; |
Zhenkai Zhu | 3290b8e | 2013-01-24 15:25:48 -0800 | [diff] [blame] | 50 | Dispatcher(const std::string &localUserName |
| 51 | , const std::string &sharedFolder |
| 52 | , const boost::filesystem::path &rootDir |
| 53 | , Ccnx::CcnxWrapperPtr ccnx |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 54 | , bool enablePrefixDiscovery = true |
| 55 | ); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 56 | ~Dispatcher(); |
| 57 | |
| 58 | // ----- Callbacks, they only submit the job to executor and immediately return so that event processing thread won't be blocked for too long ------- |
| 59 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 60 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 61 | // callback to process local file change |
| 62 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 63 | Did_LocalFile_AddOrModify (const boost::filesystem::path &relativeFilepath); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 64 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 65 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 66 | Did_LocalFile_Delete (const boost::filesystem::path &relativeFilepath); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 68 | /** |
| 69 | * @brief Invoked when FileState is detected to have a file which does not exist on a file system |
| 70 | */ |
| 71 | void |
| 72 | Restore_LocalFile (FileItemPtr file); |
| 73 | |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 74 | // for test |
| 75 | HashPtr |
| 76 | SyncRoot() { return m_core->root(); } |
| 77 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 78 | private: |
| 79 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 80 | Did_LocalFile_AddOrModify_Execute (boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 81 | |
| 82 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 83 | Did_LocalFile_Delete_Execute (boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault |
| 84 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame^] | 85 | void |
| 86 | Restore_LocalFile_Execute (FileItemPtr file); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | /** |
| 90 | * Callbacks: |
| 91 | * |
| 92 | x * - from SyncLog: when state changes -> to fetch missing actions |
| 93 | * |
| 94 | x * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action |
| 95 | * -> to add action to the action log |
| 96 | * |
| 97 | * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file |
| 98 | * |
| 99 | * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> to assemble the file if file is available in the ObjectDb, otherwise, do nothing |
| 100 | * |
| 101 | x * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb |
| 102 | * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise |
| 103 | */ |
| 104 | |
| 105 | // callback to process remote sync state change |
| 106 | void |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 107 | Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg); |
| 108 | |
| 109 | void |
| 110 | Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 111 | |
| 112 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 113 | Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionName, uint32_t seqno, Ccnx::PcoPtr actionPco); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 114 | |
| 115 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 116 | Did_ActionLog_ActionApply_Delete (const std::string &filename); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 117 | |
| 118 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 119 | Did_ActionLog_ActionApply_Delete_Execute (std::string filename); |
| 120 | |
| 121 | // void |
| 122 | // Did_ActionLog_ActionApply_AddOrModify (const std::string &filename, Ccnx::Name device_name, sqlite3_int64 seq_no, |
| 123 | // HashPtr hash, time_t m_time, int mode, int seg_num); |
| 124 | |
| 125 | void |
| 126 | Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco); |
| 127 | |
| 128 | void |
| 129 | Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco); |
| 130 | |
| 131 | void |
| 132 | Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName); |
| 133 | |
| 134 | void |
| 135 | Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName); |
| 136 | |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 137 | void |
| 138 | Did_LocalPrefix_Updated (const Ccnx::Name &prefix); |
| 139 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 140 | private: |
| 141 | void |
| 142 | AssembleFile_Execute (const Ccnx::Name &deviceName, const Hash &filehash, const boost::filesystem::path &relativeFilepath); |
| 143 | |
| 144 | // void |
| 145 | // fileChanged(const boost::filesystem::path &relativeFilepath, ActionType type); |
| 146 | |
| 147 | // void |
| 148 | // syncStateChanged(const SyncStateMsgPtr &stateMsg); |
| 149 | |
| 150 | // void |
| 151 | // actionReceived(const ActionItemPtr &actionItem); |
| 152 | |
| 153 | // void |
| 154 | // fileSegmentReceived(const Ccnx::Name &name, const Ccnx::Bytes &content); |
| 155 | |
| 156 | // void |
| 157 | // fileReady(const Ccnx::Name &fileNamePrefix); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 158 | |
| 159 | private: |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 160 | Ccnx::CcnxWrapperPtr m_ccnx; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 161 | SyncCore *m_core; |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 162 | SyncLogPtr m_syncLog; |
| 163 | ActionLogPtr m_actionLog; |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 164 | FileStatePtr m_fileState; |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 165 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 166 | boost::filesystem::path m_rootDir; |
| 167 | Executor m_executor; |
| 168 | ObjectManager m_objectManager; |
| 169 | Ccnx::Name m_localUserName; |
| 170 | // maintain object db ptrs so that we don't need to create them |
| 171 | // for every fetched segment of a file |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 172 | |
| 173 | std::map<Hash, ObjectDbPtr> m_objectDbMap; |
| 174 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 175 | std::string m_sharedFolder; |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 176 | ContentServer *m_server; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame^] | 177 | StateServer *m_stateServer; |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 178 | bool m_enablePrefixDiscovery; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 179 | |
| 180 | FetchManagerPtr m_actionFetcher; |
| 181 | FetchManagerPtr m_fileFetcher; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | namespace Error |
| 185 | { |
| 186 | struct Dispatcher : virtual boost::exception, virtual std::exception {}; |
| 187 | typedef boost::error_info<struct tag_errmsg, std::string> error_info_str; |
| 188 | } |
| 189 | |
| 190 | #endif // DISPATCHER_H |
| 191 | |