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