Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 23 | #include "logging.h" |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 24 | #include "ccnx-discovery.h" |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 25 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 26 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 28 | |
| 29 | using namespace Ccnx; |
| 30 | using namespace std; |
| 31 | using namespace boost; |
| 32 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 33 | INIT_LOGGER ("Dispatcher"); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 35 | static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare"; |
| 36 | |
| 37 | Dispatcher::Dispatcher(const filesystem::path &path, const std::string &localUserName, |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 38 | const std::string &sharedFolder, |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 39 | const filesystem::path &rootDir, Ccnx::CcnxWrapperPtr ccnx, |
Alexander Afanasyev | d94a8c6 | 2013-01-24 13:53:40 -0800 | [diff] [blame] | 40 | int poolSize) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 41 | : m_ccnx(ccnx) |
| 42 | , m_core(NULL) |
| 43 | , m_rootDir(rootDir) |
| 44 | , m_executor(poolSize) |
| 45 | , m_objectManager(ccnx, rootDir) |
| 46 | , m_localUserName(localUserName) |
| 47 | , m_sharedFolder(sharedFolder) |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 48 | , m_server(NULL) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 49 | { |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 50 | m_syncLog = make_shared<SyncLog>(path, localUserName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 51 | m_actionLog = make_shared<ActionLog>(m_ccnx, path, m_syncLog, sharedFolder, |
| 52 | // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7), |
| 53 | ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback |
| 54 | bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1)); |
Alexander Afanasyev | 1b15cc6 | 2013-01-22 17:00:40 -0800 | [diff] [blame] | 55 | Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder); |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 56 | |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 57 | m_server = new ContentServer(m_ccnx, m_actionLog, rootDir); |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 58 | m_server->registerPrefix(Name ("/")); |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 59 | m_server->registerPrefix(syncPrefix); |
| 60 | |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 61 | m_core = new SyncCore (m_syncLog, localUserName, Name ("/"), syncPrefix, |
Alexander Afanasyev | d94a8c6 | 2013-01-24 13:53:40 -0800 | [diff] [blame] | 62 | bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx); |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 64 | m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3); |
| 65 | m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3); |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 66 | |
| 67 | Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), "dispatcher")); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | Dispatcher::~Dispatcher() |
| 71 | { |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 72 | Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), "dispatcher")); |
| 73 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 74 | if (m_core != NULL) |
| 75 | { |
| 76 | delete m_core; |
| 77 | m_core = NULL; |
| 78 | } |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 79 | |
| 80 | if (m_server != NULL) |
| 81 | { |
| 82 | delete m_server; |
| 83 | m_server = NULL; |
| 84 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 87 | void |
| 88 | Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &prefix) |
| 89 | { |
| 90 | Name oldLocalPrefix = m_syncLog->LookupLocalLocator (); |
| 91 | _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << prefix); |
| 92 | |
| 93 | m_server->deregisterPrefix(prefix); |
| 94 | m_syncLog->UpdateLocalLocator (prefix); |
| 95 | m_server->deregisterPrefix(oldLocalPrefix); |
| 96 | } |
| 97 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 98 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 99 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 100 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 101 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 102 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 103 | Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 104 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 105 | m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath)); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 109 | Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 110 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 111 | filesystem::path absolutePath = m_rootDir / relativeFilePath; |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 112 | if (!filesystem::exists(absolutePath)) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 113 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 114 | BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() )); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 115 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 116 | |
| 117 | FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ()); |
| 118 | if (currentFile && |
Zhenkai Zhu | 1c036bf | 2013-01-24 15:01:17 -0800 | [diff] [blame^] | 119 | *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ()) |
| 120 | // The following two are commented out to prevent front end from reporting intermediate files |
| 121 | // should enable it if there is other way to prevent this |
| 122 | // && last_write_time (absolutePath) == currentFile->mtime () |
| 123 | // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ()) |
| 124 | ) |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 125 | { |
| 126 | _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]"); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | int seg_num; |
| 131 | HashPtr hash; |
| 132 | tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName); |
| 133 | |
| 134 | m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(), |
| 135 | *hash, |
| 136 | last_write_time (absolutePath), status (absolutePath).permissions (), seg_num); |
| 137 | |
| 138 | // notify SyncCore to propagate the change |
| 139 | m_core->localStateChanged(); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 143 | Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath) |
| 144 | { |
| 145 | m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath)); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath) |
| 150 | { |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 151 | filesystem::path absolutePath = m_rootDir / relativeFilePath; |
| 152 | if (!filesystem::exists(absolutePath)) |
| 153 | { |
| 154 | BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() )); |
| 155 | } |
| 156 | |
| 157 | FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ()); |
| 158 | if (!currentFile) |
| 159 | { |
| 160 | _LOG_ERROR ("File already deleted [" << relativeFilePath << "]"); |
| 161 | return; |
| 162 | } |
| 163 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 164 | m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string()); |
| 165 | // notify SyncCore to propagate the change |
| 166 | m_core->localStateChanged(); |
| 167 | } |
| 168 | |
| 169 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 170 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 171 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 172 | |
| 173 | /** |
| 174 | * Callbacks: |
| 175 | * |
| 176 | * - from SyncLog: when state changes -> to fetch missing actions |
| 177 | * |
| 178 | * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action |
| 179 | * -> to add action to the action log |
| 180 | * |
| 181 | * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file |
| 182 | * |
| 183 | * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing? |
| 184 | * |
| 185 | * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb |
| 186 | * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise |
| 187 | */ |
| 188 | |
| 189 | void |
| 190 | Dispatcher::Did_SyncLog_StateChange (const SyncStateMsgPtr &stateMsg) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 191 | { |
| 192 | int size = stateMsg->state_size(); |
| 193 | int index = 0; |
| 194 | // iterate and fetch the actions |
| 195 | while (index < size) |
| 196 | { |
| 197 | SyncState state = stateMsg->state(index); |
| 198 | if (state.has_old_seq() && state.has_seq()) |
| 199 | { |
| 200 | uint64_t oldSeq = state.old_seq(); |
| 201 | uint64_t newSeq = state.seq(); |
| 202 | Name userName = state.name(); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 203 | |
| 204 | // fetch actions with oldSeq + 1 to newSeq (inclusive) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 205 | Name actionNameBase = Name(userName)("action")(m_sharedFolder); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 206 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 207 | m_actionFetcher->Enqueue (userName, actionNameBase, |
| 208 | bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback (), |
| 209 | oldSeq + 1, newSeq, FetchManager::PRIORITY_HIGH); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 215 | Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionName, uint32_t seqno, Ccnx::PcoPtr actionPco) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 216 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 217 | /// @todo Errors and exception checking |
| 218 | _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionName: " << actionName << ", seqno: " << seqno); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 219 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 220 | ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco); |
| 221 | // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 223 | if (action->action () == ActionItem::UPDATE) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 224 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 225 | Hash hash (action->file_hash ().c_str(), action->file_hash ().size ()); |
| 226 | |
| 227 | Name fileNameBase = Name (deviceName)("file")(hash.GetHash (), hash.GetHashBytes ()); |
| 228 | |
| 229 | if (m_objectDbMap.find (hash) == m_objectDbMap.end ()) |
| 230 | { |
| 231 | m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir, lexical_cast<string> (hash)); |
| 232 | } |
| 233 | |
| 234 | m_fileFetcher->Enqueue (deviceName, fileNameBase, |
| 235 | bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4), |
| 236 | bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2), |
Zhenkai Zhu | 1c036bf | 2013-01-24 15:01:17 -0800 | [diff] [blame^] | 237 | 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 238 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 242 | Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 243 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 244 | m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename)); |
| 245 | } |
| 246 | |
| 247 | void |
| 248 | Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename) |
| 249 | { |
| 250 | _LOG_DEBUG ("Action to delete " << filename); |
| 251 | |
| 252 | filesystem::path absolutePath = m_rootDir / filename; |
| 253 | if (filesystem::exists(absolutePath)) |
| 254 | { |
| 255 | // need some protection from local detection of removal |
| 256 | remove (absolutePath); |
| 257 | } |
| 258 | // don't exist |
| 259 | } |
| 260 | |
| 261 | void |
| 262 | Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco) |
| 263 | { |
| 264 | m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentName, segment, fileSegmentPco)); |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco) |
| 269 | { |
| 270 | const Bytes &hashBytes = fileSegmentName.getCompFromBack (1); |
| 271 | Hash hash (head(hashBytes), hashBytes.size()); |
| 272 | |
| 273 | _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentName: " << fileSegmentName << ", segment: " << segment); |
| 274 | |
| 275 | map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash); |
| 276 | if (db != m_objectDbMap.end()) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 277 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 278 | db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 279 | } |
| 280 | else |
| 281 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 282 | _LOG_ERROR ("no db available for this content object: " << fileSegmentName << ", size: " << fileSegmentPco->buf ().size()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 287 | Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 288 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 289 | m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName)); |
| 290 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 291 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 292 | void |
| 293 | Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName) |
| 294 | { |
| 295 | _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 296 | |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 297 | const Bytes &hashBytes = fileBaseName.getCompFromBack (1); |
| 298 | Hash hash (head (hashBytes), hashBytes.size ()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 299 | |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 300 | FileItemsPtr filesToAssemble = m_actionLog->LookupFilesForHash (hash); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 301 | |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 302 | for (FileItems::iterator file = filesToAssemble->begin (); |
| 303 | file != filesToAssemble->end (); |
| 304 | file++) |
| 305 | { |
| 306 | boost::filesystem::path filePath = m_rootDir / file->filename (); |
| 307 | m_objectManager.objectsToLocalFile (deviceName, hash, filePath); |
| 308 | |
| 309 | last_write_time (filePath, file->mtime ()); |
| 310 | permissions (filePath, static_cast<filesystem::perms> (file->mode ())); |
| 311 | } |
| 312 | |
| 313 | if (m_objectDbMap.find (hash) != m_objectDbMap.end()) |
| 314 | { |
| 315 | // remove the db handle |
| 316 | m_objectDbMap.erase (hash); |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | _LOG_ERROR ("no db available for this file: " << hash); |
| 321 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 322 | } |