blob: 3bc3bff175c92d27e0759b680dc09d92943a4802 [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)
Lijing Wang6977ec42016-12-25 14:45:09 -080047 , m_executor(1)
48 // creates problems with file assembly. need to ensure somehow that
49 // FinishExectute is called after all Segment_Execute finished
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080050 , m_objectManager(ccnx, rootDir, CHRONOSHARE_APP)
51 , m_localUserName(localUserName)
52 , m_sharedFolder(sharedFolder)
53 , m_server(NULL)
54 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080055{
Lijing Wang6977ec42016-12-25 14:45:09 -080056 KeyChain keyChain;
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080057 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 m_actionLog =
59 make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder, CHRONOSHARE_APP,
60 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
61 ActionLog::OnFileAddedOrChangedCallback(), // don't really need this callback
62 bind(&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
63 m_fileState = m_actionLog->GetFileState();
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080064
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080065 Name syncPrefix = Name(BROADCAST_DOMAIN)(CHRONOSHARE_APP)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080066
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080067 // m_server needs a different ccnx face
68 m_server = new ContentServer(make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName,
Lijing Wang6977ec42016-12-25 14:45:09 -080069 m_sharedFolder, CHRONOSHARE_APP, keyChain, CONTENT_FRESHNESS);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080070 m_server->registerPrefix(Name("/"));
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080071 m_server->registerPrefix(Name(BROADCAST_DOMAIN));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080072
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080073 m_stateServer =
74 new StateServer(make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName,
75 m_sharedFolder, CHRONOSHARE_APP, m_objectManager, CONTENT_FRESHNESS);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080076 // no need to register, right now only listening on localhost prefix
77
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078 m_core = new SyncCore(m_syncLog, localUserName, Name("/"), syncPrefix,
79 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx,
80 DEFAULT_SYNC_INTEREST_INTERVAL);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080081
Zhenkai Zhuda686882013-01-29 22:32:24 -080082 FetchTaskDbPtr actionTaskDb = make_shared<FetchTaskDb>(m_rootDir, "action");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080083 m_actionFetcher =
84 make_shared<FetchManager>(m_ccnx, bind(&SyncLog::LookupLocator, &*m_syncLog, _1),
85 Name(BROADCAST_DOMAIN), // no appname suffix now
86 3,
87 bind(&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4),
88 FetchManager::FinishCallback(), actionTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080089
Zhenkai Zhuda686882013-01-29 22:32:24 -080090 FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091 m_fileFetcher =
92 make_shared<FetchManager>(m_ccnx, bind(&SyncLog::LookupLocator, &*m_syncLog, _1),
93 Name(BROADCAST_DOMAIN), // no appname suffix now
94 3, bind(&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2,
95 _3, _4),
96 bind(&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
97 fileTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080098
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080099
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 if (m_enablePrefixDiscovery) {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800101 _LOG_DEBUG("registering prefix discovery in Dispatcher");
102 string tag = "dispatcher" + m_localUserName.toString();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 Ccnx::CcnxDiscovery::registerCallback(
104 TaggedFunction(bind(&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800105 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800106
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 m_executor.start();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800108}
109
110Dispatcher::~Dispatcher()
111{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112 _LOG_DEBUG("Enter destructor of dispatcher");
113 m_executor.shutdown();
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800114
115 // _LOG_DEBUG (">>");
116
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800117 if (m_enablePrefixDiscovery) {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800118 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
119 string tag = "dispatcher" + m_localUserName.toString();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120 Ccnx::CcnxDiscovery::deregisterCallback(
121 TaggedFunction(bind(&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800122 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800123
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800124 if (m_core != NULL) {
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800125 delete m_core;
126 m_core = NULL;
127 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800128
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800129 if (m_server != NULL) {
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800130 delete m_server;
131 m_server = NULL;
132 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800133
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800134 if (m_stateServer != NULL) {
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800135 delete m_stateServer;
136 m_stateServer = NULL;
137 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800138}
139
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800140void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800141Dispatcher::Did_LocalPrefix_Updated(const Ccnx::Name& forwardingHint)
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800142{
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800143 Name effectiveForwardingHint;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800144 if (m_localUserName.size() >= forwardingHint.size() &&
145 m_localUserName.getPartialName(0, forwardingHint.size()) == forwardingHint) {
146 effectiveForwardingHint = Name("/"); // "directly" accesible
147 }
148 else {
149 effectiveForwardingHint = forwardingHint;
150 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800151
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800152 Name oldLocalPrefix = m_syncLog->LookupLocalLocator();
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800153
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800154 if (oldLocalPrefix == effectiveForwardingHint) {
155 _LOG_DEBUG(
156 "Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint
157 << ", but effective prefix didn't change");
158 return;
159 }
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800160
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800161 if (effectiveForwardingHint == Name("/") || effectiveForwardingHint == Name(BROADCAST_DOMAIN)) {
162 _LOG_DEBUG("Basic effective prefix [" << effectiveForwardingHint
163 << "]. Updating local prefix, but don't reregister");
164 m_syncLog->UpdateLocalLocator(effectiveForwardingHint);
165 return;
166 }
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800167
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800168 _LOG_DEBUG("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint);
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800169
170 m_server->registerPrefix(effectiveForwardingHint);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800171 m_syncLog->UpdateLocalLocator(effectiveForwardingHint);
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800172
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800173 if (oldLocalPrefix == Name("/") || oldLocalPrefix == Name(BROADCAST_DOMAIN)) {
174 _LOG_DEBUG("Don't deregister basic prefix: " << oldLocalPrefix);
175 return;
176 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800177 m_server->deregisterPrefix(oldLocalPrefix);
178}
179
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800180// moved to state-server
181// void
182// Dispatcher::Restore_LocalFile (FileItemPtr file)
183// {
184// m_executor.execute (bind (&Dispatcher::Restore_LocalFile_Execute, this, file));
185// }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800186
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800187/////////////////////////////////////////////////////////////////////////////////////////////////////
188/////////////////////////////////////////////////////////////////////////////////////////////////////
189/////////////////////////////////////////////////////////////////////////////////////////////////////
190
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800191void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800192Dispatcher::Did_LocalFile_AddOrModify(const filesystem::path& relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800193{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800194 m_executor.execute(bind(&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800195}
196
197void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800198Dispatcher::Did_LocalFile_AddOrModify_Execute(filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800199{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800200 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800201 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800202 if (!filesystem::exists(absolutePath)) {
203 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
204 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
205 return;
206 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800207
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800208 FileItemPtr currentFile = m_fileState->LookupFile(relativeFilePath.generic_string());
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800209 if (currentFile &&
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800210 *Hash::FromFileContent(absolutePath) ==
211 Hash(currentFile->file_hash().c_str(), currentFile->file_hash().size())
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800212 // The following two are commented out to prevent front end from reporting intermediate files
213 // should enable it if there is other way to prevent this
214 // && last_write_time (absolutePath) == currentFile->mtime ()
215 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800216 ) {
217 _LOG_ERROR("Got notification about the same file [" << relativeFilePath << "]");
218 return;
219 }
220
221 if (currentFile && !currentFile->is_complete()) {
222 _LOG_ERROR("Got notification about incomplete file [" << relativeFilePath << "]");
223 return;
224 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800225
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800226
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800227 int seg_num;
228 HashPtr hash;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800229 tie(hash, seg_num) = m_objectManager.localFileToObjects(absolutePath, m_localUserName);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800230
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800231 try {
232 m_actionLog->AddLocalActionUpdate(relativeFilePath.generic_string(),
233 *hash,
234 last_write_time(absolutePath),
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700235#if BOOST_VERSION >= 104900
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800236 status(absolutePath).permissions(),
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700237#else
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800238 0,
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700239#endif
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800240 seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800241
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800242 // notify SyncCore to propagate the change
243 m_core->localStateChangedDelayed();
244 }
245 catch (filesystem::filesystem_error& error) {
246 _LOG_ERROR("File operations failed on [" << relativeFilePath << "] (ignoring)");
247 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800248}
249
250void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800251Dispatcher::Did_LocalFile_Delete(const filesystem::path& relativeFilePath)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800252{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800253 m_executor.execute(bind(&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800254}
255
256void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800257Dispatcher::Did_LocalFile_Delete_Execute(filesystem::path relativeFilePath)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800258{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800259 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800260 if (filesystem::exists(absolutePath)) {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800261 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
262 return;
263 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800264
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800265 m_actionLog->AddLocalActionDelete(relativeFilePath.generic_string());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800266 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800267 m_core->localStateChangedDelayed();
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800268}
269
270/////////////////////////////////////////////////////////////////////////////////////////////////////
271/////////////////////////////////////////////////////////////////////////////////////////////////////
272/////////////////////////////////////////////////////////////////////////////////////////////////////
273
274/**
275 * Callbacks:
276 *
277 * - from SyncLog: when state changes -> to fetch missing actions
278 *
279 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
280 * -> to add action to the action log
281 *
282 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
283 *
284 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
285 *
286 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
Lijing Wang6977ec42016-12-25 14:45:09 -0800287 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem.
288 * Don't do anything otherwise
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800289 */
290
291void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800292Dispatcher::Did_SyncLog_StateChange(SyncStateMsgPtr stateMsg)
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800293{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800294 m_executor.execute(bind(&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800295}
296
297void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800298Dispatcher::Did_SyncLog_StateChange_Execute(SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800299{
300 int size = stateMsg->state_size();
301 int index = 0;
302 // iterate and fetch the actions
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800303 for (; index < size; index++) {
304 SyncState state = stateMsg->state(index);
305 if (state.has_old_seq() && state.has_seq()) {
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800306 uint64_t oldSeq = state.old_seq();
307 uint64_t newSeq = state.seq();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800308 Name userName(reinterpret_cast<const unsigned char*>(state.name().c_str()),
309 state.name().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800310
311 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800312 Name actionNameBase = Name("/")(userName)(CHRONOSHARE_APP)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800313
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800314 m_actionFetcher->Enqueue(userName, actionNameBase, std::max<uint64_t>(oldSeq + 1, 1), newSeq,
315 FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800316 }
317 }
318}
319
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800320
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800321void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800322Dispatcher::Did_FetchManager_ActionFetch(const Ccnx::Name& deviceName,
323 const Ccnx::Name& actionBaseName, uint32_t seqno,
324 Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800325{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800326 /// @todo Errors and exception checking
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800327 _LOG_DEBUG("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName
328 << ", seqno: "
329 << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800330
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800331 ActionItemPtr action = m_actionLog->AddRemoteAction(deviceName, seqno, actionPco);
332 if (!action) {
333 _LOG_ERROR("AddRemoteAction did not insert action, ignoring");
334 return;
335 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800336 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800337
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800338 if (action->action() == ActionItem::UPDATE) {
339 Hash hash(action->file_hash().c_str(), action->file_hash().size());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800340
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800341 Name fileNameBase =
342 Name("/")(deviceName)(CHRONOSHARE_APP)("file")(hash.GetHash(), hash.GetHashBytes());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800343
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800344 string hashStr = lexical_cast<string>(hash);
345 if (ObjectDb::DoesExist(m_rootDir / ".chronoshare", deviceName, hashStr)) {
346 _LOG_DEBUG(
347 "File already exists in the database. No need to refetch, just directly applying the action");
348 Did_FetchManager_FileFetchComplete(deviceName, fileNameBase);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800349 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800350 else {
351 if (m_objectDbMap.find(hash) == m_objectDbMap.end()) {
352 _LOG_DEBUG("create ObjectDb for " << hash);
353 m_objectDbMap[hash] = make_shared<ObjectDb>(m_rootDir / ".chronoshare", hashStr);
354 }
355
356 m_fileFetcher->Enqueue(deviceName, fileNameBase, 0, action->seg_num() - 1,
357 FetchManager::PRIORITY_NORMAL);
358 }
359 }
Lijing Wang6977ec42016-12-25 14:45:09 -0800360 // if necessary (when version number is the highest) delete will be
361 // applied through the trigger in m_actionLog->AddRemoteAction call
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800362}
363
364void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800365Dispatcher::Did_ActionLog_ActionApply_Delete(const std::string& filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800366{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800367 m_executor.execute(bind(&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800368}
369
370void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800371Dispatcher::Did_ActionLog_ActionApply_Delete_Execute(std::string filename)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800372{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800373 _LOG_DEBUG("Action to delete " << filename);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800374
375 filesystem::path absolutePath = m_rootDir / filename;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800376 try {
377 if (filesystem::exists(absolutePath)) {
378 // need some protection from local detection of removal
379 remove(absolutePath);
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800380
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800381 // hack to remove empty parent dirs
382 filesystem::path parentPath = absolutePath.parent_path();
383 while (parentPath > m_rootDir) {
384 if (filesystem::is_empty(parentPath)) {
385 filesystem::remove(parentPath);
386 parentPath = parentPath.parent_path();
387 }
388 else {
389 break;
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800390 }
391 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800392 }
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800393 // don't exist
394 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800395 catch (filesystem::filesystem_error& error) {
396 _LOG_ERROR("File operations failed when removing [" << absolutePath << "] (ignoring)");
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800397 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800398}
399
400void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800401Dispatcher::Did_FetchManager_FileSegmentFetch(const Ccnx::Name& deviceName,
402 const Ccnx::Name& fileSegmentBaseName,
403 uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800404{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800405 m_executor.execute(bind(&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName,
406 fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800407}
408
409void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800410Dispatcher::Did_FetchManager_FileSegmentFetch_Execute(Ccnx::Name deviceName,
411 Ccnx::Name fileSegmentBaseName,
412 uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800413{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800414 // fileSegmentBaseName: /<device_name>/<appname>/file/<hash>
415
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800416 const Bytes& hashBytes = fileSegmentBaseName.getCompFromBack(0);
417 Hash hash(head(hashBytes), hashBytes.size());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800418
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800419 _LOG_DEBUG("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName
420 << ", segment: "
421 << segment);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800422
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800423 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800424
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800425 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find(hash);
426 if (db != m_objectDbMap.end()) {
427 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800428 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800429 else {
430 _LOG_ERROR("no db available for this content object: " << fileSegmentBaseName << ", size: "
431 << fileSegmentPco->buf().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800432 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800433
434 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
435 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800436}
437
438void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800439Dispatcher::Did_FetchManager_FileFetchComplete(const Ccnx::Name& deviceName,
440 const Ccnx::Name& fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800441{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800442 m_executor.execute(
443 bind(&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800444}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800445
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800446void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800447Dispatcher::Did_FetchManager_FileFetchComplete_Execute(Ccnx::Name deviceName, Ccnx::Name fileBaseName)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800448{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800449 // fileBaseName: /<device_name>/<appname>/file/<hash>
450
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800451 _LOG_DEBUG("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800452
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800453 const Bytes& hashBytes = fileBaseName.getCompFromBack(0);
454 Hash hash(head(hashBytes), hashBytes.size());
455 _LOG_DEBUG("Extracted hash: " << hash.shortHash());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800456
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800457 if (m_objectDbMap.find(hash) != m_objectDbMap.end()) {
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800458 // remove the db handle
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800459 m_objectDbMap.erase(hash); // to commit write
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800460 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800461 else {
462 _LOG_ERROR("no db available for this file: " << hash);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800463 }
464
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800465 FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash(hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800466
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800467 for (FileItems::iterator file = filesToAssemble->begin(); file != filesToAssemble->end(); file++) {
468 boost::filesystem::path filePath = m_rootDir / file->filename();
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700469
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800470 try {
471 if (filesystem::exists(filePath) && filesystem::last_write_time(filePath) == file->mtime() &&
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700472#if BOOST_VERSION >= 104900
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800473 filesystem::status(filePath).permissions() == static_cast<filesystem::perms>(file->mode()) &&
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700474#endif
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800475 *Hash::FromFileContent(filePath) == hash) {
476 _LOG_DEBUG("Asking to assemble a file, but file already exists on a filesystem");
477 continue;
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800478 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800479 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800480 catch (filesystem::filesystem_error& error) {
481 _LOG_ERROR("File operations failed on [" << filePath << "] (ignoring)");
482 }
483
484 if (ObjectDb::DoesExist(m_rootDir / ".chronoshare", deviceName,
485 boost::lexical_cast<string>(hash))) {
486 bool ok = m_objectManager.objectsToLocalFile(deviceName, hash, filePath);
487 if (ok) {
488 last_write_time(filePath, file->mtime());
489#if BOOST_VERSION >= 104900
490 permissions(filePath, static_cast<filesystem::perms>(file->mode()));
491#endif
492
493 m_fileState->SetFileComplete(file->filename());
494 }
495 else {
496 _LOG_ERROR("Notified about complete fetch, but file cannot be restored from the database: ["
497 << filePath
498 << "]");
499 }
500 }
501 else {
502 _LOG_ERROR(filePath << " supposed to have all segments, but not");
503 // should abort for debugging
504 }
505 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800506}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800507
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800508// moved to state-server
509// void
510// Dispatcher::Restore_LocalFile_Execute (FileItemPtr file)
511// {
512// _LOG_DEBUG ("Got request to restore local file [" << file->filename () << "]");
513// // the rest will gracefully fail if object-db is missing or incomplete
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800514
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800515// boost::filesystem::path filePath = m_rootDir / file->filename ();
516// Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
517// Hash hash (file->file_hash ().c_str (), file->file_hash ().size ());
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800518
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800519// try
520// {
521// if (filesystem::exists (filePath) &&
522// filesystem::last_write_time (filePath) == file->mtime () &&
523// filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
524// *Hash::FromFileContent (filePath) == hash)
525// {
526// _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
527// return;
528// }
529// }
530// catch (filesystem::filesystem_error &error)
531// {
532// _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
533// }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800534
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800535// m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800536
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800537// last_write_time (filePath, file->mtime ());
538// permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800539
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800540// }