blob: b5e5668f22108ff4177991b42f17f80d2aec3dff [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"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080025
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080026#include <boost/make_shared.hpp>
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080027#include <boost/lexical_cast.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080028
29using namespace Ccnx;
30using namespace std;
31using namespace boost;
32
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080033INIT_LOGGER ("Dispatcher");
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080034
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080035static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare";
Alexander Afanasyevcb502052013-01-25 23:51:11 -080036const static double DEFAULT_SYNC_INTEREST_INTERVAL = 10.0;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080037
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080038Dispatcher::Dispatcher(const std::string &localUserName
39 , const std::string &sharedFolder
40 , const filesystem::path &rootDir
41 , Ccnx::CcnxWrapperPtr ccnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080042 , bool enablePrefixDiscovery
43 )
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080044 : m_ccnx(ccnx)
45 , m_core(NULL)
46 , m_rootDir(rootDir)
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080047 , 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 -080048 , m_objectManager(ccnx, rootDir)
49 , m_localUserName(localUserName)
50 , m_sharedFolder(sharedFolder)
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080051 , m_server(NULL)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080052 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080053{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080054 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
55 m_actionLog = make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080056 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
57 ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback
58 bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
Alexander Afanasyev1b15cc62013-01-22 17:00:40 -080059 Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080060
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080061 m_server = new ContentServer(m_ccnx, m_actionLog, rootDir, m_localUserName, m_sharedFolder);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080062 m_server->registerPrefix(Name ("/"));
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080063 m_server->registerPrefix(Name(BROADCAST_DOMAIN));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080064
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080065 m_core = new SyncCore (m_syncLog, localUserName, Name ("/"), syncPrefix,
Zhenkai Zhu95160102013-01-25 21:54:57 -080066 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx, DEFAULT_SYNC_INTEREST_INTERVAL);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080067
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080068 m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3);
69 m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080070
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080071 if (m_enablePrefixDiscovery)
72 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080073 _LOG_DEBUG("registering prefix discovery in Dispatcher");
74 string tag = "dispatcher" + m_localUserName.toString();
75 Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080076 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080077
78 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080079}
80
81Dispatcher::~Dispatcher()
82{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080083 // _LOG_DEBUG ("Enter destructor of dispatcher");
84 m_executor.shutdown ();
85
86 // _LOG_DEBUG (">>");
87
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080088 if (m_enablePrefixDiscovery)
89 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080090 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
91 string tag = "dispatcher" + m_localUserName.toString();
92 Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080093 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080094
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080095 if (m_core != NULL)
96 {
97 delete m_core;
98 m_core = NULL;
99 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800100
101 if (m_server != NULL)
102 {
103 delete m_server;
104 m_server = NULL;
105 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800106}
107
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800108void
109Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &prefix)
110{
111 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
112 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << prefix);
113
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800114 m_server->registerPrefix(prefix);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800115 m_syncLog->UpdateLocalLocator (prefix);
116 m_server->deregisterPrefix(oldLocalPrefix);
117}
118
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800119/////////////////////////////////////////////////////////////////////////////////////////////////////
120/////////////////////////////////////////////////////////////////////////////////////////////////////
121/////////////////////////////////////////////////////////////////////////////////////////////////////
122
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800123void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800124Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800125{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800126 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800127}
128
129void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800130Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800131{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800132 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800133 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800134 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800135 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800136 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
137 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
138 return;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800139 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800140
141 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
142 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800143 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
144 // The following two are commented out to prevent front end from reporting intermediate files
145 // should enable it if there is other way to prevent this
146 // && last_write_time (absolutePath) == currentFile->mtime ()
147 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
148 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800149 {
150 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
151 return;
152 }
153
154 int seg_num;
155 HashPtr hash;
156 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
157
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800158 try
159 {
160 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
161 *hash,
162 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800163
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800164 // notify SyncCore to propagate the change
165 m_core->localStateChanged();
166 }
167 catch (filesystem::filesystem_error &error)
168 {
169 _LOG_ERROR ("File operations failed on [" << relativeFilePath << "] (ignoring)");
170 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800171}
172
173void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800174Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
175{
176 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
177}
178
179void
180Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
181{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800182 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevd3310b12013-01-25 17:44:11 -0800183 if (filesystem::exists(absolutePath))
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800184 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800185 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800186 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800187 return;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800188 }
189
190 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
191 if (!currentFile)
192 {
193 _LOG_ERROR ("File already deleted [" << relativeFilePath << "]");
194 return;
195 }
196
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800197 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
198 // notify SyncCore to propagate the change
199 m_core->localStateChanged();
200}
201
202/////////////////////////////////////////////////////////////////////////////////////////////////////
203/////////////////////////////////////////////////////////////////////////////////////////////////////
204/////////////////////////////////////////////////////////////////////////////////////////////////////
205
206/**
207 * Callbacks:
208 *
209 * - from SyncLog: when state changes -> to fetch missing actions
210 *
211 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
212 * -> to add action to the action log
213 *
214 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
215 *
216 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
217 *
218 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
219 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
220 */
221
222void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800223Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
224{
225 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
226}
227
228void
229Dispatcher::Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800230{
231 int size = stateMsg->state_size();
232 int index = 0;
233 // iterate and fetch the actions
Alexander Afanasyeve6f2ae12013-01-24 21:50:00 -0800234 for (; index < size; index++)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800235 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800236 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800237 if (state.has_old_seq() && state.has_seq())
238 {
239 uint64_t oldSeq = state.old_seq();
240 uint64_t newSeq = state.seq();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800241 Name userName (reinterpret_cast<const unsigned char *> (state.name ().c_str ()), state.name ().size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800242
243 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800244 Name actionNameBase = Name(userName)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800245
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800246 m_actionFetcher->Enqueue (userName, actionNameBase,
247 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback (),
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800248 std::max<uint64_t> (oldSeq + 1, 1), newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800249 }
250 }
251}
252
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800253
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800254void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800255Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionBaseName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800256{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800257 /// @todo Errors and exception checking
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800258 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800259
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800260 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
261 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800262
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800263 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800264 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800265 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
266
267 Name fileNameBase = Name (deviceName)("file")(hash.GetHash (), hash.GetHashBytes ());
268
269 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
270 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800271 _LOG_DEBUG ("create ObjectDb for " << hash);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800272 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800273 }
274
275 m_fileFetcher->Enqueue (deviceName, fileNameBase,
276 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
277 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800278 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800279 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800280}
281
282void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800283Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800284{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800285 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
286}
287
288void
289Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
290{
291 _LOG_DEBUG ("Action to delete " << filename);
292
293 filesystem::path absolutePath = m_rootDir / filename;
294 if (filesystem::exists(absolutePath))
295 {
296 // need some protection from local detection of removal
297 remove (absolutePath);
298 }
299 // don't exist
300}
301
302void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800303Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800304{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800305 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800306}
307
308void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800309Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800310{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800311 const Bytes &hashBytes = fileSegmentBaseName.getCompFromBack (0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800312 Hash hash (head(hashBytes), hashBytes.size());
313
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800314 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment);
315
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800316 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800317
318 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
319 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800320 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800321 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800322 }
323 else
324 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800325 _LOG_ERROR ("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800326 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800327
328 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
329 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800330}
331
332void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800333Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800334{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800335 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
336}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800337
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800338void
339Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
340{
341 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800342
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800343 const Bytes &hashBytes = fileBaseName.getCompFromBack (0);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800344 Hash hash (head (hashBytes), hashBytes.size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800345
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800346 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
347 {
348 // remove the db handle
349 m_objectDbMap.erase (hash); // to commit write
350 }
351 else
352 {
353 _LOG_ERROR ("no db available for this file: " << hash);
354 }
355
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800356 FileItemsPtr filesToAssemble = m_actionLog->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800357
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800358 for (FileItems::iterator file = filesToAssemble->begin ();
359 file != filesToAssemble->end ();
360 file++)
361 {
362 boost::filesystem::path filePath = m_rootDir / file->filename ();
363 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
364
365 last_write_time (filePath, file->mtime ());
366 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
367 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800368}