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