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