blob: f4cf57d3706921039b9e6c4e895c2bb794144614 [file] [log] [blame]
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22#include "dispatcher.h"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080023#include "logging.h"
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080024#include "ccnx-discovery.h"
Zhenkai Zhuda686882013-01-29 22:32:24 -080025#include "fetch-task-db.h"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080026
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080027#include <boost/make_shared.hpp>
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080028#include <boost/lexical_cast.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080029
30using namespace Ccnx;
31using namespace std;
32using namespace boost;
33
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080034INIT_LOGGER ("Dispatcher");
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080035
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080036static const string CHRONOSHARE_APP = "chronoshare";
37static const string BROADCAST_DOMAIN = "/ndn/broadcast";
38
Zhenkai Zhu126b21c2013-01-28 17:56:19 -080039static const int CONTENT_FRESHNESS = 1800; // seconds
40const static double DEFAULT_SYNC_INTEREST_INTERVAL = 10.0; // seconds;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080041
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080042Dispatcher::Dispatcher(const std::string &localUserName
43 , const std::string &sharedFolder
44 , const filesystem::path &rootDir
45 , Ccnx::CcnxWrapperPtr ccnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080046 , bool enablePrefixDiscovery
47 )
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080048 : m_ccnx(ccnx)
49 , m_core(NULL)
50 , m_rootDir(rootDir)
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080051 , m_executor(1) // creates problems with file assembly. need to ensure somehow that FinishExectute is called after all Segment_Execute finished
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080052 , m_objectManager(ccnx, rootDir, CHRONOSHARE_APP)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080053 , m_localUserName(localUserName)
54 , m_sharedFolder(sharedFolder)
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080055 , m_server(NULL)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080056 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080057{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080058 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080059 m_actionLog = make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder, CHRONOSHARE_APP,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080060 // 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));
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080063 m_fileState = m_actionLog->GetFileState ();
64
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080065 Name syncPrefix = Name(BROADCAST_DOMAIN)(CHRONOSHARE_APP)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080066
Zhenkai Zhu126b21c2013-01-28 17:56:19 -080067 // m_server needs a different ccnx face
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080068 m_server = new ContentServer(make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName, m_sharedFolder, CHRONOSHARE_APP, CONTENT_FRESHNESS);
69 m_server->registerPrefix(Name("/"));
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080070 m_server->registerPrefix(Name(BROADCAST_DOMAIN));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080071
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080072 m_core = new SyncCore (m_syncLog, localUserName, Name("/"), syncPrefix,
Zhenkai Zhu95160102013-01-25 21:54:57 -080073 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx, DEFAULT_SYNC_INTEREST_INTERVAL);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080074
Zhenkai Zhuda686882013-01-29 22:32:24 -080075 FetchTaskDbPtr actionTaskDb = make_shared<FetchTaskDb>(m_rootDir, "action");
Alexander Afanasyev473346f2013-02-07 14:14:45 -080076 m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1),
77 Name(BROADCAST_DOMAIN), // no appname suffix now
78 3,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080079 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback(), actionTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080080
Zhenkai Zhuda686882013-01-29 22:32:24 -080081 FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file");
Alexander Afanasyev473346f2013-02-07 14:14:45 -080082 m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1),
83 Name(BROADCAST_DOMAIN), // no appname suffix now
84 3,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080085 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
86 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
87 fileTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080088
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080089
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080090 if (m_enablePrefixDiscovery)
91 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080092 _LOG_DEBUG("registering prefix discovery in Dispatcher");
93 string tag = "dispatcher" + m_localUserName.toString();
94 Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080095 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080096
97 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080098}
99
100Dispatcher::~Dispatcher()
101{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800102 _LOG_DEBUG ("Enter destructor of dispatcher");
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800103 m_executor.shutdown ();
104
105 // _LOG_DEBUG (">>");
106
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800107 if (m_enablePrefixDiscovery)
108 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800109 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
110 string tag = "dispatcher" + m_localUserName.toString();
111 Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800112 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800113
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800114 if (m_core != NULL)
115 {
116 delete m_core;
117 m_core = NULL;
118 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800119
120 if (m_server != NULL)
121 {
122 delete m_server;
123 m_server = NULL;
124 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800125}
126
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800127void
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800128Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &forwardingHint)
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800129{
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800130 Name effectiveForwardingHint;
131 if (m_localUserName.size () >= forwardingHint.size () &&
132 m_localUserName.getPartialName (0, forwardingHint.size ()) == forwardingHint)
133 {
134 effectiveForwardingHint = Name ("/"); // "directly" accesible
135 }
136 else
137 {
138 effectiveForwardingHint = forwardingHint;
139 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800140
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800141 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
142
143 if (oldLocalPrefix == effectiveForwardingHint)
144 {
145 _LOG_DEBUG ("Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint << ", but effective prefix didn't change");
146 return;
147 }
148
149 if (effectiveForwardingHint == Name ("/") ||
150 effectiveForwardingHint == Name (BROADCAST_DOMAIN))
151 {
152 _LOG_DEBUG ("Basic effective prefix [" << effectiveForwardingHint << "]. Updating local prefix, but don't reregister");
153 m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
154 return;
155 }
156
157 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint);
158
159 m_server->registerPrefix(effectiveForwardingHint);
160 m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
161
162 if (oldLocalPrefix == Name ("/") ||
163 oldLocalPrefix == Name (BROADCAST_DOMAIN))
164 {
165 _LOG_DEBUG ("Don't deregister basic prefix: " << oldLocalPrefix);
166 return;
167 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800168 m_server->deregisterPrefix(oldLocalPrefix);
169}
170
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800171void
172Dispatcher::Restore_LocalFile (FileItemPtr file)
173{
174 m_executor.execute (bind (&Dispatcher::Restore_LocalFile_Execute, this, file));
175}
176
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800177/////////////////////////////////////////////////////////////////////////////////////////////////////
178/////////////////////////////////////////////////////////////////////////////////////////////////////
179/////////////////////////////////////////////////////////////////////////////////////////////////////
180
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800181void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800182Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800183{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800184 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800185}
186
187void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800188Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800189{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800190 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800191 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800192 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800193 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800194 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
195 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
196 return;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800197 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800198
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800199 FileItemPtr currentFile = m_fileState->LookupFile (relativeFilePath.generic_string ());
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800200 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800201 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
202 // The following two are commented out to prevent front end from reporting intermediate files
203 // should enable it if there is other way to prevent this
204 // && last_write_time (absolutePath) == currentFile->mtime ()
205 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
206 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800207 {
208 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
209 return;
210 }
211
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800212 if (currentFile &&
213 !currentFile->is_complete ())
214 {
215 _LOG_ERROR ("Got notification about incomplete file [" << relativeFilePath << "]");
216 return;
217 }
218
219
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800220 int seg_num;
221 HashPtr hash;
222 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
223
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800224 try
225 {
226 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
227 *hash,
228 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800229
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800230 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800231 m_core->localStateChangedDelayed ();
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800232 }
233 catch (filesystem::filesystem_error &error)
234 {
235 _LOG_ERROR ("File operations failed on [" << relativeFilePath << "] (ignoring)");
236 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800237}
238
239void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800240Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
241{
242 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
243}
244
245void
246Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
247{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800248 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevd3310b12013-01-25 17:44:11 -0800249 if (filesystem::exists(absolutePath))
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800250 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800251 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800252 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800253 return;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800254 }
255
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800256 FileItemPtr currentFile = m_fileState->LookupFile (relativeFilePath.generic_string ());
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800257 if (!currentFile)
258 {
259 _LOG_ERROR ("File already deleted [" << relativeFilePath << "]");
260 return;
261 }
262
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800263 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
264 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800265 m_core->localStateChangedDelayed();
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800266}
267
268/////////////////////////////////////////////////////////////////////////////////////////////////////
269/////////////////////////////////////////////////////////////////////////////////////////////////////
270/////////////////////////////////////////////////////////////////////////////////////////////////////
271
272/**
273 * Callbacks:
274 *
275 * - from SyncLog: when state changes -> to fetch missing actions
276 *
277 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
278 * -> to add action to the action log
279 *
280 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
281 *
282 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
283 *
284 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
285 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
286 */
287
288void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800289Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
290{
291 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
292}
293
294void
295Dispatcher::Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800296{
297 int size = stateMsg->state_size();
298 int index = 0;
299 // iterate and fetch the actions
Alexander Afanasyeve6f2ae12013-01-24 21:50:00 -0800300 for (; index < size; index++)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800301 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800302 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800303 if (state.has_old_seq() && state.has_seq())
304 {
305 uint64_t oldSeq = state.old_seq();
306 uint64_t newSeq = state.seq();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800307 Name userName (reinterpret_cast<const unsigned char *> (state.name ().c_str ()), state.name ().size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800308
309 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800310 Name actionNameBase = Name ("/")(userName)(CHRONOSHARE_APP)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800311
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800312 m_actionFetcher->Enqueue (userName, actionNameBase,
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800313 std::max<uint64_t> (oldSeq + 1, 1), newSeq, 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 Afanasyev28ca3ed2013-01-24 23:17:15 -0800320Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionBaseName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800321{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800322 /// @todo Errors and exception checking
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800323 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800324
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800325 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800326 if (!action)
327 {
328 _LOG_ERROR ("AddRemoteAction did not insert action, ignoring");
329 return;
330 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800331 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800332
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800333 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800334 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800335 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
336
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800337 Name fileNameBase = Name ("/")(deviceName)(CHRONOSHARE_APP)("file")(hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800338
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800339 string hashStr = lexical_cast<string> (hash);
340 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, hashStr))
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800341 {
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800342 _LOG_DEBUG ("File already exists in the database. No need to refetch, just directly applying the action");
343 Did_FetchManager_FileFetchComplete (deviceName, fileNameBase);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800344 }
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800345 else
346 {
347 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
348 {
349 _LOG_DEBUG ("create ObjectDb for " << hash);
350 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir / ".chronoshare", hashStr);
351 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800352
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800353 m_fileFetcher->Enqueue (deviceName, fileNameBase,
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800354 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
355 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800356 }
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800357 // 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 -0800358}
359
360void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800361Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800362{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800363 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
364}
365
366void
367Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
368{
369 _LOG_DEBUG ("Action to delete " << filename);
370
371 filesystem::path absolutePath = m_rootDir / filename;
372 if (filesystem::exists(absolutePath))
373 {
374 // need some protection from local detection of removal
375 remove (absolutePath);
376 }
377 // don't exist
378}
379
380void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800381Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800382{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800383 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800384}
385
386void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800387Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800388{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800389 // fileSegmentBaseName: /<device_name>/<appname>/file/<hash>
390
391 const Bytes &hashBytes = fileSegmentBaseName.getCompFromBack (0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800392 Hash hash (head(hashBytes), hashBytes.size());
393
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800394 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment);
395
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800396 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800397
398 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
399 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800400 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800401 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800402 }
403 else
404 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800405 _LOG_ERROR ("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800406 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800407
408 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
409 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800410}
411
412void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800413Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800414{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800415 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
416}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800417
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800418void
419Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
420{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800421 // fileBaseName: /<device_name>/<appname>/file/<hash>
422
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800423 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800424
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800425 const Bytes &hashBytes = fileBaseName.getCompFromBack (0);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800426 Hash hash (head (hashBytes), hashBytes.size ());
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800427 _LOG_DEBUG ("Extracted hash: " << hash.shortHash ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800428
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800429 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
430 {
431 // remove the db handle
432 m_objectDbMap.erase (hash); // to commit write
433 }
434 else
435 {
436 _LOG_ERROR ("no db available for this file: " << hash);
437 }
438
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800439 FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800440
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800441 for (FileItems::iterator file = filesToAssemble->begin ();
442 file != filesToAssemble->end ();
443 file++)
444 {
445 boost::filesystem::path filePath = m_rootDir / file->filename ();
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800446
Alexander Afanasyevce925102013-01-31 17:04:05 -0800447 try
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800448 {
Alexander Afanasyevce925102013-01-31 17:04:05 -0800449 if (filesystem::exists (filePath) &&
450 filesystem::last_write_time (filePath) == file->mtime () &&
451 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
452 *Hash::FromFileContent (filePath) == hash)
453 {
454 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
455 continue;
456 }
457 }
458 catch (filesystem::filesystem_error &error)
459 {
460 _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800461 }
462
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800463 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, boost::lexical_cast<string>(hash)))
464 {
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800465 bool ok = m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
466 if (ok)
467 {
468 last_write_time (filePath, file->mtime ());
469 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800470
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800471 m_fileState->SetFileComplete (file->filename ());
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800472 }
473 else
474 {
475 _LOG_ERROR ("Notified about complete fetch, but file cannot be restored from the database: [" << filePath << "]");
476 }
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800477 }
478 else
479 {
480 _LOG_ERROR (filePath << " supposed to have all segments, but not");
481 // should abort for debugging
482 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800483 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800484}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800485
486void
487Dispatcher::Restore_LocalFile_Execute (FileItemPtr file)
488{
489 _LOG_DEBUG ("Got request to restore local file [" << file->filename () << "]");
490 // the rest will gracefully fail if object-db is missing or incomplete
491
492 boost::filesystem::path filePath = m_rootDir / file->filename ();
493 Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
494 Hash hash (file->file_hash ().c_str (), file->file_hash ().size ());
495
Alexander Afanasyevce925102013-01-31 17:04:05 -0800496 try
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800497 {
Alexander Afanasyevce925102013-01-31 17:04:05 -0800498 if (filesystem::exists (filePath) &&
499 filesystem::last_write_time (filePath) == file->mtime () &&
500 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
501 *Hash::FromFileContent (filePath) == hash)
502 {
503 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
504 return;
505 }
506 }
507 catch (filesystem::filesystem_error &error)
508 {
509 _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800510 }
511
512 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
513
514 last_write_time (filePath, file->mtime ());
515 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
516
517}