blob: 717fadb195d964363c682c8fc80945a2581426df [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 Afanasyev026eaf32013-02-23 16:37:14 -080072 m_stateServer = new StateServer (make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName, m_sharedFolder, CHRONOSHARE_APP, m_objectManager, CONTENT_FRESHNESS);
73 // no need to register, right now only listening on localhost prefix
74
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080075 m_core = new SyncCore (m_syncLog, localUserName, Name("/"), syncPrefix,
Zhenkai Zhu95160102013-01-25 21:54:57 -080076 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx, DEFAULT_SYNC_INTEREST_INTERVAL);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080077
Zhenkai Zhuda686882013-01-29 22:32:24 -080078 FetchTaskDbPtr actionTaskDb = make_shared<FetchTaskDb>(m_rootDir, "action");
Alexander Afanasyev473346f2013-02-07 14:14:45 -080079 m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1),
80 Name(BROADCAST_DOMAIN), // no appname suffix now
81 3,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080082 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback(), actionTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080083
Zhenkai Zhuda686882013-01-29 22:32:24 -080084 FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file");
Alexander Afanasyev473346f2013-02-07 14:14:45 -080085 m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1),
86 Name(BROADCAST_DOMAIN), // no appname suffix now
87 3,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080088 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
89 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
90 fileTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080091
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080092
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080093 if (m_enablePrefixDiscovery)
94 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080095 _LOG_DEBUG("registering prefix discovery in Dispatcher");
96 string tag = "dispatcher" + m_localUserName.toString();
97 Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080098 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080099
100 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800101}
102
103Dispatcher::~Dispatcher()
104{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800105 _LOG_DEBUG ("Enter destructor of dispatcher");
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800106 m_executor.shutdown ();
107
108 // _LOG_DEBUG (">>");
109
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800110 if (m_enablePrefixDiscovery)
111 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800112 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
113 string tag = "dispatcher" + m_localUserName.toString();
114 Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800115 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800116
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800117 if (m_core != NULL)
118 {
119 delete m_core;
120 m_core = NULL;
121 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800122
123 if (m_server != NULL)
124 {
125 delete m_server;
126 m_server = NULL;
127 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800128
129 if (m_stateServer != NULL)
130 {
131 delete m_stateServer;
132 m_stateServer = NULL;
133 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800134}
135
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800136void
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800137Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &forwardingHint)
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800138{
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800139 Name effectiveForwardingHint;
140 if (m_localUserName.size () >= forwardingHint.size () &&
141 m_localUserName.getPartialName (0, forwardingHint.size ()) == forwardingHint)
142 {
143 effectiveForwardingHint = Name ("/"); // "directly" accesible
144 }
145 else
146 {
147 effectiveForwardingHint = forwardingHint;
148 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800149
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800150 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
151
152 if (oldLocalPrefix == effectiveForwardingHint)
153 {
154 _LOG_DEBUG ("Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint << ", but effective prefix didn't change");
155 return;
156 }
157
158 if (effectiveForwardingHint == Name ("/") ||
159 effectiveForwardingHint == Name (BROADCAST_DOMAIN))
160 {
161 _LOG_DEBUG ("Basic effective prefix [" << effectiveForwardingHint << "]. Updating local prefix, but don't reregister");
162 m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
163 return;
164 }
165
166 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint);
167
168 m_server->registerPrefix(effectiveForwardingHint);
169 m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
170
171 if (oldLocalPrefix == Name ("/") ||
172 oldLocalPrefix == Name (BROADCAST_DOMAIN))
173 {
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 Afanasyevf9978f82013-01-23 16:30:31 -0800192Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800193{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800194 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800195}
196
197void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -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 Afanasyevf2c16e02013-01-23 18:08:04 -0800202 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800203 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800204 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
205 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
206 return;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800207 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800208
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800209 FileItemPtr currentFile = m_fileState->LookupFile (relativeFilePath.generic_string ());
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800210 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800211 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
212 // 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 ())
216 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800217 {
218 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
219 return;
220 }
221
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800222 if (currentFile &&
223 !currentFile->is_complete ())
224 {
225 _LOG_ERROR ("Got notification about incomplete file [" << relativeFilePath << "]");
226 return;
227 }
228
229
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800230 int seg_num;
231 HashPtr hash;
232 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
233
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800234 try
235 {
236 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
237 *hash,
238 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800239
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800240 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800241 m_core->localStateChangedDelayed ();
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800242 }
243 catch (filesystem::filesystem_error &error)
244 {
245 _LOG_ERROR ("File operations failed on [" << relativeFilePath << "] (ignoring)");
246 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800247}
248
249void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800250Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
251{
252 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
253}
254
255void
256Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
257{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800258 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevd3310b12013-01-25 17:44:11 -0800259 if (filesystem::exists(absolutePath))
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800260 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800261 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800262 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800263 return;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800264 }
265
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800266 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
267 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800268 m_core->localStateChangedDelayed();
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800269}
270
271/////////////////////////////////////////////////////////////////////////////////////////////////////
272/////////////////////////////////////////////////////////////////////////////////////////////////////
273/////////////////////////////////////////////////////////////////////////////////////////////////////
274
275/**
276 * Callbacks:
277 *
278 * - from SyncLog: when state changes -> to fetch missing actions
279 *
280 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
281 * -> to add action to the action log
282 *
283 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
284 *
285 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
286 *
287 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
288 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
289 */
290
291void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800292Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
293{
294 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
295}
296
297void
298Dispatcher::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 Afanasyeve6f2ae12013-01-24 21:50:00 -0800303 for (; index < size; index++)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800304 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800305 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800306 if (state.has_old_seq() && state.has_seq())
307 {
308 uint64_t oldSeq = state.old_seq();
309 uint64_t newSeq = state.seq();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800310 Name userName (reinterpret_cast<const unsigned char *> (state.name ().c_str ()), state.name ().size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800311
312 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800313 Name actionNameBase = Name ("/")(userName)(CHRONOSHARE_APP)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800314
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800315 m_actionFetcher->Enqueue (userName, actionNameBase,
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800316 std::max<uint64_t> (oldSeq + 1, 1), newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800317 }
318 }
319}
320
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800321
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800322void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800323Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionBaseName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800324{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800325 /// @todo Errors and exception checking
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800326 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800327
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800328 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800329 if (!action)
330 {
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 Afanasyevf9978f82013-01-23 16:30:31 -0800336 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800337 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800338 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
339
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800340 Name fileNameBase = Name ("/")(deviceName)(CHRONOSHARE_APP)("file")(hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800341
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800342 string hashStr = lexical_cast<string> (hash);
343 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, hashStr))
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800344 {
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800345 _LOG_DEBUG ("File already exists in the database. No need to refetch, just directly applying the action");
346 Did_FetchManager_FileFetchComplete (deviceName, fileNameBase);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800347 }
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800348 else
349 {
350 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
351 {
352 _LOG_DEBUG ("create ObjectDb for " << hash);
353 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir / ".chronoshare", hashStr);
354 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800355
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800356 m_fileFetcher->Enqueue (deviceName, fileNameBase,
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800357 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
358 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800359 }
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800360 // 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 -0800361}
362
363void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800364Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800365{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800366 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
367}
368
369void
370Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
371{
372 _LOG_DEBUG ("Action to delete " << filename);
373
374 filesystem::path absolutePath = m_rootDir / filename;
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800375 try
376 {
377 if (filesystem::exists(absolutePath))
378 {
379 // need some protection from local detection of removal
380 remove (absolutePath);
381
382 // hack to remove empty parent dirs
383 filesystem::path parentPath = absolutePath.parent_path();
384 while (parentPath > m_rootDir)
385 {
386 if (filesystem::is_empty(parentPath))
387 {
388 filesystem::remove(parentPath);
389 parentPath = parentPath.parent_path();
390 }
391 else
392 {
393 break;
394 }
395 }
396 }
397 // don't exist
398 }
399 catch (filesystem::filesystem_error &error)
400 {
401 _LOG_ERROR ("File operations failed when removing [" << absolutePath << "] (ignoring)");
402 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800403}
404
405void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800406Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800407{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800408 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800409}
410
411void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800412Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentBaseName, 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
416 const Bytes &hashBytes = fileSegmentBaseName.getCompFromBack (0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800417 Hash hash (head(hashBytes), hashBytes.size());
418
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800419 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment);
420
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800421 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800422
423 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
424 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800425 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800426 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800427 }
428 else
429 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800430 _LOG_ERROR ("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800431 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800432
433 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
434 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800435}
436
437void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800438Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800439{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800440 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
441}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800442
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800443void
444Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
445{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800446 // fileBaseName: /<device_name>/<appname>/file/<hash>
447
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800448 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800449
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800450 const Bytes &hashBytes = fileBaseName.getCompFromBack (0);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800451 Hash hash (head (hashBytes), hashBytes.size ());
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800452 _LOG_DEBUG ("Extracted hash: " << hash.shortHash ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800453
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800454 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
455 {
456 // remove the db handle
457 m_objectDbMap.erase (hash); // to commit write
458 }
459 else
460 {
461 _LOG_ERROR ("no db available for this file: " << hash);
462 }
463
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800464 FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800465
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800466 for (FileItems::iterator file = filesToAssemble->begin ();
467 file != filesToAssemble->end ();
468 file++)
469 {
470 boost::filesystem::path filePath = m_rootDir / file->filename ();
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800471
Alexander Afanasyevce925102013-01-31 17:04:05 -0800472 try
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800473 {
Alexander Afanasyevce925102013-01-31 17:04:05 -0800474 if (filesystem::exists (filePath) &&
475 filesystem::last_write_time (filePath) == file->mtime () &&
476 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
477 *Hash::FromFileContent (filePath) == hash)
478 {
479 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
480 continue;
481 }
482 }
483 catch (filesystem::filesystem_error &error)
484 {
485 _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800486 }
487
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800488 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, boost::lexical_cast<string>(hash)))
489 {
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800490 bool ok = m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
491 if (ok)
492 {
493 last_write_time (filePath, file->mtime ());
494 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800495
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800496 m_fileState->SetFileComplete (file->filename ());
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800497 }
498 else
499 {
500 _LOG_ERROR ("Notified about complete fetch, but file cannot be restored from the database: [" << filePath << "]");
501 }
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800502 }
503 else
504 {
505 _LOG_ERROR (filePath << " supposed to have all segments, but not");
506 // should abort for debugging
507 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800508 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800509}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800510
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800511// moved to state-server
512// void
513// Dispatcher::Restore_LocalFile_Execute (FileItemPtr file)
514// {
515// _LOG_DEBUG ("Got request to restore local file [" << file->filename () << "]");
516// // the rest will gracefully fail if object-db is missing or incomplete
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800517
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800518// boost::filesystem::path filePath = m_rootDir / file->filename ();
519// Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
520// Hash hash (file->file_hash ().c_str (), file->file_hash ().size ());
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800521
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800522// try
523// {
524// if (filesystem::exists (filePath) &&
525// filesystem::last_write_time (filePath) == file->mtime () &&
526// filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
527// *Hash::FromFileContent (filePath) == hash)
528// {
529// _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
530// return;
531// }
532// }
533// catch (filesystem::filesystem_error &error)
534// {
535// _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
536// }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800537
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800538// m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800539
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800540// last_write_time (filePath, file->mtime ());
541// permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800542
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800543// }