blob: dcc73e44750f49dc1dfcfded495d5a15708bdeb1 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wang8e56d082016-12-25 14:45:23 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Zhuc3fd51e2013-01-22 10:45:54 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Zhuc3fd51e2013-01-22 10:45:54 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Zhuc3fd51e2013-01-22 10:45:54 -080019 */
20
Lijing Wang8e56d082016-12-25 14:45:23 -080021#ifndef CHRONOSHARE_SRC_DISPATCHER_HPP
22#define CHRONOSHARE_SRC_DISPATCHER_HPP
23
24#include "core/chronoshare-common.hpp"
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080025
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080026#include "action-log.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080027#include "content-server.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080028#include "fetch-manager.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080029#include "object-db.hpp"
30#include "object-manager.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080031#include "sync-core.hpp"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080032
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080033#include <boost/filesystem.hpp>
Lijing Wang8e56d082016-12-25 14:45:23 -080034#include <boost/filesystem/fstream.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080035#include <map>
36
Lijing Wang8e56d082016-12-25 14:45:23 -080037namespace ndn {
38namespace chronoshare {
39
40typedef shared_ptr<ActionItem> ActionItemPtr;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080041
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 Wang8e56d082016-12-25 14:45:23 -080045
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 Zhuc3fd51e2013-01-22 10:45:54 -080064class Dispatcher
65{
66public:
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080067 // sharedFolder is the name to be used in NDN name;
68 // rootDir is the shared folder dir in local file system;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080069 Dispatcher(const std::string& localUserName, const std::string& sharedFolder,
Lijing Wang8e56d082016-12-25 14:45:23 -080070 const boost::filesystem::path& rootDir, Face& face, bool enablePrefixDiscovery = true);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080071 ~Dispatcher();
72
Lijing Wang8e56d082016-12-25 14:45:23 -080073 // ----- 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 Afanasyevf9978f82013-01-23 16:30:31 -080075
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080076 // callback to process local file change
77 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078 Did_LocalFile_AddOrModify(const boost::filesystem::path& relativeFilepath);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080079
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080080 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080081 Did_LocalFile_Delete(const boost::filesystem::path& relativeFilepath);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080082
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080083 /**
84 * @brief Invoked when FileState is detected to have a file which does not exist on a file system
85 */
86 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080087 Restore_LocalFile(FileItemPtr file);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080088
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080089 // for test
Lijing Wang8e56d082016-12-25 14:45:23 -080090 ConstBufferPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091 SyncRoot()
92 {
93 return m_core->root();
94 }
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080095
Zhenkai Zhu25e13582013-02-27 15:33:01 -080096 inline void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 LookupRecentFileActions(const boost::function<void(const std::string&, int, int)>& visitor,
98 int limit)
99 {
100 m_actionLog->LookupRecentFileActions(visitor, limit);
101 }
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800102
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800103private:
104 void
Lijing Wang8e56d082016-12-25 14:45:23 -0800105 Did_LocalFile_AddOrModify_Execute(boost::filesystem::path relativeFilepath); // cannot be const &
106 // for Execute
107 // event!!! otherwise
108 // there will be
109 // segfault
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800110
111 void
Lijing Wang8e56d082016-12-25 14:45:23 -0800112 Did_LocalFile_Delete_Execute(boost::filesystem::path relativeFilepath); // cannot be const & for
113 // Execute event!!!
114 // otherwise there will be
115 // segfault
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800116
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800117 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800118 Restore_LocalFile_Execute(FileItemPtr file);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800119
120private:
121 /**
122 * Callbacks:
123 *
Lijing Wang8e56d082016-12-25 14:45:23 -0800124 * - from SyncLog: when state changes -> to fetch missing actions
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800125 *
Lijing Wang8e56d082016-12-25 14:45:23 -0800126 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800127 * -> to add action to the action log
128 *
Lijing Wang8e56d082016-12-25 14:45:23 -0800129 * - from ActionLog/Delete: when action applied(file state changed, file deleted) -> to delete local file
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800130 *
Lijing Wang8e56d082016-12-25 14:45:23 -0800131 * - 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 Afanasyevf9978f82013-01-23 16:30:31 -0800133 *
Lijing Wang8e56d082016-12-25 14:45:23 -0800134 * - 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 Afanasyevf9978f82013-01-23 16:30:31 -0800137 */
138
139 // callback to process remote sync state change
140 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800141 Did_SyncLog_StateChange(SyncStateMsgPtr stateMsg);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800142
143 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800144 Did_SyncLog_StateChange_Execute(SyncStateMsgPtr stateMsg);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800145
146 void
Lijing Wang8e56d082016-12-25 14:45:23 -0800147 Did_FetchManager_ActionFetch(const Name& deviceName, const Name& actionName, uint32_t seqno,
148 shared_ptr<Data> actionData);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800149
150 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800151 Did_ActionLog_ActionApply_Delete(const std::string& filename);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800152
153 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800154 Did_ActionLog_ActionApply_Delete_Execute(std::string filename);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800155
156 // void
Lijing Wang8e56d082016-12-25 14:45:23 -0800157 // 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 Afanasyevf9978f82013-01-23 16:30:31 -0800161
162 void
Lijing Wang8e56d082016-12-25 14:45:23 -0800163 Did_FetchManager_FileSegmentFetch(const Name& deviceName, const Name& fileSegmentName,
164 uint32_t segment, shared_ptr<Data> fileSegmentData);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800165
166 void
Lijing Wang8e56d082016-12-25 14:45:23 -0800167 Did_FetchManager_FileSegmentFetch_Execute(Name deviceName, Name fileSegmentName, uint32_t segment,
168 shared_ptr<Data> fileSegmentData);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800169
170 void
Lijing Wang8e56d082016-12-25 14:45:23 -0800171 Did_FetchManager_FileFetchComplete(const Name& deviceName, const Name& fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800172
173 void
Lijing Wang8e56d082016-12-25 14:45:23 -0800174 Did_FetchManager_FileFetchComplete_Execute(Name deviceName, Name fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800175
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800176 void
Lijing Wang8e56d082016-12-25 14:45:23 -0800177 Did_LocalPrefix_Updated(const Name& prefix);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800178
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800179private:
180 void
Lijing Wang8e56d082016-12-25 14:45:23 -0800181 AssembleFile_Execute(const Name& deviceName, const Buffer& filehash,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800182 const boost::filesystem::path& relativeFilepath);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800183
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 Wang8e56d082016-12-25 14:45:23 -0800194 // fileSegmentReceived(const Name &name, const Ccnx::Bytes &content);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800195
196 // void
Lijing Wang8e56d082016-12-25 14:45:23 -0800197 // fileReady(const Name &fileNamePrefix);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800198
199private:
Lijing Wang8e56d082016-12-25 14:45:23 -0800200 Face& m_face;
201 unique_ptr<SyncCore> m_core;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800202 SyncLogPtr m_syncLog;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800203 ActionLogPtr m_actionLog;
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800204 FileStatePtr m_fileState;
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700205 FileStatePtr m_fileStateCow;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800206
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800207 boost::filesystem::path m_rootDir;
Lijing Wang8e56d082016-12-25 14:45:23 -0800208 boost::asio::io_service& m_ioService;
209
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800210 ObjectManager m_objectManager;
Lijing Wang8e56d082016-12-25 14:45:23 -0800211 Name m_localUserName;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800212 // maintain object db ptrs so that we don't need to create them
213 // for every fetched segment of a file
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800214
Lijing Wang8e56d082016-12-25 14:45:23 -0800215 std::map<Buffer, shared_ptr<ObjectDb>> m_objectDbMap;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800216
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800217 std::string m_sharedFolder;
Lijing Wang8e56d082016-12-25 14:45:23 -0800218 unique_ptr<ContentServer> m_server;
219 //unique_ptr<StateServer> m_stateServer;
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800220 bool m_enablePrefixDiscovery;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800221
222 FetchManagerPtr m_actionFetcher;
223 FetchManagerPtr m_fileFetcher;
Lijing Wang8e56d082016-12-25 14:45:23 -0800224
225 KeyChain m_keyChain;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800226};
227
Lijing Wang8e56d082016-12-25 14:45:23 -0800228namespace error {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800229struct Dispatcher : virtual boost::exception, virtual std::exception
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800230{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800231};
232typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
Lijing Wang8e56d082016-12-25 14:45:23 -0800233} // namespace error
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800234
Lijing Wang8e56d082016-12-25 14:45:23 -0800235} // namespace chronoshare
236} // namespace ndn
237
238#endif // CHRONOSHARE_SRC_DISPATCHER_HPP