blob: d9bc37a07e3aa5521dd03f371f3f22cf9450652e [file] [log] [blame]
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -08001/* -*- 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 Afanasyevf9978f82013-01-23 16:30:31 -080023#include "logging.h"
24
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080025#include <boost/make_shared.hpp>
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080026#include <boost/lexical_cast.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080027
28using namespace Ccnx;
29using namespace std;
30using namespace boost;
31
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080032INIT_LOGGER ("Dispatcher");
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080033
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080034static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare";
35
36Dispatcher::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 Zhuc3fd51e2013-01-22 10:45:54 -080040 : 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 Zhu1dcbbab2013-01-22 16:03:20 -080047 , m_server(NULL)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080048{
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080049 m_syncLog = make_shared<SyncLog>(path, localUserName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080050 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 Afanasyev1b15cc62013-01-22 17:00:40 -080054 Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080055
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080056 m_server = new ContentServer(m_ccnx, m_actionLog, rootDir);
57 m_server->registerPrefix(localPrefix);
58 m_server->registerPrefix(syncPrefix);
59
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080060 m_core = new SyncCore (m_syncLog, localUserName, localPrefix, syncPrefix,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080061 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx, scheduler);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080062
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080063 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 Zhuc3fd51e2013-01-22 10:45:54 -080065}
66
67Dispatcher::~Dispatcher()
68{
69 if (m_core != NULL)
70 {
71 delete m_core;
72 m_core = NULL;
73 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080074
75 if (m_server != NULL)
76 {
77 delete m_server;
78 m_server = NULL;
79 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080080}
81
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080082/////////////////////////////////////////////////////////////////////////////////////////////////////
83/////////////////////////////////////////////////////////////////////////////////////////////////////
84/////////////////////////////////////////////////////////////////////////////////////////////////////
85
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080086void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080087Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080088{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080089 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080090}
91
92void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080093Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080094{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080095 filesystem::path absolutePath = m_rootDir / relativeFilePath;
96 if (filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080097 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080098 HashPtr hash = Hash::FromFileContent(absolutePath);
99 if (m_actionLog->KnownFileState(relativeFilePath.generic_string(), *hash))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800100 {
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 Afanasyevf9978f82013-01-23 16:30:31 -0800106 else
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800107 {
108 uintmax_t fileSize = filesystem::file_size(absolutePath);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800109 int seg_num;
110 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
111
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800112 time_t wtime = filesystem::last_write_time(absolutePath);
113 filesystem::file_status stat = filesystem::status(absolutePath);
114 int mode = stat.permissions();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800115
116 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(), *hash, wtime, mode, seg_num);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800117 // publish the file
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800118
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800119 // notify SyncCore to propagate the change
120 m_core->localStateChanged();
121 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800122 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800123 else
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800124 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800125 BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800126 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800127}
128
129void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800130Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
131{
132 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
133}
134
135void
136Dispatcher::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
163void
164Dispatcher::Did_SyncLog_StateChange (const SyncStateMsgPtr &stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800165{
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 Zhuc3fd51e2013-01-22 10:45:54 -0800177
178 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800179 Name actionNameBase = Name(userName)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800180
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800181 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 Zhuc3fd51e2013-01-22 10:45:54 -0800184 }
185 }
186}
187
188void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800189Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800190{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800191 /// @todo Errors and exception checking
192 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionName: " << actionName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800193
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800194 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
195 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800196
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800197 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800198 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800199 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 Zhuc3fd51e2013-01-22 10:45:54 -0800212 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800213}
214
215void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800216Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800217{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800218 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
219}
220
221void
222Dispatcher::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
235void
236Dispatcher::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
241void
242Dispatcher::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 Zhuc3fd51e2013-01-22 10:45:54 -0800251 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800252 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800253 }
254 else
255 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800256 _LOG_ERROR ("no db available for this content object: " << fileSegmentName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800257 }
258}
259
260void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800261Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800262{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800263 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
264}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800265
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800266void
267Dispatcher::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 Zhuc3fd51e2013-01-22 10:45:54 -0800286
287 // query the action table to get the path on local file system
288 // m_objectManager.objectsToLocalFile(deviceName, hash, relativeFilePath);
289
290}