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 | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017, 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 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 21 | #ifndef CHRONOSHARE_SRC_DISPATCHER_HPP |
| 22 | #define CHRONOSHARE_SRC_DISPATCHER_HPP |
| 23 | |
| 24 | #include "core/chronoshare-common.hpp" |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 26 | #include "action-log.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include "content-server.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 | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 31 | #include "sync-core.hpp" |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 32 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 33 | #include <boost/filesystem.hpp> |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 34 | #include <boost/filesystem/fstream.hpp> |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 35 | #include <map> |
| 36 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 37 | namespace ndn { |
| 38 | namespace chronoshare { |
| 39 | |
| 40 | typedef shared_ptr<ActionItem> ActionItemPtr; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 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 |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 45 | |
| 46 | /** |
| 47 | * @brief Class synchroize different components |
| 48 | * |
| 49 | * - from SyncLog: when state changes -> to fetch missing actions |
| 50 | * |
| 51 | * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action |
| 52 | * -> to add action to the action log |
| 53 | * |
| 54 | * - from ActionLog/Delete: when action applied(file state changed, file deleted) -> to delete local file |
| 55 | * |
| 56 | * - from ActionLog/AddOrUpdate: when action applied(file state changes, file added or modified) -> |
| 57 | * to assemble the file if file is available in the ObjectDb, otherwise, do nothing |
| 58 | * |
| 59 | * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb |
| 60 | * when file fetch is completed -> if file belongs to FileState, then assemble |
| 61 | * it to filesystem. Don't do anything otherwise |
| 62 | */ |
| 63 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 64 | class Dispatcher |
| 65 | { |
| 66 | public: |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 67 | // sharedFolder is the name to be used in NDN name; |
| 68 | // rootDir is the shared folder dir in local file system; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 69 | Dispatcher(const std::string& localUserName, const std::string& sharedFolder, |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 70 | const boost::filesystem::path& rootDir, Face& face, bool enablePrefixDiscovery = true); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 71 | ~Dispatcher(); |
| 72 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 73 | // ----- Callbacks, they only submit the job to executor and immediately return so that event |
| 74 | // processing thread won't be blocked for too long ------- |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 75 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 76 | // callback to process local file change |
| 77 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 78 | Did_LocalFile_AddOrModify(const boost::filesystem::path& relativeFilepath); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 79 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 80 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 81 | Did_LocalFile_Delete(const boost::filesystem::path& relativeFilepath); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 83 | /** |
| 84 | * @brief Invoked when FileState is detected to have a file which does not exist on a file system |
| 85 | */ |
| 86 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 87 | Restore_LocalFile(FileItemPtr file); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 88 | |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 89 | // for test |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 90 | ConstBufferPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 91 | SyncRoot() |
| 92 | { |
| 93 | return m_core->root(); |
| 94 | } |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 95 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 96 | inline void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 97 | LookupRecentFileActions(const boost::function<void(const std::string&, int, int)>& visitor, |
| 98 | int limit) |
| 99 | { |
| 100 | m_actionLog->LookupRecentFileActions(visitor, limit); |
| 101 | } |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 102 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 103 | private: |
| 104 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 105 | Did_LocalFile_AddOrModify_Execute(boost::filesystem::path relativeFilepath); // cannot be const & |
| 106 | // for Execute |
| 107 | // event!!! otherwise |
| 108 | // there will be |
| 109 | // segfault |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 110 | |
| 111 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 112 | Did_LocalFile_Delete_Execute(boost::filesystem::path relativeFilepath); // cannot be const & for |
| 113 | // Execute event!!! |
| 114 | // otherwise there will be |
| 115 | // segfault |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 117 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | Restore_LocalFile_Execute(FileItemPtr file); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | /** |
| 122 | * Callbacks: |
| 123 | * |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 124 | * - from SyncLog: when state changes -> to fetch missing actions |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 125 | * |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 126 | * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 127 | * -> to add action to the action log |
| 128 | * |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 129 | * - from ActionLog/Delete: when action applied(file state changed, file deleted) -> to delete local file |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 130 | * |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 131 | * - from ActionLog/AddOrUpdate: when action applied(file state changes, file added or modified) -> |
| 132 | * to assemble the file if file is available in the ObjectDb, otherwise, do nothing |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 133 | * |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 134 | * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb |
| 135 | * when file fetch is completed -> if file belongs to FileState, then assemble |
| 136 | * it to filesystem. Don't do anything otherwise |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 137 | */ |
| 138 | |
| 139 | // callback to process remote sync state change |
| 140 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 141 | Did_SyncLog_StateChange(SyncStateMsgPtr stateMsg); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 142 | |
| 143 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 144 | Did_SyncLog_StateChange_Execute(SyncStateMsgPtr stateMsg); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 145 | |
| 146 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 147 | Did_FetchManager_ActionFetch(const Name& deviceName, const Name& actionName, uint32_t seqno, |
| 148 | shared_ptr<Data> actionData); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 149 | |
| 150 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 151 | Did_ActionLog_ActionApply_Delete(const std::string& filename); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 152 | |
| 153 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 154 | Did_ActionLog_ActionApply_Delete_Execute(std::string filename); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 155 | |
| 156 | // void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 157 | // Did_ActionLog_ActionApply_AddOrModify(const std::string &filename, Name device_name, |
| 158 | // sqlite3_int64 seq_no, |
| 159 | // ConstBufferPtr hash, time_t m_time, int mode, int |
| 160 | // seg_num); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 161 | |
| 162 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 163 | Did_FetchManager_FileSegmentFetch(const Name& deviceName, const Name& fileSegmentName, |
| 164 | uint32_t segment, shared_ptr<Data> fileSegmentData); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 165 | |
| 166 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 167 | Did_FetchManager_FileSegmentFetch_Execute(Name deviceName, Name fileSegmentName, uint32_t segment, |
| 168 | shared_ptr<Data> fileSegmentData); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 169 | |
| 170 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 171 | Did_FetchManager_FileFetchComplete(const Name& deviceName, const Name& fileBaseName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 172 | |
| 173 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 174 | Did_FetchManager_FileFetchComplete_Execute(Name deviceName, Name fileBaseName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 175 | |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 176 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 177 | Did_LocalPrefix_Updated(const Name& prefix); |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 179 | private: |
| 180 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 181 | AssembleFile_Execute(const Name& deviceName, const Buffer& filehash, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 182 | const boost::filesystem::path& relativeFilepath); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 183 | |
| 184 | // void |
| 185 | // fileChanged(const boost::filesystem::path &relativeFilepath, ActionType type); |
| 186 | |
| 187 | // void |
| 188 | // syncStateChanged(const SyncStateMsgPtr &stateMsg); |
| 189 | |
| 190 | // void |
| 191 | // actionReceived(const ActionItemPtr &actionItem); |
| 192 | |
| 193 | // void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 194 | // fileSegmentReceived(const Name &name, const Ccnx::Bytes &content); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 195 | |
| 196 | // void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 197 | // fileReady(const Name &fileNamePrefix); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 198 | |
| 199 | private: |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 200 | Face& m_face; |
| 201 | unique_ptr<SyncCore> m_core; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 202 | SyncLogPtr m_syncLog; |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 203 | ActionLogPtr m_actionLog; |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 204 | FileStatePtr m_fileState; |
Yingdi Yu | adb54eb | 2013-08-15 10:28:28 -0700 | [diff] [blame] | 205 | FileStatePtr m_fileStateCow; |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 206 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 207 | boost::filesystem::path m_rootDir; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 208 | boost::asio::io_service& m_ioService; |
| 209 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 210 | ObjectManager m_objectManager; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 211 | Name m_localUserName; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 212 | // maintain object db ptrs so that we don't need to create them |
| 213 | // for every fetched segment of a file |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 214 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 215 | std::map<Buffer, shared_ptr<ObjectDb>> m_objectDbMap; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 216 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 217 | std::string m_sharedFolder; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 218 | unique_ptr<ContentServer> m_server; |
| 219 | //unique_ptr<StateServer> m_stateServer; |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 220 | bool m_enablePrefixDiscovery; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 221 | |
| 222 | FetchManagerPtr m_actionFetcher; |
| 223 | FetchManagerPtr m_fileFetcher; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 224 | |
| 225 | KeyChain m_keyChain; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 228 | namespace error { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 229 | struct Dispatcher : virtual boost::exception, virtual std::exception |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 230 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 231 | }; |
| 232 | typedef boost::error_info<struct tag_errmsg, std::string> error_info_str; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 233 | } // namespace error |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 234 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame^] | 235 | } // namespace chronoshare |
| 236 | } // namespace ndn |
| 237 | |
| 238 | #endif // CHRONOSHARE_SRC_DISPATCHER_HPP |