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