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