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