blob: be7d3c49017a6c0a3796eba16b237271f5360444 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -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
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "dispatcher.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080022#include "ccnx-discovery.hpp"
23#include "fetch-task-db.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include "logging.hpp"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080025
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080026#include <boost/lexical_cast.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080027#include <boost/make_shared.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080028
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070029using namespace Ndnx;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080030using namespace std;
31using namespace boost;
32
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080033_LOG_INIT(Dispatcher);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080034
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080035static const string CHRONOSHARE_APP = "chronoshare";
36static const string BROADCAST_DOMAIN = "/ndn/broadcast";
37
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080038static const int CONTENT_FRESHNESS = 1800; // seconds
Zhenkai Zhu126b21c2013-01-28 17:56:19 -080039const static double DEFAULT_SYNC_INTEREST_INTERVAL = 10.0; // seconds;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080040
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080041Dispatcher::Dispatcher(const std::string& localUserName, const std::string& sharedFolder,
42 const filesystem::path& rootDir, Ccnx::CcnxWrapperPtr ccnx,
43 bool enablePrefixDiscovery)
44 : m_ccnx(ccnx)
45 , m_core(NULL)
46 , m_rootDir(rootDir)
47 , m_executor(
48 1) // creates problems with file assembly. need to ensure somehow that FinishExectute is called after all Segment_Execute finished
49 , m_objectManager(ccnx, rootDir, CHRONOSHARE_APP)
50 , m_localUserName(localUserName)
51 , m_sharedFolder(sharedFolder)
52 , m_server(NULL)
53 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080054{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080055 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056 m_actionLog =
57 make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder, CHRONOSHARE_APP,
58 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
59 ActionLog::OnFileAddedOrChangedCallback(), // don't really need this callback
60 bind(&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
61 m_fileState = m_actionLog->GetFileState();
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080062
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080063 Name syncPrefix = Name(BROADCAST_DOMAIN)(CHRONOSHARE_APP)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080064
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080065 // m_server needs a different ccnx face
66 m_server = new ContentServer(make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName,
67 m_sharedFolder, CHRONOSHARE_APP, CONTENT_FRESHNESS);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080068 m_server->registerPrefix(Name("/"));
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080069 m_server->registerPrefix(Name(BROADCAST_DOMAIN));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080070
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080071 m_stateServer =
72 new StateServer(make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName,
73 m_sharedFolder, CHRONOSHARE_APP, m_objectManager, CONTENT_FRESHNESS);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080074 // no need to register, right now only listening on localhost prefix
75
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 m_core = new SyncCore(m_syncLog, localUserName, Name("/"), syncPrefix,
77 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx,
78 DEFAULT_SYNC_INTEREST_INTERVAL);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080079
Zhenkai Zhuda686882013-01-29 22:32:24 -080080 FetchTaskDbPtr actionTaskDb = make_shared<FetchTaskDb>(m_rootDir, "action");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080081 m_actionFetcher =
82 make_shared<FetchManager>(m_ccnx, bind(&SyncLog::LookupLocator, &*m_syncLog, _1),
83 Name(BROADCAST_DOMAIN), // no appname suffix now
84 3,
85 bind(&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4),
86 FetchManager::FinishCallback(), actionTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080087
Zhenkai Zhuda686882013-01-29 22:32:24 -080088 FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080089 m_fileFetcher =
90 make_shared<FetchManager>(m_ccnx, bind(&SyncLog::LookupLocator, &*m_syncLog, _1),
91 Name(BROADCAST_DOMAIN), // no appname suffix now
92 3, bind(&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2,
93 _3, _4),
94 bind(&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
95 fileTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080096
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080097
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080098 if (m_enablePrefixDiscovery) {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080099 _LOG_DEBUG("registering prefix discovery in Dispatcher");
100 string tag = "dispatcher" + m_localUserName.toString();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101 Ccnx::CcnxDiscovery::registerCallback(
102 TaggedFunction(bind(&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800103 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800104
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800105 m_executor.start();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800106}
107
108Dispatcher::~Dispatcher()
109{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 _LOG_DEBUG("Enter destructor of dispatcher");
111 m_executor.shutdown();
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800112
113 // _LOG_DEBUG (">>");
114
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800115 if (m_enablePrefixDiscovery) {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800116 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
117 string tag = "dispatcher" + m_localUserName.toString();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800118 Ccnx::CcnxDiscovery::deregisterCallback(
119 TaggedFunction(bind(&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800120 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800121
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800122 if (m_core != NULL) {
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800123 delete m_core;
124 m_core = NULL;
125 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800126
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800127 if (m_server != NULL) {
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800128 delete m_server;
129 m_server = NULL;
130 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800131
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800132 if (m_stateServer != NULL) {
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800133 delete m_stateServer;
134 m_stateServer = NULL;
135 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800136}
137
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800138void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800139Dispatcher::Did_LocalPrefix_Updated(const Ccnx::Name& forwardingHint)
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800140{
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800141 Name effectiveForwardingHint;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800142 if (m_localUserName.size() >= forwardingHint.size() &&
143 m_localUserName.getPartialName(0, forwardingHint.size()) == forwardingHint) {
144 effectiveForwardingHint = Name("/"); // "directly" accesible
145 }
146 else {
147 effectiveForwardingHint = forwardingHint;
148 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800149
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800150 Name oldLocalPrefix = m_syncLog->LookupLocalLocator();
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800151
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800152 if (oldLocalPrefix == effectiveForwardingHint) {
153 _LOG_DEBUG(
154 "Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint
155 << ", but effective prefix didn't change");
156 return;
157 }
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800158
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800159 if (effectiveForwardingHint == Name("/") || effectiveForwardingHint == Name(BROADCAST_DOMAIN)) {
160 _LOG_DEBUG("Basic effective prefix [" << effectiveForwardingHint
161 << "]. Updating local prefix, but don't reregister");
162 m_syncLog->UpdateLocalLocator(effectiveForwardingHint);
163 return;
164 }
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800165
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800166 _LOG_DEBUG("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint);
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800167
168 m_server->registerPrefix(effectiveForwardingHint);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800169 m_syncLog->UpdateLocalLocator(effectiveForwardingHint);
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800170
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800171 if (oldLocalPrefix == Name("/") || oldLocalPrefix == Name(BROADCAST_DOMAIN)) {
172 _LOG_DEBUG("Don't deregister basic prefix: " << oldLocalPrefix);
173 return;
174 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800175 m_server->deregisterPrefix(oldLocalPrefix);
176}
177
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800178// moved to state-server
179// void
180// Dispatcher::Restore_LocalFile (FileItemPtr file)
181// {
182// m_executor.execute (bind (&Dispatcher::Restore_LocalFile_Execute, this, file));
183// }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800184
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800185/////////////////////////////////////////////////////////////////////////////////////////////////////
186/////////////////////////////////////////////////////////////////////////////////////////////////////
187/////////////////////////////////////////////////////////////////////////////////////////////////////
188
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800189void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800190Dispatcher::Did_LocalFile_AddOrModify(const filesystem::path& relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800191{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800192 m_executor.execute(bind(&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800193}
194
195void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800196Dispatcher::Did_LocalFile_AddOrModify_Execute(filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800197{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800198 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800199 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800200 if (!filesystem::exists(absolutePath)) {
201 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
202 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
203 return;
204 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800205
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800206 FileItemPtr currentFile = m_fileState->LookupFile(relativeFilePath.generic_string());
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800207 if (currentFile &&
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800208 *Hash::FromFileContent(absolutePath) ==
209 Hash(currentFile->file_hash().c_str(), currentFile->file_hash().size())
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800210 // The following two are commented out to prevent front end from reporting intermediate files
211 // should enable it if there is other way to prevent this
212 // && last_write_time (absolutePath) == currentFile->mtime ()
213 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800214 ) {
215 _LOG_ERROR("Got notification about the same file [" << relativeFilePath << "]");
216 return;
217 }
218
219 if (currentFile && !currentFile->is_complete()) {
220 _LOG_ERROR("Got notification about incomplete file [" << relativeFilePath << "]");
221 return;
222 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800223
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800224
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800225 int seg_num;
226 HashPtr hash;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800227 tie(hash, seg_num) = m_objectManager.localFileToObjects(absolutePath, m_localUserName);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800228
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800229 try {
230 m_actionLog->AddLocalActionUpdate(relativeFilePath.generic_string(),
231 *hash,
232 last_write_time(absolutePath),
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700233#if BOOST_VERSION >= 104900
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800234 status(absolutePath).permissions(),
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700235#else
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800236 0,
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700237#endif
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800238 seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800239
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800240 // notify SyncCore to propagate the change
241 m_core->localStateChangedDelayed();
242 }
243 catch (filesystem::filesystem_error& error) {
244 _LOG_ERROR("File operations failed on [" << relativeFilePath << "] (ignoring)");
245 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800246}
247
248void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800249Dispatcher::Did_LocalFile_Delete(const filesystem::path& relativeFilePath)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800250{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800251 m_executor.execute(bind(&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800252}
253
254void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800255Dispatcher::Did_LocalFile_Delete_Execute(filesystem::path relativeFilePath)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800256{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800257 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800258 if (filesystem::exists(absolutePath)) {
259 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
260 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
261 return;
262 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800263
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800264 m_actionLog->AddLocalActionDelete(relativeFilePath.generic_string());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800265 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800266 m_core->localStateChangedDelayed();
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800267}
268
269/////////////////////////////////////////////////////////////////////////////////////////////////////
270/////////////////////////////////////////////////////////////////////////////////////////////////////
271/////////////////////////////////////////////////////////////////////////////////////////////////////
272
273/**
274 * Callbacks:
275 *
276 * - from SyncLog: when state changes -> to fetch missing actions
277 *
278 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
279 * -> to add action to the action log
280 *
281 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
282 *
283 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
284 *
285 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
286 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
287 */
288
289void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800290Dispatcher::Did_SyncLog_StateChange(SyncStateMsgPtr stateMsg)
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800291{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800292 m_executor.execute(bind(&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800293}
294
295void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800296Dispatcher::Did_SyncLog_StateChange_Execute(SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800297{
298 int size = stateMsg->state_size();
299 int index = 0;
300 // iterate and fetch the actions
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800301 for (; index < size; index++) {
302 SyncState state = stateMsg->state(index);
303 if (state.has_old_seq() && state.has_seq()) {
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800304 uint64_t oldSeq = state.old_seq();
305 uint64_t newSeq = state.seq();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800306 Name userName(reinterpret_cast<const unsigned char*>(state.name().c_str()),
307 state.name().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800308
309 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800310 Name actionNameBase = Name("/")(userName)(CHRONOSHARE_APP)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800311
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800312 m_actionFetcher->Enqueue(userName, actionNameBase, std::max<uint64_t>(oldSeq + 1, 1), newSeq,
313 FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800314 }
315 }
316}
317
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800318
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800319void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800320Dispatcher::Did_FetchManager_ActionFetch(const Ccnx::Name& deviceName,
321 const Ccnx::Name& actionBaseName, uint32_t seqno,
322 Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800323{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800324 /// @todo Errors and exception checking
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800325 _LOG_DEBUG("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName
326 << ", seqno: "
327 << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800328
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800329 ActionItemPtr action = m_actionLog->AddRemoteAction(deviceName, seqno, actionPco);
330 if (!action) {
331 _LOG_ERROR("AddRemoteAction did not insert action, ignoring");
332 return;
333 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800334 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800335
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800336 if (action->action() == ActionItem::UPDATE) {
337 Hash hash(action->file_hash().c_str(), action->file_hash().size());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800338
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800339 Name fileNameBase =
340 Name("/")(deviceName)(CHRONOSHARE_APP)("file")(hash.GetHash(), hash.GetHashBytes());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800341
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800342 string hashStr = lexical_cast<string>(hash);
343 if (ObjectDb::DoesExist(m_rootDir / ".chronoshare", deviceName, hashStr)) {
344 _LOG_DEBUG(
345 "File already exists in the database. No need to refetch, just directly applying the action");
346 Did_FetchManager_FileFetchComplete(deviceName, fileNameBase);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800347 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800348 else {
349 if (m_objectDbMap.find(hash) == m_objectDbMap.end()) {
350 _LOG_DEBUG("create ObjectDb for " << hash);
351 m_objectDbMap[hash] = make_shared<ObjectDb>(m_rootDir / ".chronoshare", hashStr);
352 }
353
354 m_fileFetcher->Enqueue(deviceName, fileNameBase, 0, action->seg_num() - 1,
355 FetchManager::PRIORITY_NORMAL);
356 }
357 }
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800358 // if necessary (when version number is the highest) delete will be applied through the trigger in m_actionLog->AddRemoteAction call
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800359}
360
361void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800362Dispatcher::Did_ActionLog_ActionApply_Delete(const std::string& filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800363{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800364 m_executor.execute(bind(&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800365}
366
367void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800368Dispatcher::Did_ActionLog_ActionApply_Delete_Execute(std::string filename)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800369{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800370 _LOG_DEBUG("Action to delete " << filename);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800371
372 filesystem::path absolutePath = m_rootDir / filename;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800373 try {
374 if (filesystem::exists(absolutePath)) {
375 // need some protection from local detection of removal
376 remove(absolutePath);
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800377
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800378 // hack to remove empty parent dirs
379 filesystem::path parentPath = absolutePath.parent_path();
380 while (parentPath > m_rootDir) {
381 if (filesystem::is_empty(parentPath)) {
382 filesystem::remove(parentPath);
383 parentPath = parentPath.parent_path();
384 }
385 else {
386 break;
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800387 }
388 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800389 }
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800390 // don't exist
391 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800392 catch (filesystem::filesystem_error& error) {
393 _LOG_ERROR("File operations failed when removing [" << absolutePath << "] (ignoring)");
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800394 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800395}
396
397void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800398Dispatcher::Did_FetchManager_FileSegmentFetch(const Ccnx::Name& deviceName,
399 const Ccnx::Name& fileSegmentBaseName,
400 uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800401{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800402 m_executor.execute(bind(&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName,
403 fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800404}
405
406void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800407Dispatcher::Did_FetchManager_FileSegmentFetch_Execute(Ccnx::Name deviceName,
408 Ccnx::Name fileSegmentBaseName,
409 uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800410{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800411 // fileSegmentBaseName: /<device_name>/<appname>/file/<hash>
412
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800413 const Bytes& hashBytes = fileSegmentBaseName.getCompFromBack(0);
414 Hash hash(head(hashBytes), hashBytes.size());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800415
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800416 _LOG_DEBUG("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName
417 << ", segment: "
418 << segment);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800419
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800420 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800421
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800422 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find(hash);
423 if (db != m_objectDbMap.end()) {
424 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800425 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800426 else {
427 _LOG_ERROR("no db available for this content object: " << fileSegmentBaseName << ", size: "
428 << fileSegmentPco->buf().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800429 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800430
431 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
432 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800433}
434
435void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800436Dispatcher::Did_FetchManager_FileFetchComplete(const Ccnx::Name& deviceName,
437 const Ccnx::Name& fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800438{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800439 m_executor.execute(
440 bind(&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800441}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800442
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800443void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800444Dispatcher::Did_FetchManager_FileFetchComplete_Execute(Ccnx::Name deviceName, Ccnx::Name fileBaseName)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800445{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800446 // fileBaseName: /<device_name>/<appname>/file/<hash>
447
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800448 _LOG_DEBUG("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800449
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800450 const Bytes& hashBytes = fileBaseName.getCompFromBack(0);
451 Hash hash(head(hashBytes), hashBytes.size());
452 _LOG_DEBUG("Extracted hash: " << hash.shortHash());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800453
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800454 if (m_objectDbMap.find(hash) != m_objectDbMap.end()) {
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800455 // remove the db handle
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800456 m_objectDbMap.erase(hash); // to commit write
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800457 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800458 else {
459 _LOG_ERROR("no db available for this file: " << hash);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800460 }
461
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800462 FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash(hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800463
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800464 for (FileItems::iterator file = filesToAssemble->begin(); file != filesToAssemble->end(); file++) {
465 boost::filesystem::path filePath = m_rootDir / file->filename();
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700466
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800467 try {
468 if (filesystem::exists(filePath) && filesystem::last_write_time(filePath) == file->mtime() &&
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700469#if BOOST_VERSION >= 104900
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800470 filesystem::status(filePath).permissions() == static_cast<filesystem::perms>(file->mode()) &&
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700471#endif
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800472 *Hash::FromFileContent(filePath) == hash) {
473 _LOG_DEBUG("Asking to assemble a file, but file already exists on a filesystem");
474 continue;
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800475 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800476 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800477 catch (filesystem::filesystem_error& error) {
478 _LOG_ERROR("File operations failed on [" << filePath << "] (ignoring)");
479 }
480
481 if (ObjectDb::DoesExist(m_rootDir / ".chronoshare", deviceName,
482 boost::lexical_cast<string>(hash))) {
483 bool ok = m_objectManager.objectsToLocalFile(deviceName, hash, filePath);
484 if (ok) {
485 last_write_time(filePath, file->mtime());
486#if BOOST_VERSION >= 104900
487 permissions(filePath, static_cast<filesystem::perms>(file->mode()));
488#endif
489
490 m_fileState->SetFileComplete(file->filename());
491 }
492 else {
493 _LOG_ERROR("Notified about complete fetch, but file cannot be restored from the database: ["
494 << filePath
495 << "]");
496 }
497 }
498 else {
499 _LOG_ERROR(filePath << " supposed to have all segments, but not");
500 // should abort for debugging
501 }
502 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800503}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800504
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800505// moved to state-server
506// void
507// Dispatcher::Restore_LocalFile_Execute (FileItemPtr file)
508// {
509// _LOG_DEBUG ("Got request to restore local file [" << file->filename () << "]");
510// // the rest will gracefully fail if object-db is missing or incomplete
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800511
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800512// boost::filesystem::path filePath = m_rootDir / file->filename ();
513// Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
514// Hash hash (file->file_hash ().c_str (), file->file_hash ().size ());
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800515
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800516// try
517// {
518// if (filesystem::exists (filePath) &&
519// filesystem::last_write_time (filePath) == file->mtime () &&
520// filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
521// *Hash::FromFileContent (filePath) == hash)
522// {
523// _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
524// return;
525// }
526// }
527// catch (filesystem::filesystem_error &error)
528// {
529// _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
530// }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800531
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800532// m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800533
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800534// last_write_time (filePath, file->mtime ());
535// permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800536
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800537// }