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" |
| 23 | #include <boost/make_shared.hpp> |
| 24 | |
| 25 | using namespace Ccnx; |
| 26 | using namespace std; |
| 27 | using namespace boost; |
| 28 | |
| 29 | static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare"; |
| 30 | static const int MAX_FILE_SEGMENT_SIZE = 1024; |
| 31 | |
| 32 | Dispatcher::Dispatcher(const filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix, const std::string &sharedFolder, const filesystem::path &rootDir, const Ccnx::CcnxWrapperPtr &ccnx, const SchedulerPtr &scheduler, int poolSize) |
| 33 | : m_ccnx(ccnx) |
| 34 | , m_core(NULL) |
| 35 | , m_rootDir(rootDir) |
| 36 | , m_executor(poolSize) |
| 37 | , m_objectManager(ccnx, rootDir) |
| 38 | , m_localUserName(localUserName) |
| 39 | , m_sharedFolder(sharedFolder) |
| 40 | { |
| 41 | m_log = make_shared<ActionLog>(m_ccnx, path, localUserName, sharedFolder); |
| 42 | Name syncPrefix(BROADCAST_DOMAIN + sharedFolder); |
| 43 | m_core = new SyncCore(m_log, localUserName, localPrefix, syncPrefix, bind(&Dispatcher::syncStateChanged, this, _1), ccnx, scheduler); |
| 44 | } |
| 45 | |
| 46 | Dispatcher::~Dispatcher() |
| 47 | { |
| 48 | if (m_core != NULL) |
| 49 | { |
| 50 | delete m_core; |
| 51 | m_core = NULL; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | Dispatcher::fileChangedCallback(const filesystem::path &relativeFilePath, ActionType type) |
| 57 | { |
| 58 | Executor::Job job = bind(&Dispatcher::fileChanged, this, relativeFilePath, type); |
| 59 | m_executor.execute(job); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | Dispatcher::syncStateChangedCallback(const SyncStateMsgPtr &stateMsg) |
| 64 | { |
| 65 | Executor::Job job = bind(&Dispatcher::syncStateChanged, this, stateMsg); |
| 66 | m_executor.execute(job); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | Dispatcher::actionReceivedCallback(const ActionItemPtr &actionItem) |
| 71 | { |
| 72 | Executor::Job job = bind(&Dispatcher::actionReceived, this, actionItem); |
| 73 | m_executor.execute(job); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | Dispatcher::fileSegmentReceivedCallback(const Ccnx::Name &name, const Ccnx::Bytes &content) |
| 78 | { |
| 79 | Executor::Job job = bind(&Dispatcher::fileSegmentReceived, this, name, content); |
| 80 | m_executor.execute(job); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | Dispatcher::fileReadyCallback(const Ccnx::Name &fileNamePrefix) |
| 85 | { |
| 86 | Executor::Job job = bind(&Dispatcher::fileReady, this, fileNamePrefix); |
| 87 | m_executor.execute(job); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | Dispatcher::fileChanged(const filesystem::path &relativeFilePath, ActionType type) |
| 92 | { |
| 93 | |
| 94 | switch (type) |
| 95 | { |
| 96 | case UPDATE: |
| 97 | { |
| 98 | filesystem::path absolutePath = m_rootDir + relativeFilePath; |
| 99 | if (filesystem::exists(absolutePath)) |
| 100 | { |
| 101 | HashPtr hash = Hash::FromFileContent(absolutePath); |
| 102 | if (m_log->KnownFileState(relativeFilePath.string(), *hash)) |
| 103 | { |
| 104 | // the file state is known; i.e. the detected changed file is identical to |
| 105 | // the file state kept in FileState table |
| 106 | // it is the case that backend puts the file fetched from remote; |
| 107 | // we should not publish action for this. |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | uintmax_t fileSize = filesystem::file_size(absolutePath); |
| 112 | int seg_num = fileSize / MAX_FILE_SEGMENT_SIZE + ((fileSize % MAX_FILE_SEGMENT_SIZE == 0) ? 0 : 1); |
| 113 | time_t wtime = filesystem::last_write_time(absolutePath); |
| 114 | filesystem::file_status stat = filesystem::status(absolutePath); |
| 115 | int mode = stat.permissions(); |
| 116 | m_log->AddActionUpdate (relativeFilePath.string(), *hash, wtime, mode, seg_num); |
| 117 | // publish the file |
| 118 | m_objectManager.localFileToObjects(relativeFilePath, m_localUserName); |
| 119 | // notify SyncCore to propagate the change |
| 120 | m_core->localStateChanged(); |
| 121 | } |
| 122 | break; |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() )); |
| 127 | } |
| 128 | } |
| 129 | case DELETE: |
| 130 | { |
| 131 | m_log->AddActionDelete (relativeFilePath.string()); |
| 132 | // notify SyncCore to propagate the change |
| 133 | m_core->localStateChanged(); |
| 134 | break; |
| 135 | } |
| 136 | default: |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | Dispatcher::syncStateChanged(const SyncStateMsgPtr &stateMsg) |
| 143 | { |
| 144 | int size = stateMsg->state_size(); |
| 145 | int index = 0; |
| 146 | // iterate and fetch the actions |
| 147 | while (index < size) |
| 148 | { |
| 149 | SyncState state = stateMsg->state(index); |
| 150 | if (state.has_old_seq() && state.has_seq()) |
| 151 | { |
| 152 | uint64_t oldSeq = state.old_seq(); |
| 153 | uint64_t newSeq = state.seq(); |
| 154 | Name userName = state.name(); |
| 155 | Name locator = state.locator(); |
| 156 | |
| 157 | // fetch actions with oldSeq + 1 to newSeq (inclusive) |
| 158 | Name actionNameBase(userName); |
| 159 | actionNameBase.appendComp("action") |
| 160 | .appendComp(m_sharedFolder); |
| 161 | |
| 162 | for (uint64_t seqNo = oldSeq + 1; seqNo <= newSeq; seqNo++) |
| 163 | { |
| 164 | Name actionName = actionNameBase; |
| 165 | actionName.appendComp(seqNo); |
| 166 | |
| 167 | // TODO: |
| 168 | // use fetcher to fetch the name with callback "actionRecieved" |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | Dispatcher::actionReceived(const ActionItemPtr &actionItem) |
| 176 | { |
| 177 | switch (actionItem->action()) |
| 178 | { |
| 179 | case ActionItem::UPDATE: |
| 180 | { |
| 181 | // TODO: |
| 182 | // need a function in ActionLog to apply received action, i.e. record remote action in ActionLog |
| 183 | |
| 184 | string hashBytes = actionItem->file_hash(); |
| 185 | Hash hash(hashBytes.c_str(), hashBytes.size()); |
| 186 | ostringstream oss; |
| 187 | oss << hash; |
| 188 | string hashString = oss.str(); |
| 189 | ObjectDbPtr db = make_shared<ObjectDb>(m_rootDir, hashString); |
| 190 | m_objectDbMap[hashString] = db; |
| 191 | |
| 192 | // TODO: |
| 193 | // user fetcher to fetch the file with callback "fileSegmentReceived" for segment callback and "fileReady" for file ready callback |
| 194 | break; |
| 195 | } |
| 196 | case ActionItem::DELETE: |
| 197 | { |
| 198 | string filename = actionItem->filename(); |
| 199 | // TODO: |
| 200 | // m_log->AddRemoteActionDelete(filename); |
| 201 | break; |
| 202 | } |
| 203 | default: |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | Dispatcher::fileSegmentReceived(const Ccnx::Name &name, const Ccnx::Bytes &content) |
| 210 | { |
| 211 | int size = name.size(); |
| 212 | uint64_t segment = name.getCompAsInt(size - 1); |
| 213 | Bytes hashBytes = name.getComp(size - 2); |
| 214 | Hash hash(head(hashBytes), hashBytes.size()); |
| 215 | ostringstream oss; |
| 216 | oss << hash; |
| 217 | string hashString = oss.str(); |
| 218 | if (m_objectDbMap.find(hashString) != m_objectDbMap.end()) |
| 219 | { |
| 220 | ObjectDbPtr db = m_objectDbMap[hashString]; |
| 221 | // get the device name |
| 222 | // Name deviceName = name.getPartialName(); |
| 223 | // db->saveContenObject(deviceName, segment, content); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | cout << "no db available for this content object: " << name << ", size: " << content.size() << endl; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | Dispatcher::fileReady(const Ccnx::Name &fileNamePrefix) |
| 233 | { |
| 234 | int size = fileNamePrefix.size(); |
| 235 | Bytes hashBytes = fileNamePrefix.getComp(size - 1); |
| 236 | Hash hash(head(hashBytes), hashBytes.size()); |
| 237 | ostringstream oss; |
| 238 | oss << hash; |
| 239 | string hashString = oss.str(); |
| 240 | |
| 241 | if (m_objectDbMap.find(hashString) != m_objectDbMap.end()) |
| 242 | { |
| 243 | // remove the db handle |
| 244 | m_objectDbMap.erase(hashString); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | cout << "no db available for this file: " << fileNamePrefix << endl; |
| 249 | } |
| 250 | |
| 251 | // query the action table to get the path on local file system |
| 252 | // m_objectManager.objectsToLocalFile(deviceName, hash, relativeFilePath); |
| 253 | |
| 254 | } |