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