blob: 75121ea10f5c2bfc3f00308f18d7d63005d6a85d [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"
25#include "sync-core.hpp"
26#include "ccnx-wrapper.hpp"
27#include "executor.hpp"
28#include "object-db.hpp"
29#include "object-manager.hpp"
30#include "content-server.hpp"
31#include "state-server.hpp"
32#include "fetch-manager.hpp"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080033
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080034#include <boost/function.hpp>
35#include <boost/filesystem.hpp>
36#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;
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080049 Dispatcher(const std::string &localUserName
50 , const std::string &sharedFolder
51 , const boost::filesystem::path &rootDir
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070052 , Ndnx::NdnxWrapperPtr ndnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080053 , bool enablePrefixDiscovery = true
54 );
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080055 ~Dispatcher();
56
57 // ----- Callbacks, they only submit the job to executor and immediately return so that event processing thread won't be blocked for too long -------
58
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080059
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080060 // callback to process local file change
61 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080062 Did_LocalFile_AddOrModify (const boost::filesystem::path &relativeFilepath);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080063
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080064 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080065 Did_LocalFile_Delete (const boost::filesystem::path &relativeFilepath);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080066
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080067 /**
68 * @brief Invoked when FileState is detected to have a file which does not exist on a file system
69 */
70 void
71 Restore_LocalFile (FileItemPtr file);
72
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080073 // for test
74 HashPtr
75 SyncRoot() { return m_core->root(); }
76
Zhenkai Zhu25e13582013-02-27 15:33:01 -080077 inline void
78 LookupRecentFileActions(const boost::function<void(const std::string &, int, int)> &visitor, int limit) { m_actionLog->LookupRecentFileActions(visitor, limit); }
79
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080080private:
81 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080082 Did_LocalFile_AddOrModify_Execute (boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080083
84 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080085 Did_LocalFile_Delete_Execute (boost::filesystem::path relativeFilepath); // cannot be const & for Execute event!!! otherwise there will be segfault
86
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080087 void
88 Restore_LocalFile_Execute (FileItemPtr file);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080089
90private:
91 /**
92 * Callbacks:
93 *
94 x * - from SyncLog: when state changes -> to fetch missing actions
95 *
96 x * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
97 * -> to add action to the action log
98 *
99 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
100 *
101 * - 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
102 *
103 x * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
104 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
105 */
106
107 // callback to process remote sync state change
108 void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800109 Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg);
110
111 void
112 Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800113
114 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700115 Did_FetchManager_ActionFetch (const Ndnx::Name &deviceName, const Ndnx::Name &actionName, uint32_t seqno, Ndnx::PcoPtr actionPco);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800116
117 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800118 Did_ActionLog_ActionApply_Delete (const std::string &filename);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800119
120 void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800121 Did_ActionLog_ActionApply_Delete_Execute (std::string filename);
122
123 // void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700124 // Did_ActionLog_ActionApply_AddOrModify (const std::string &filename, Ndnx::Name device_name, sqlite3_int64 seq_no,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800125 // HashPtr hash, time_t m_time, int mode, int seg_num);
126
127 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700128 Did_FetchManager_FileSegmentFetch (const Ndnx::Name &deviceName, const Ndnx::Name &fileSegmentName, uint32_t segment, Ndnx::PcoPtr fileSegmentPco);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800129
130 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700131 Did_FetchManager_FileSegmentFetch_Execute (Ndnx::Name deviceName, Ndnx::Name fileSegmentName, uint32_t segment, Ndnx::PcoPtr fileSegmentPco);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800132
133 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700134 Did_FetchManager_FileFetchComplete (const Ndnx::Name &deviceName, const Ndnx::Name &fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800135
136 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700137 Did_FetchManager_FileFetchComplete_Execute (Ndnx::Name deviceName, Ndnx::Name fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800138
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800139 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700140 Did_LocalPrefix_Updated (const Ndnx::Name &prefix);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800141
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800142private:
143 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700144 AssembleFile_Execute (const Ndnx::Name &deviceName, const Hash &filehash, const boost::filesystem::path &relativeFilepath);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800145
146 // void
147 // fileChanged(const boost::filesystem::path &relativeFilepath, ActionType type);
148
149 // void
150 // syncStateChanged(const SyncStateMsgPtr &stateMsg);
151
152 // void
153 // actionReceived(const ActionItemPtr &actionItem);
154
155 // void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700156 // fileSegmentReceived(const Ndnx::Name &name, const Ndnx::Bytes &content);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800157
158 // void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700159 // fileReady(const Ndnx::Name &fileNamePrefix);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800160
161private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700162 Ndnx::NdnxWrapperPtr m_ndnx;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800163 SyncCore *m_core;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800164 SyncLogPtr m_syncLog;
165 ActionLogPtr m_actionLog;
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800166 FileStatePtr m_fileState;
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700167 FileStatePtr m_fileStateCow;
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800168
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800169 boost::filesystem::path m_rootDir;
170 Executor m_executor;
171 ObjectManager m_objectManager;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700172 Ndnx::Name m_localUserName;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800173 // maintain object db ptrs so that we don't need to create them
174 // for every fetched segment of a file
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800175
176 std::map<Hash, ObjectDbPtr> m_objectDbMap;
177
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800178 std::string m_sharedFolder;
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800179 ContentServer *m_server;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800180 StateServer *m_stateServer;
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800181 bool m_enablePrefixDiscovery;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800182
183 FetchManagerPtr m_actionFetcher;
184 FetchManagerPtr m_fileFetcher;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800185};
186
187namespace Error
188{
189 struct Dispatcher : virtual boost::exception, virtual std::exception {};
190 typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
191}
192
193#endif // DISPATCHER_H
194