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