blob: 38ba37e346826e8d0d4df2f66b4142bc23d4692e [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");
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080076 m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080077 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback(), actionTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080078
Zhenkai Zhuda686882013-01-29 22:32:24 -080079 FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file");
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080080 m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3,
81 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
82 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
83 fileTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080084
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080085
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080086 if (m_enablePrefixDiscovery)
87 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080088 _LOG_DEBUG("registering prefix discovery in Dispatcher");
89 string tag = "dispatcher" + m_localUserName.toString();
90 Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080091 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080092
93 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080094}
95
96Dispatcher::~Dispatcher()
97{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080098 _LOG_DEBUG ("Enter destructor of dispatcher");
Alexander Afanasyevfc720362013-01-24 21:49:48 -080099 m_executor.shutdown ();
100
101 // _LOG_DEBUG (">>");
102
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800103 if (m_enablePrefixDiscovery)
104 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800105 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
106 string tag = "dispatcher" + m_localUserName.toString();
107 Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800108 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800109
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800110 if (m_core != NULL)
111 {
112 delete m_core;
113 m_core = NULL;
114 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800115
116 if (m_server != NULL)
117 {
118 delete m_server;
119 m_server = NULL;
120 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800121}
122
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800123void
124Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &prefix)
125{
126 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
127 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << prefix);
128
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800129 m_server->registerPrefix(prefix);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800130 m_syncLog->UpdateLocalLocator (prefix);
131 m_server->deregisterPrefix(oldLocalPrefix);
132}
133
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800134void
135Dispatcher::Restore_LocalFile (FileItemPtr file)
136{
137 m_executor.execute (bind (&Dispatcher::Restore_LocalFile_Execute, this, file));
138}
139
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800140/////////////////////////////////////////////////////////////////////////////////////////////////////
141/////////////////////////////////////////////////////////////////////////////////////////////////////
142/////////////////////////////////////////////////////////////////////////////////////////////////////
143
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800144void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800145Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800146{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800147 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800148}
149
150void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800151Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800152{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800153 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800154 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800155 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800156 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800157 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
158 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
159 return;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800160 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800161
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800162 FileItemPtr currentFile = m_fileState->LookupFile (relativeFilePath.generic_string ());
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800163 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800164 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
165 // The following two are commented out to prevent front end from reporting intermediate files
166 // should enable it if there is other way to prevent this
167 // && last_write_time (absolutePath) == currentFile->mtime ()
168 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
169 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800170 {
171 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
172 return;
173 }
174
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800175 if (currentFile &&
176 !currentFile->is_complete ())
177 {
178 _LOG_ERROR ("Got notification about incomplete file [" << relativeFilePath << "]");
179 return;
180 }
181
182
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800183 int seg_num;
184 HashPtr hash;
185 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
186
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800187 try
188 {
189 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
190 *hash,
191 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800192
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800193 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800194 m_core->localStateChangedDelayed ();
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800195 }
196 catch (filesystem::filesystem_error &error)
197 {
198 _LOG_ERROR ("File operations failed on [" << relativeFilePath << "] (ignoring)");
199 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800200}
201
202void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800203Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
204{
205 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
206}
207
208void
209Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
210{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800211 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevd3310b12013-01-25 17:44:11 -0800212 if (filesystem::exists(absolutePath))
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800213 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800214 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800215 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800216 return;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800217 }
218
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800219 FileItemPtr currentFile = m_fileState->LookupFile (relativeFilePath.generic_string ());
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800220 if (!currentFile)
221 {
222 _LOG_ERROR ("File already deleted [" << relativeFilePath << "]");
223 return;
224 }
225
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800226 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
227 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800228 m_core->localStateChangedDelayed();
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800229}
230
231/////////////////////////////////////////////////////////////////////////////////////////////////////
232/////////////////////////////////////////////////////////////////////////////////////////////////////
233/////////////////////////////////////////////////////////////////////////////////////////////////////
234
235/**
236 * Callbacks:
237 *
238 * - from SyncLog: when state changes -> to fetch missing actions
239 *
240 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
241 * -> to add action to the action log
242 *
243 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
244 *
245 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
246 *
247 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
248 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
249 */
250
251void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800252Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
253{
254 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
255}
256
257void
258Dispatcher::Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800259{
260 int size = stateMsg->state_size();
261 int index = 0;
262 // iterate and fetch the actions
Alexander Afanasyeve6f2ae12013-01-24 21:50:00 -0800263 for (; index < size; index++)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800264 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800265 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800266 if (state.has_old_seq() && state.has_seq())
267 {
268 uint64_t oldSeq = state.old_seq();
269 uint64_t newSeq = state.seq();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800270 Name userName (reinterpret_cast<const unsigned char *> (state.name ().c_str ()), state.name ().size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800271
272 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800273 Name actionNameBase = Name ("/")(CHRONOSHARE_APP)(m_sharedFolder)("action")(userName);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800274
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800275 m_actionFetcher->Enqueue (userName, actionNameBase,
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800276 std::max<uint64_t> (oldSeq + 1, 1), newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800277 }
278 }
279}
280
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800281
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800282void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800283Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionBaseName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800284{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800285 /// @todo Errors and exception checking
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800286 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800287
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800288 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
289 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800290
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800291 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800292 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800293 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
294
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800295 Name fileNameBase = Name ("/")(CHRONOSHARE_APP)("file")(hash.GetHash (), hash.GetHashBytes ())(deviceName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800296
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800297 string hashStr = lexical_cast<string> (hash);
298 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, hashStr))
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800299 {
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800300 _LOG_DEBUG ("File already exists in the database. No need to refetch, just directly applying the action");
301 Did_FetchManager_FileFetchComplete (deviceName, fileNameBase);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800302 }
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800303 else
304 {
305 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
306 {
307 _LOG_DEBUG ("create ObjectDb for " << hash);
308 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir / ".chronoshare", hashStr);
309 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800310
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800311 m_fileFetcher->Enqueue (deviceName, fileNameBase,
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800312 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
313 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800314 }
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800315 // 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 -0800316}
317
318void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800319Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800320{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800321 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
322}
323
324void
325Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
326{
327 _LOG_DEBUG ("Action to delete " << filename);
328
329 filesystem::path absolutePath = m_rootDir / filename;
330 if (filesystem::exists(absolutePath))
331 {
332 // need some protection from local detection of removal
333 remove (absolutePath);
334 }
335 // don't exist
336}
337
338void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800339Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800340{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800341 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800342}
343
344void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800345Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800346{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800347 const Bytes &hashBytes = fileSegmentBaseName.getComp (2);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800348 Hash hash (head(hashBytes), hashBytes.size());
349
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800350 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment);
351
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800352 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800353
354 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
355 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800356 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800357 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800358 }
359 else
360 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800361 _LOG_ERROR ("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800362 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800363
364 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
365 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800366}
367
368void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800369Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800370{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800371 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
372}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800373
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800374void
375Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
376{
377 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800378
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800379 const Bytes &hashBytes = fileBaseName.getComp (2);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800380 Hash hash (head (hashBytes), hashBytes.size ());
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800381 _LOG_DEBUG ("Extracted hash: " << hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800382
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800383 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
384 {
385 // remove the db handle
386 m_objectDbMap.erase (hash); // to commit write
387 }
388 else
389 {
390 _LOG_ERROR ("no db available for this file: " << hash);
391 }
392
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800393 FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800394
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800395 for (FileItems::iterator file = filesToAssemble->begin ();
396 file != filesToAssemble->end ();
397 file++)
398 {
399 boost::filesystem::path filePath = m_rootDir / file->filename ();
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800400
Alexander Afanasyevce925102013-01-31 17:04:05 -0800401 try
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800402 {
Alexander Afanasyevce925102013-01-31 17:04:05 -0800403 if (filesystem::exists (filePath) &&
404 filesystem::last_write_time (filePath) == file->mtime () &&
405 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
406 *Hash::FromFileContent (filePath) == hash)
407 {
408 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
409 continue;
410 }
411 }
412 catch (filesystem::filesystem_error &error)
413 {
414 _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800415 }
416
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800417 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, boost::lexical_cast<string>(hash)))
418 {
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800419 bool ok = m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
420 if (ok)
421 {
422 last_write_time (filePath, file->mtime ());
423 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800424
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800425 m_fileState->SetFileComplete (file->filename ());
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800426 }
427 else
428 {
429 _LOG_ERROR ("Notified about complete fetch, but file cannot be restored from the database: [" << filePath << "]");
430 }
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800431 }
432 else
433 {
434 _LOG_ERROR (filePath << " supposed to have all segments, but not");
435 // should abort for debugging
436 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800437 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800438}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800439
440void
441Dispatcher::Restore_LocalFile_Execute (FileItemPtr file)
442{
443 _LOG_DEBUG ("Got request to restore local file [" << file->filename () << "]");
444 // the rest will gracefully fail if object-db is missing or incomplete
445
446 boost::filesystem::path filePath = m_rootDir / file->filename ();
447 Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
448 Hash hash (file->file_hash ().c_str (), file->file_hash ().size ());
449
Alexander Afanasyevce925102013-01-31 17:04:05 -0800450 try
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800451 {
Alexander Afanasyevce925102013-01-31 17:04:05 -0800452 if (filesystem::exists (filePath) &&
453 filesystem::last_write_time (filePath) == file->mtime () &&
454 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
455 *Hash::FromFileContent (filePath) == hash)
456 {
457 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
458 return;
459 }
460 }
461 catch (filesystem::filesystem_error &error)
462 {
463 _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800464 }
465
466 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
467
468 last_write_time (filePath, file->mtime ());
469 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
470
471}