blob: 526a633059126ee2fe435354b2972805f8a0ccbd [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, 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
21#ifndef DISPATCHER_H
22#define DISPATCHER_H
23
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "action-log.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080025#include "ccnx-wrapper.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080026#include "content-server.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080027#include "executor.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 Afanasyevf4cde4e2016-12-25 13:42:57 -080031#include "state-server.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080032#include "sync-core.hpp"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080033
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080034#include <boost/filesystem.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080035#include <boost/function.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080036#include <boost/shared_ptr.hpp>
37#include <map>
38
39typedef 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
44class Dispatcher
45{
46public:
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080047 // sharedFolder is the name to be used in NDN name;
48 // rootDir is the shared folder dir in local file system;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080049 Dispatcher(const std::string& localUserName, const std::string& sharedFolder,
50 const boost::filesystem::path& rootDir, Ccnx::CcnxWrapperPtr ccnx,
51 bool enablePrefixDiscovery = true);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080052 ~Dispatcher();
53
54 // ----- Callbacks, they only submit the job to executor and immediately return so that event processing thread won't be blocked for too long -------
55
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080056
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080057 // callback to process local file change
58 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059 Did_LocalFile_AddOrModify(const boost::filesystem::path& relativeFilepath);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080060
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080061 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062 Did_LocalFile_Delete(const boost::filesystem::path& relativeFilepath);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080063
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080064 /**
65 * @brief Invoked when FileState is detected to have a file which does not exist on a file system
66 */
67 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080068 Restore_LocalFile(FileItemPtr file);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080069
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080070 // for test
71 HashPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080072 SyncRoot()
73 {
74 return m_core->root();
75 }
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080076
Zhenkai Zhu25e13582013-02-27 15:33:01 -080077 inline void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078 LookupRecentFileActions(const boost::function<void(const std::string&, int, int)>& visitor,
79 int limit)
80 {
81 m_actionLog->LookupRecentFileActions(visitor, limit);
82 }
Zhenkai Zhu25e13582013-02-27 15:33:01 -080083
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080084private:
85 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080086 Did_LocalFile_AddOrModify_Execute(
87 boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080088
89 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080090 Did_LocalFile_Delete_Execute(
91 boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080092
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080093 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080094 Restore_LocalFile_Execute(FileItemPtr file);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080095
96private:
97 /**
98 * Callbacks:
99 *
100 x * - from SyncLog: when state changes -> to fetch missing actions
101 *
102 x * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
103 * -> to add action to the action log
104 *
105 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
106 *
107 * - 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
108 *
109 x * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
110 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
111 */
112
113 // callback to process remote sync state change
114 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800115 Did_SyncLog_StateChange(SyncStateMsgPtr stateMsg);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800116
117 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800118 Did_SyncLog_StateChange_Execute(SyncStateMsgPtr stateMsg);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800119
120 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800121 Did_FetchManager_ActionFetch(const Ccnx::Name& deviceName, const Ccnx::Name& actionName,
122 uint32_t seqno, Ccnx::PcoPtr actionPco);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800123
124 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800125 Did_ActionLog_ActionApply_Delete(const std::string& filename);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800126
127 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800128 Did_ActionLog_ActionApply_Delete_Execute(std::string filename);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800129
130 // void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700131 // Did_ActionLog_ActionApply_AddOrModify (const std::string &filename, Ndnx::Name device_name, sqlite3_int64 seq_no,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800132 // HashPtr hash, time_t m_time, int mode, int seg_num);
133
134 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800135 Did_FetchManager_FileSegmentFetch(const Ccnx::Name& deviceName, const Ccnx::Name& fileSegmentName,
136 uint32_t segment, Ccnx::PcoPtr fileSegmentPco);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800137
138 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800139 Did_FetchManager_FileSegmentFetch_Execute(Ccnx::Name deviceName, Ccnx::Name fileSegmentName,
140 uint32_t segment, Ccnx::PcoPtr fileSegmentPco);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800141
142 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800143 Did_FetchManager_FileFetchComplete(const Ccnx::Name& deviceName, const Ccnx::Name& fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800144
145 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800146 Did_FetchManager_FileFetchComplete_Execute(Ccnx::Name deviceName, Ccnx::Name fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800147
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800148 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800149 Did_LocalPrefix_Updated(const Ccnx::Name& prefix);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800150
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800151private:
152 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800153 AssembleFile_Execute(const Ccnx::Name& deviceName, const Hash& filehash,
154 const boost::filesystem::path& relativeFilepath);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800155
156 // void
157 // fileChanged(const boost::filesystem::path &relativeFilepath, ActionType type);
158
159 // void
160 // syncStateChanged(const SyncStateMsgPtr &stateMsg);
161
162 // void
163 // actionReceived(const ActionItemPtr &actionItem);
164
165 // void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700166 // fileSegmentReceived(const Ndnx::Name &name, const Ndnx::Bytes &content);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800167
168 // void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700169 // fileReady(const Ndnx::Name &fileNamePrefix);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800170
171private:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800172 Ccnx::CcnxWrapperPtr m_ccnx;
173 SyncCore* m_core;
174 SyncLogPtr m_syncLog;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800175 ActionLogPtr m_actionLog;
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800176 FileStatePtr m_fileState;
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700177 FileStatePtr m_fileStateCow;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800178
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800179 boost::filesystem::path m_rootDir;
180 Executor m_executor;
181 ObjectManager m_objectManager;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700182 Ndnx::Name m_localUserName;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800183 // maintain object db ptrs so that we don't need to create them
184 // for every fetched segment of a file
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800185
186 std::map<Hash, ObjectDbPtr> m_objectDbMap;
187
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800188 std::string m_sharedFolder;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800189 ContentServer* m_server;
190 StateServer* m_stateServer;
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800191 bool m_enablePrefixDiscovery;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800192
193 FetchManagerPtr m_actionFetcher;
194 FetchManagerPtr m_fileFetcher;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800195};
196
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800197namespace Error {
198struct Dispatcher : virtual boost::exception, virtual std::exception
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800199{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800200};
201typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800202}
203
204#endif // DISPATCHER_H