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