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, |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame] | 71 | const boost::filesystem::path& rootDir, Face& face); |
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 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 157 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 158 | Did_FetchManager_FileSegmentFetch(const Name& deviceName, const Name& fileSegmentName, |
| 159 | uint32_t segment, shared_ptr<Data> fileSegmentData); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 160 | |
| 161 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 162 | Did_FetchManager_FileSegmentFetch_Execute(Name deviceName, Name fileSegmentName, uint32_t segment, |
| 163 | shared_ptr<Data> fileSegmentData); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 164 | |
| 165 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 166 | Did_FetchManager_FileFetchComplete(const Name& deviceName, const Name& fileBaseName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 167 | |
| 168 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 169 | Did_FetchManager_FileFetchComplete_Execute(Name deviceName, Name fileBaseName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 170 | |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 171 | void |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame] | 172 | Handle_Prefix_Discovery(const Interest& interest, const Data& data); |
| 173 | |
| 174 | void |
| 175 | DiscoverPrefix(); |
| 176 | |
| 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 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 185 | private: |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 186 | Face& m_face; |
| 187 | unique_ptr<SyncCore> m_core; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 188 | SyncLogPtr m_syncLog; |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 189 | ActionLogPtr m_actionLog; |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 190 | FileStatePtr m_fileState; |
Yingdi Yu | adb54eb | 2013-08-15 10:28:28 -0700 | [diff] [blame] | 191 | FileStatePtr m_fileStateCow; |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 192 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 193 | boost::filesystem::path m_rootDir; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 194 | boost::asio::io_service& m_ioService; |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame] | 195 | Scheduler m_scheduler; |
| 196 | util::scheduler::ScopedEventId m_autoDiscovery; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 197 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 198 | ObjectManager m_objectManager; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 199 | Name m_localUserName; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 200 | // maintain object db ptrs so that we don't need to create them |
| 201 | // for every fetched segment of a file |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 202 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 203 | std::map<Buffer, shared_ptr<ObjectDb>> m_objectDbMap; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 204 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 205 | std::string m_sharedFolder; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 206 | unique_ptr<ContentServer> m_server; |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 207 | unique_ptr<StateServer> m_stateServer; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 208 | |
| 209 | FetchManagerPtr m_actionFetcher; |
| 210 | FetchManagerPtr m_fileFetcher; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 211 | |
| 212 | KeyChain m_keyChain; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 213 | }; |
| 214 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 215 | namespace error { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 216 | struct Dispatcher : virtual boost::exception, virtual std::exception |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 217 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 218 | }; |
| 219 | typedef boost::error_info<struct tag_errmsg, std::string> error_info_str; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 220 | } // namespace error |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 221 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 222 | } // namespace chronoshare |
| 223 | } // namespace ndn |
| 224 | |
| 225 | #endif // CHRONOSHARE_SRC_DISPATCHER_HPP |