blob: cbd6b41dfe333917e69e3284495bbe9e4076ef2a [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 Afanasyevf9978f82013-01-23 16:30:31 -080036static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare";
Zhenkai Zhu126b21c2013-01-28 17:56:19 -080037static const int CONTENT_FRESHNESS = 1800; // seconds
38const static double DEFAULT_SYNC_INTEREST_INTERVAL = 10.0; // seconds;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080039
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080040Dispatcher::Dispatcher(const std::string &localUserName
41 , const std::string &sharedFolder
42 , const filesystem::path &rootDir
43 , Ccnx::CcnxWrapperPtr ccnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080044 , bool enablePrefixDiscovery
45 )
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080046 : m_ccnx(ccnx)
47 , m_core(NULL)
48 , m_rootDir(rootDir)
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080049 , m_executor(1) // creates problems with file assembly. need to ensure somehow that FinishExectute is called after all Segment_Execute finished
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080050 , m_objectManager(ccnx, rootDir)
51 , m_localUserName(localUserName)
52 , m_sharedFolder(sharedFolder)
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080053 , m_server(NULL)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080054 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080055{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080056 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
57 m_actionLog = make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080058 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
59 ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback
60 bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
Alexander Afanasyev1b15cc62013-01-22 17:00:40 -080061 Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080062
Zhenkai Zhu126b21c2013-01-28 17:56:19 -080063 // m_server needs a different ccnx face
64 m_server = new ContentServer(make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName, m_sharedFolder, CONTENT_FRESHNESS);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080065 m_server->registerPrefix(Name ("/"));
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080066 m_server->registerPrefix(Name(BROADCAST_DOMAIN));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080067
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080068 m_core = new SyncCore (m_syncLog, localUserName, Name ("/"), syncPrefix,
Zhenkai Zhu95160102013-01-25 21:54:57 -080069 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx, DEFAULT_SYNC_INTEREST_INTERVAL);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080070
Zhenkai Zhuda686882013-01-29 22:32:24 -080071 FetchTaskDbPtr actionTaskDb = make_shared<FetchTaskDb>(m_rootDir, "action");
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080072 m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3,
Zhenkai Zhuda686882013-01-29 22:32:24 -080073 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback(), actionTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080074
Zhenkai Zhuda686882013-01-29 22:32:24 -080075 FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file");
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080076 m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3,
77 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
Zhenkai Zhuda686882013-01-29 22:32:24 -080078 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
79 fileTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080080
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080081
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080082 if (m_enablePrefixDiscovery)
83 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080084 _LOG_DEBUG("registering prefix discovery in Dispatcher");
85 string tag = "dispatcher" + m_localUserName.toString();
86 Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080087 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080088
89 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080090}
91
92Dispatcher::~Dispatcher()
93{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080094 // _LOG_DEBUG ("Enter destructor of dispatcher");
95 m_executor.shutdown ();
96
97 // _LOG_DEBUG (">>");
98
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080099 if (m_enablePrefixDiscovery)
100 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800101 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
102 string tag = "dispatcher" + m_localUserName.toString();
103 Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800104 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800105
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800106 if (m_core != NULL)
107 {
108 delete m_core;
109 m_core = NULL;
110 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800111
112 if (m_server != NULL)
113 {
114 delete m_server;
115 m_server = NULL;
116 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800117}
118
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800119void
120Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &prefix)
121{
122 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
123 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << prefix);
124
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800125 m_server->registerPrefix(prefix);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800126 m_syncLog->UpdateLocalLocator (prefix);
127 m_server->deregisterPrefix(oldLocalPrefix);
128}
129
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800130void
131Dispatcher::Restore_LocalFile (FileItemPtr file)
132{
133 m_executor.execute (bind (&Dispatcher::Restore_LocalFile_Execute, this, file));
134}
135
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800136/////////////////////////////////////////////////////////////////////////////////////////////////////
137/////////////////////////////////////////////////////////////////////////////////////////////////////
138/////////////////////////////////////////////////////////////////////////////////////////////////////
139
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800140void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800141Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800142{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800143 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800144}
145
146void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800147Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800148{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800149 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800150 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800151 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800152 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800153 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
154 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
155 return;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800156 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800157
158 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
159 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800160 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
161 // The following two are commented out to prevent front end from reporting intermediate files
162 // should enable it if there is other way to prevent this
163 // && last_write_time (absolutePath) == currentFile->mtime ()
164 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
165 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800166 {
167 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
168 return;
169 }
170
171 int seg_num;
172 HashPtr hash;
173 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
174
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800175 try
176 {
177 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
178 *hash,
179 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800180
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800181 // notify SyncCore to propagate the change
182 m_core->localStateChanged();
183 }
184 catch (filesystem::filesystem_error &error)
185 {
186 _LOG_ERROR ("File operations failed on [" << relativeFilePath << "] (ignoring)");
187 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800188}
189
190void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800191Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
192{
193 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
194}
195
196void
197Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
198{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800199 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevd3310b12013-01-25 17:44:11 -0800200 if (filesystem::exists(absolutePath))
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800201 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800202 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800203 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800204 return;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800205 }
206
207 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
208 if (!currentFile)
209 {
210 _LOG_ERROR ("File already deleted [" << relativeFilePath << "]");
211 return;
212 }
213
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800214 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
215 // notify SyncCore to propagate the change
216 m_core->localStateChanged();
217}
218
219/////////////////////////////////////////////////////////////////////////////////////////////////////
220/////////////////////////////////////////////////////////////////////////////////////////////////////
221/////////////////////////////////////////////////////////////////////////////////////////////////////
222
223/**
224 * Callbacks:
225 *
226 * - from SyncLog: when state changes -> to fetch missing actions
227 *
228 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
229 * -> to add action to the action log
230 *
231 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
232 *
233 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
234 *
235 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
236 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
237 */
238
239void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800240Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
241{
242 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
243}
244
245void
246Dispatcher::Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800247{
248 int size = stateMsg->state_size();
249 int index = 0;
250 // iterate and fetch the actions
Alexander Afanasyeve6f2ae12013-01-24 21:50:00 -0800251 for (; index < size; index++)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800252 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800253 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800254 if (state.has_old_seq() && state.has_seq())
255 {
256 uint64_t oldSeq = state.old_seq();
257 uint64_t newSeq = state.seq();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800258 Name userName (reinterpret_cast<const unsigned char *> (state.name ().c_str ()), state.name ().size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800259
260 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800261 Name actionNameBase = Name(userName)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800262
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800263 m_actionFetcher->Enqueue (userName, actionNameBase,
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800264 std::max<uint64_t> (oldSeq + 1, 1), newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800265 }
266 }
267}
268
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800269
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800270void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800271Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionBaseName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800272{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800273 /// @todo Errors and exception checking
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800274 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800275
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800276 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
277 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800278
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800279 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800280 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800281 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
282
283 Name fileNameBase = Name (deviceName)("file")(hash.GetHash (), hash.GetHashBytes ());
284
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800285 string hashStr = lexical_cast<string> (hash);
286 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, hashStr))
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800287 {
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800288 _LOG_DEBUG ("File already exists in the database. No need to refetch, just directly applying the action");
289 Did_FetchManager_FileFetchComplete (deviceName, fileNameBase);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800290 }
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800291 else
292 {
293 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
294 {
295 _LOG_DEBUG ("create ObjectDb for " << hash);
296 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir / ".chronoshare", hashStr);
297 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800298
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800299 m_fileFetcher->Enqueue (deviceName, fileNameBase,
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800300 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
301 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800302 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800303}
304
305void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800306Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800307{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800308 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
309}
310
311void
312Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
313{
314 _LOG_DEBUG ("Action to delete " << filename);
315
316 filesystem::path absolutePath = m_rootDir / filename;
317 if (filesystem::exists(absolutePath))
318 {
319 // need some protection from local detection of removal
320 remove (absolutePath);
321 }
322 // don't exist
323}
324
325void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800326Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800327{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800328 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800329}
330
331void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800332Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800333{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800334 const Bytes &hashBytes = fileSegmentBaseName.getCompFromBack (0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800335 Hash hash (head(hashBytes), hashBytes.size());
336
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800337 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment);
338
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800339 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800340
341 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
342 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800343 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800344 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800345 }
346 else
347 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800348 _LOG_ERROR ("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800349 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800350
351 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
352 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800353}
354
355void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800356Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800357{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800358 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
359}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800360
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800361void
362Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
363{
364 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800365
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800366 const Bytes &hashBytes = fileBaseName.getCompFromBack (0);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800367 Hash hash (head (hashBytes), hashBytes.size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800368
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800369 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
370 {
371 // remove the db handle
372 m_objectDbMap.erase (hash); // to commit write
373 }
374 else
375 {
376 _LOG_ERROR ("no db available for this file: " << hash);
377 }
378
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800379 FileItemsPtr filesToAssemble = m_actionLog->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800380
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800381 for (FileItems::iterator file = filesToAssemble->begin ();
382 file != filesToAssemble->end ();
383 file++)
384 {
385 boost::filesystem::path filePath = m_rootDir / file->filename ();
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800386
387 if (filesystem::exists (filePath) &&
388 filesystem::last_write_time (filePath) == file->mtime () &&
389 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
390 *Hash::FromFileContent (filePath) == hash)
391 {
392 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
393 continue;
394 }
395
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800396 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
397
398 last_write_time (filePath, file->mtime ());
399 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800400
401 m_actionLog->SetFileComplete (file->filename ());
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800402 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800403}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800404
405void
406Dispatcher::Restore_LocalFile_Execute (FileItemPtr file)
407{
408 _LOG_DEBUG ("Got request to restore local file [" << file->filename () << "]");
409 // the rest will gracefully fail if object-db is missing or incomplete
410
411 boost::filesystem::path filePath = m_rootDir / file->filename ();
412 Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
413 Hash hash (file->file_hash ().c_str (), file->file_hash ().size ());
414
415 if (filesystem::exists (filePath) &&
416 filesystem::last_write_time (filePath) == file->mtime () &&
417 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
418 *Hash::FromFileContent (filePath) == hash)
419 {
420 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
421 return;
422 }
423
424 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
425
426 last_write_time (filePath, file->mtime ());
427 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
428
429}