blob: babae92e0deaee0125ce53e6e9350fd7c9c4b0a6 [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"
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080024#include "ccnx-discovery.h"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080025
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080026#include <boost/make_shared.hpp>
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080027#include <boost/lexical_cast.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080028
29using namespace Ccnx;
30using namespace std;
31using namespace boost;
32
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080033INIT_LOGGER ("Dispatcher");
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080034
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080035static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare";
36
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080037Dispatcher::Dispatcher(const std::string &localUserName
38 , const std::string &sharedFolder
39 , const filesystem::path &rootDir
40 , Ccnx::CcnxWrapperPtr ccnx
41 , int poolSize)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080042 : m_ccnx(ccnx)
43 , m_core(NULL)
44 , m_rootDir(rootDir)
45 , m_executor(poolSize)
46 , m_objectManager(ccnx, rootDir)
47 , m_localUserName(localUserName)
48 , m_sharedFolder(sharedFolder)
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080049 , m_server(NULL)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080050{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080051 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
52 m_actionLog = make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080053 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
54 ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback
55 bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
Alexander Afanasyev1b15cc62013-01-22 17:00:40 -080056 Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080057
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080058 m_server = new ContentServer(m_ccnx, m_actionLog, rootDir);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080059 m_server->registerPrefix(Name ("/"));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080060 m_server->registerPrefix(syncPrefix);
61
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080062 m_core = new SyncCore (m_syncLog, localUserName, Name ("/"), syncPrefix,
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080063 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080064
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080065 m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3);
66 m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080067
68 Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), "dispatcher"));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080069}
70
71Dispatcher::~Dispatcher()
72{
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080073 Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), "dispatcher"));
74
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080075 if (m_core != NULL)
76 {
77 delete m_core;
78 m_core = NULL;
79 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080080
81 if (m_server != NULL)
82 {
83 delete m_server;
84 m_server = NULL;
85 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080086}
87
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080088void
89Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &prefix)
90{
91 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
92 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << prefix);
93
94 m_server->deregisterPrefix(prefix);
95 m_syncLog->UpdateLocalLocator (prefix);
96 m_server->deregisterPrefix(oldLocalPrefix);
97}
98
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080099/////////////////////////////////////////////////////////////////////////////////////////////////////
100/////////////////////////////////////////////////////////////////////////////////////////////////////
101/////////////////////////////////////////////////////////////////////////////////////////////////////
102
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800103void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800104Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800105{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800106 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800107}
108
109void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800110Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800111{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800112 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800113 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800114 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800115 BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800116 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800117
118 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
119 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800120 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
121 // The following two are commented out to prevent front end from reporting intermediate files
122 // should enable it if there is other way to prevent this
123 // && last_write_time (absolutePath) == currentFile->mtime ()
124 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
125 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800126 {
127 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
128 return;
129 }
130
131 int seg_num;
132 HashPtr hash;
133 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
134
135 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
136 *hash,
137 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
138
139 // notify SyncCore to propagate the change
140 m_core->localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800141}
142
143void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800144Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
145{
146 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
147}
148
149void
150Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
151{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800152 filesystem::path absolutePath = m_rootDir / relativeFilePath;
153 if (!filesystem::exists(absolutePath))
154 {
155 BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
156 }
157
158 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
159 if (!currentFile)
160 {
161 _LOG_ERROR ("File already deleted [" << relativeFilePath << "]");
162 return;
163 }
164
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800165 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
166 // notify SyncCore to propagate the change
167 m_core->localStateChanged();
168}
169
170/////////////////////////////////////////////////////////////////////////////////////////////////////
171/////////////////////////////////////////////////////////////////////////////////////////////////////
172/////////////////////////////////////////////////////////////////////////////////////////////////////
173
174/**
175 * Callbacks:
176 *
177 * - from SyncLog: when state changes -> to fetch missing actions
178 *
179 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
180 * -> to add action to the action log
181 *
182 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
183 *
184 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
185 *
186 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
187 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
188 */
189
190void
191Dispatcher::Did_SyncLog_StateChange (const SyncStateMsgPtr &stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800192{
193 int size = stateMsg->state_size();
194 int index = 0;
195 // iterate and fetch the actions
196 while (index < size)
197 {
198 SyncState state = stateMsg->state(index);
199 if (state.has_old_seq() && state.has_seq())
200 {
201 uint64_t oldSeq = state.old_seq();
202 uint64_t newSeq = state.seq();
203 Name userName = state.name();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800204
205 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800206 Name actionNameBase = Name(userName)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800207
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800208 m_actionFetcher->Enqueue (userName, actionNameBase,
209 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback (),
210 oldSeq + 1, newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800211 }
212 }
213}
214
215void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800216Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800217{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800218 /// @todo Errors and exception checking
219 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionName: " << actionName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800220
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800221 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
222 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800223
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800224 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800225 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800226 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
227
228 Name fileNameBase = Name (deviceName)("file")(hash.GetHash (), hash.GetHashBytes ());
229
230 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
231 {
232 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir, lexical_cast<string> (hash));
233 }
234
235 m_fileFetcher->Enqueue (deviceName, fileNameBase,
236 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
237 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800238 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800239 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800240}
241
242void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800243Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800244{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800245 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
246}
247
248void
249Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
250{
251 _LOG_DEBUG ("Action to delete " << filename);
252
253 filesystem::path absolutePath = m_rootDir / filename;
254 if (filesystem::exists(absolutePath))
255 {
256 // need some protection from local detection of removal
257 remove (absolutePath);
258 }
259 // don't exist
260}
261
262void
263Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
264{
265 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentName, segment, fileSegmentPco));
266}
267
268void
269Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
270{
271 const Bytes &hashBytes = fileSegmentName.getCompFromBack (1);
272 Hash hash (head(hashBytes), hashBytes.size());
273
274 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentName: " << fileSegmentName << ", segment: " << segment);
275
276 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
277 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800278 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800279 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800280 }
281 else
282 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800283 _LOG_ERROR ("no db available for this content object: " << fileSegmentName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800284 }
285}
286
287void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800288Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800289{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800290 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
291}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800292
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800293void
294Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
295{
296 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800297
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800298 const Bytes &hashBytes = fileBaseName.getCompFromBack (1);
299 Hash hash (head (hashBytes), hashBytes.size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800300
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800301 FileItemsPtr filesToAssemble = m_actionLog->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800302
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800303 for (FileItems::iterator file = filesToAssemble->begin ();
304 file != filesToAssemble->end ();
305 file++)
306 {
307 boost::filesystem::path filePath = m_rootDir / file->filename ();
308 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
309
310 last_write_time (filePath, file->mtime ());
311 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
312 }
313
314 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
315 {
316 // remove the db handle
317 m_objectDbMap.erase (hash);
318 }
319 else
320 {
321 _LOG_ERROR ("no db available for this file: " << hash);
322 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800323}