blob: 5d5d96525919947e7ce9a39da7f7008735944a05 [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";
Zhenkai Zhu126b21c2013-01-28 17:56:19 -080036static const int CONTENT_FRESHNESS = 1800; // seconds
37const static double DEFAULT_SYNC_INTEREST_INTERVAL = 10.0; // seconds;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080038
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080039Dispatcher::Dispatcher(const std::string &localUserName
40 , const std::string &sharedFolder
41 , const filesystem::path &rootDir
42 , Ccnx::CcnxWrapperPtr ccnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080043 , bool enablePrefixDiscovery
44 )
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080045 : m_ccnx(ccnx)
46 , m_core(NULL)
47 , m_rootDir(rootDir)
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080048 , 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 -080049 , m_objectManager(ccnx, rootDir)
50 , m_localUserName(localUserName)
51 , m_sharedFolder(sharedFolder)
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080052 , m_server(NULL)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080053 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080054{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080055 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
56 m_actionLog = make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080057 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
58 ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback
59 bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
Alexander Afanasyev1b15cc62013-01-22 17:00:40 -080060 Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080061
Zhenkai Zhu126b21c2013-01-28 17:56:19 -080062 // m_server needs a different ccnx face
63 m_server = new ContentServer(make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName, m_sharedFolder, CONTENT_FRESHNESS);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080064 m_server->registerPrefix(Name ("/"));
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080065 m_server->registerPrefix(Name(BROADCAST_DOMAIN));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080066
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080067 m_core = new SyncCore (m_syncLog, localUserName, Name ("/"), syncPrefix,
Zhenkai Zhu95160102013-01-25 21:54:57 -080068 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx, DEFAULT_SYNC_INTEREST_INTERVAL);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080069
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080070 m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3,
71 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4));
72
73 m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3,
74 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
75 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2));
76
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080077
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080078 if (m_enablePrefixDiscovery)
79 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080080 _LOG_DEBUG("registering prefix discovery in Dispatcher");
81 string tag = "dispatcher" + m_localUserName.toString();
82 Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080083 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080084
85 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080086}
87
88Dispatcher::~Dispatcher()
89{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080090 // _LOG_DEBUG ("Enter destructor of dispatcher");
91 m_executor.shutdown ();
92
93 // _LOG_DEBUG (">>");
94
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080095 if (m_enablePrefixDiscovery)
96 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080097 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
98 string tag = "dispatcher" + m_localUserName.toString();
99 Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800100 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800101
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800102 if (m_core != NULL)
103 {
104 delete m_core;
105 m_core = NULL;
106 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800107
108 if (m_server != NULL)
109 {
110 delete m_server;
111 m_server = NULL;
112 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800113}
114
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800115void
116Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &prefix)
117{
118 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
119 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << prefix);
120
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800121 m_server->registerPrefix(prefix);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800122 m_syncLog->UpdateLocalLocator (prefix);
123 m_server->deregisterPrefix(oldLocalPrefix);
124}
125
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800126/////////////////////////////////////////////////////////////////////////////////////////////////////
127/////////////////////////////////////////////////////////////////////////////////////////////////////
128/////////////////////////////////////////////////////////////////////////////////////////////////////
129
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800130void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800131Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800132{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800133 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800134}
135
136void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800137Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800138{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800139 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800140 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800141 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800142 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800143 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
144 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
145 return;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800146 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800147
148 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
149 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800150 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
151 // The following two are commented out to prevent front end from reporting intermediate files
152 // should enable it if there is other way to prevent this
153 // && last_write_time (absolutePath) == currentFile->mtime ()
154 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
155 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800156 {
157 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
158 return;
159 }
160
161 int seg_num;
162 HashPtr hash;
163 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
164
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800165 try
166 {
167 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
168 *hash,
169 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800170
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800171 // notify SyncCore to propagate the change
172 m_core->localStateChanged();
173 }
174 catch (filesystem::filesystem_error &error)
175 {
176 _LOG_ERROR ("File operations failed on [" << relativeFilePath << "] (ignoring)");
177 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800178}
179
180void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800181Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
182{
183 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
184}
185
186void
187Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
188{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800189 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevd3310b12013-01-25 17:44:11 -0800190 if (filesystem::exists(absolutePath))
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800191 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800192 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800193 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800194 return;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800195 }
196
197 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
198 if (!currentFile)
199 {
200 _LOG_ERROR ("File already deleted [" << relativeFilePath << "]");
201 return;
202 }
203
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800204 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
205 // notify SyncCore to propagate the change
206 m_core->localStateChanged();
207}
208
209/////////////////////////////////////////////////////////////////////////////////////////////////////
210/////////////////////////////////////////////////////////////////////////////////////////////////////
211/////////////////////////////////////////////////////////////////////////////////////////////////////
212
213/**
214 * Callbacks:
215 *
216 * - from SyncLog: when state changes -> to fetch missing actions
217 *
218 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
219 * -> to add action to the action log
220 *
221 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
222 *
223 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
224 *
225 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
226 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
227 */
228
229void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800230Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
231{
232 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
233}
234
235void
236Dispatcher::Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800237{
238 int size = stateMsg->state_size();
239 int index = 0;
240 // iterate and fetch the actions
Alexander Afanasyeve6f2ae12013-01-24 21:50:00 -0800241 for (; index < size; index++)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800242 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800243 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800244 if (state.has_old_seq() && state.has_seq())
245 {
246 uint64_t oldSeq = state.old_seq();
247 uint64_t newSeq = state.seq();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800248 Name userName (reinterpret_cast<const unsigned char *> (state.name ().c_str ()), state.name ().size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800249
250 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800251 Name actionNameBase = Name(userName)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800252
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800253 m_actionFetcher->Enqueue (userName, actionNameBase,
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800254 std::max<uint64_t> (oldSeq + 1, 1), newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800255 }
256 }
257}
258
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800259
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800260void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800261Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionBaseName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800262{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800263 /// @todo Errors and exception checking
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800264 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800265
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800266 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
267 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800268
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800269 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800270 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800271 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
272
273 Name fileNameBase = Name (deviceName)("file")(hash.GetHash (), hash.GetHashBytes ());
274
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800275 string hashStr = lexical_cast<string> (hash);
276 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, hashStr))
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800277 {
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800278 _LOG_DEBUG ("File already exists in the database. No need to refetch, just directly applying the action");
279 Did_FetchManager_FileFetchComplete (deviceName, fileNameBase);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800280 }
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800281 else
282 {
283 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
284 {
285 _LOG_DEBUG ("create ObjectDb for " << hash);
286 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir / ".chronoshare", hashStr);
287 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800288
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800289 m_fileFetcher->Enqueue (deviceName, fileNameBase,
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800290 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
291 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800292 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800293}
294
295void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800296Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800297{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800298 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
299}
300
301void
302Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
303{
304 _LOG_DEBUG ("Action to delete " << filename);
305
306 filesystem::path absolutePath = m_rootDir / filename;
307 if (filesystem::exists(absolutePath))
308 {
309 // need some protection from local detection of removal
310 remove (absolutePath);
311 }
312 // don't exist
313}
314
315void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800316Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800317{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800318 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800319}
320
321void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800322Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800323{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800324 const Bytes &hashBytes = fileSegmentBaseName.getCompFromBack (0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800325 Hash hash (head(hashBytes), hashBytes.size());
326
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800327 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment);
328
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800329 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800330
331 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
332 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800333 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800334 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800335 }
336 else
337 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800338 _LOG_ERROR ("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800339 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800340
341 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
342 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800343}
344
345void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800346Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800347{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800348 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
349}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800350
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800351void
352Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
353{
354 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800355
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800356 const Bytes &hashBytes = fileBaseName.getCompFromBack (0);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800357 Hash hash (head (hashBytes), hashBytes.size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800358
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800359 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
360 {
361 // remove the db handle
362 m_objectDbMap.erase (hash); // to commit write
363 }
364 else
365 {
366 _LOG_ERROR ("no db available for this file: " << hash);
367 }
368
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800369 FileItemsPtr filesToAssemble = m_actionLog->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800370
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800371 for (FileItems::iterator file = filesToAssemble->begin ();
372 file != filesToAssemble->end ();
373 file++)
374 {
375 boost::filesystem::path filePath = m_rootDir / file->filename ();
376 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
377
378 last_write_time (filePath, file->mtime ());
379 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
380 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800381}