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" |
| 24 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 25 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 27 | |
| 28 | using namespace Ccnx; |
| 29 | using namespace std; |
| 30 | using namespace boost; |
| 31 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 32 | INIT_LOGGER ("Dispatcher"); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 34 | static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare"; |
| 35 | |
| 36 | Dispatcher::Dispatcher(const filesystem::path &path, const std::string &localUserName, |
| 37 | const Ccnx::Name &localPrefix, const std::string &sharedFolder, |
| 38 | const filesystem::path &rootDir, Ccnx::CcnxWrapperPtr ccnx, |
| 39 | SchedulerPtr scheduler, int poolSize) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 40 | : m_ccnx(ccnx) |
| 41 | , m_core(NULL) |
| 42 | , m_rootDir(rootDir) |
| 43 | , m_executor(poolSize) |
| 44 | , m_objectManager(ccnx, rootDir) |
| 45 | , m_localUserName(localUserName) |
| 46 | , m_sharedFolder(sharedFolder) |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 47 | , m_server(NULL) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 48 | { |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 49 | m_syncLog = make_shared<SyncLog>(path, localUserName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 50 | m_actionLog = make_shared<ActionLog>(m_ccnx, path, m_syncLog, sharedFolder, |
| 51 | // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7), |
| 52 | ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback |
| 53 | bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1)); |
Alexander Afanasyev | 1b15cc6 | 2013-01-22 17:00:40 -0800 | [diff] [blame] | 54 | Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder); |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 55 | |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 56 | m_server = new ContentServer(m_ccnx, m_actionLog, rootDir); |
| 57 | m_server->registerPrefix(localPrefix); |
| 58 | m_server->registerPrefix(syncPrefix); |
| 59 | |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 60 | m_core = new SyncCore (m_syncLog, localUserName, localPrefix, syncPrefix, |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 61 | bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx, scheduler); |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 63 | m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3); |
| 64 | m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Dispatcher::~Dispatcher() |
| 68 | { |
| 69 | if (m_core != NULL) |
| 70 | { |
| 71 | delete m_core; |
| 72 | m_core = NULL; |
| 73 | } |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 74 | |
| 75 | if (m_server != NULL) |
| 76 | { |
| 77 | delete m_server; |
| 78 | m_server = NULL; |
| 79 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 82 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 83 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 84 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 85 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 86 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 87 | Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 88 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 89 | m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath)); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 93 | Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 94 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 95 | filesystem::path absolutePath = m_rootDir / relativeFilePath; |
| 96 | if (filesystem::exists(absolutePath)) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 97 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 98 | HashPtr hash = Hash::FromFileContent(absolutePath); |
| 99 | if (m_actionLog->KnownFileState(relativeFilePath.generic_string(), *hash)) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 100 | { |
| 101 | // the file state is known; i.e. the detected changed file is identical to |
| 102 | // the file state kept in FileState table |
| 103 | // it is the case that backend puts the file fetched from remote; |
| 104 | // we should not publish action for this. |
| 105 | } |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 106 | else |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 107 | { |
| 108 | uintmax_t fileSize = filesystem::file_size(absolutePath); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 109 | int seg_num; |
| 110 | tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName); |
| 111 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 112 | time_t wtime = filesystem::last_write_time(absolutePath); |
| 113 | filesystem::file_status stat = filesystem::status(absolutePath); |
| 114 | int mode = stat.permissions(); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 115 | |
| 116 | m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(), *hash, wtime, mode, seg_num); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 117 | // publish the file |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 118 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 119 | // notify SyncCore to propagate the change |
| 120 | m_core->localStateChanged(); |
| 121 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 122 | } |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 123 | else |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 124 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 125 | BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() )); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 126 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 130 | Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath) |
| 131 | { |
| 132 | m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath)); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath) |
| 137 | { |
| 138 | m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string()); |
| 139 | // notify SyncCore to propagate the change |
| 140 | m_core->localStateChanged(); |
| 141 | } |
| 142 | |
| 143 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 144 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 145 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 146 | |
| 147 | /** |
| 148 | * Callbacks: |
| 149 | * |
| 150 | * - from SyncLog: when state changes -> to fetch missing actions |
| 151 | * |
| 152 | * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action |
| 153 | * -> to add action to the action log |
| 154 | * |
| 155 | * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file |
| 156 | * |
| 157 | * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing? |
| 158 | * |
| 159 | * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb |
| 160 | * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise |
| 161 | */ |
| 162 | |
| 163 | void |
| 164 | Dispatcher::Did_SyncLog_StateChange (const SyncStateMsgPtr &stateMsg) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 165 | { |
| 166 | int size = stateMsg->state_size(); |
| 167 | int index = 0; |
| 168 | // iterate and fetch the actions |
| 169 | while (index < size) |
| 170 | { |
| 171 | SyncState state = stateMsg->state(index); |
| 172 | if (state.has_old_seq() && state.has_seq()) |
| 173 | { |
| 174 | uint64_t oldSeq = state.old_seq(); |
| 175 | uint64_t newSeq = state.seq(); |
| 176 | Name userName = state.name(); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 177 | |
| 178 | // fetch actions with oldSeq + 1 to newSeq (inclusive) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 179 | Name actionNameBase = Name(userName)("action")(m_sharedFolder); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 180 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 181 | m_actionFetcher->Enqueue (userName, actionNameBase, |
| 182 | bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback (), |
| 183 | oldSeq + 1, newSeq, FetchManager::PRIORITY_HIGH); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 189 | Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionName, uint32_t seqno, Ccnx::PcoPtr actionPco) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 190 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 191 | /// @todo Errors and exception checking |
| 192 | _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionName: " << actionName << ", seqno: " << seqno); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 193 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 194 | ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco); |
| 195 | // 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] | 196 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 197 | if (action->action () == ActionItem::UPDATE) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 198 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 199 | Hash hash (action->file_hash ().c_str(), action->file_hash ().size ()); |
| 200 | |
| 201 | Name fileNameBase = Name (deviceName)("file")(hash.GetHash (), hash.GetHashBytes ()); |
| 202 | |
| 203 | if (m_objectDbMap.find (hash) == m_objectDbMap.end ()) |
| 204 | { |
| 205 | m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir, lexical_cast<string> (hash)); |
| 206 | } |
| 207 | |
| 208 | m_fileFetcher->Enqueue (deviceName, fileNameBase, |
| 209 | bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4), |
| 210 | bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2), |
| 211 | 0, action->seg_num (), FetchManager::PRIORITY_NORMAL); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 212 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 216 | Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 217 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 218 | m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename)); |
| 219 | } |
| 220 | |
| 221 | void |
| 222 | Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename) |
| 223 | { |
| 224 | _LOG_DEBUG ("Action to delete " << filename); |
| 225 | |
| 226 | filesystem::path absolutePath = m_rootDir / filename; |
| 227 | if (filesystem::exists(absolutePath)) |
| 228 | { |
| 229 | // need some protection from local detection of removal |
| 230 | remove (absolutePath); |
| 231 | } |
| 232 | // don't exist |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco) |
| 237 | { |
| 238 | m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentName, segment, fileSegmentPco)); |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco) |
| 243 | { |
| 244 | const Bytes &hashBytes = fileSegmentName.getCompFromBack (1); |
| 245 | Hash hash (head(hashBytes), hashBytes.size()); |
| 246 | |
| 247 | _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentName: " << fileSegmentName << ", segment: " << segment); |
| 248 | |
| 249 | map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash); |
| 250 | if (db != m_objectDbMap.end()) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 251 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 252 | db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 253 | } |
| 254 | else |
| 255 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 256 | _LOG_ERROR ("no db available for this content object: " << fileSegmentName << ", size: " << fileSegmentPco->buf ().size()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | void |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 261 | Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 262 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 263 | m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName)); |
| 264 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 265 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 266 | void |
| 267 | Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName) |
| 268 | { |
| 269 | _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName); |
| 270 | // int size = fileNamePrefix.size(); |
| 271 | // Bytes hashBytes = fileNamePrefix.getComp(size - 1); |
| 272 | // Hash hash(head(hashBytes), hashBytes.size()); |
| 273 | // ostringstream oss; |
| 274 | // oss << hash; |
| 275 | // string hashString = oss.str(); |
| 276 | |
| 277 | // if (m_objectDbMap.find(hashString) != m_objectDbMap.end()) |
| 278 | // { |
| 279 | // // remove the db handle |
| 280 | // m_objectDbMap.erase(hashString); |
| 281 | // } |
| 282 | // else |
| 283 | // { |
| 284 | // cout << "no db available for this file: " << fileNamePrefix << endl; |
| 285 | // } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 286 | |
| 287 | // query the action table to get the path on local file system |
| 288 | // m_objectManager.objectsToLocalFile(deviceName, hash, relativeFilePath); |
| 289 | |
| 290 | } |