blob: ab2dc416340ed3dbcefd9953fca90924f8c54240 [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;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -080096 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080097 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080098 BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080099 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800100
101 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
102 if (currentFile &&
103 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ()) &&
104 last_write_time (absolutePath) == currentFile->mtime () &&
105 status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ()))
106 {
107 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
108 return;
109 }
110
111 int seg_num;
112 HashPtr hash;
113 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
114
115 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
116 *hash,
117 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
118
119 // notify SyncCore to propagate the change
120 m_core->localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800121}
122
123void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800124Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
125{
126 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
127}
128
129void
130Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
131{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800132 filesystem::path absolutePath = m_rootDir / relativeFilePath;
133 if (!filesystem::exists(absolutePath))
134 {
135 BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
136 }
137
138 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
139 if (!currentFile)
140 {
141 _LOG_ERROR ("File already deleted [" << relativeFilePath << "]");
142 return;
143 }
144
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800145 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
146 // notify SyncCore to propagate the change
147 m_core->localStateChanged();
148}
149
150/////////////////////////////////////////////////////////////////////////////////////////////////////
151/////////////////////////////////////////////////////////////////////////////////////////////////////
152/////////////////////////////////////////////////////////////////////////////////////////////////////
153
154/**
155 * Callbacks:
156 *
157 * - from SyncLog: when state changes -> to fetch missing actions
158 *
159 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
160 * -> to add action to the action log
161 *
162 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
163 *
164 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
165 *
166 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
167 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
168 */
169
170void
171Dispatcher::Did_SyncLog_StateChange (const SyncStateMsgPtr &stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800172{
173 int size = stateMsg->state_size();
174 int index = 0;
175 // iterate and fetch the actions
176 while (index < size)
177 {
178 SyncState state = stateMsg->state(index);
179 if (state.has_old_seq() && state.has_seq())
180 {
181 uint64_t oldSeq = state.old_seq();
182 uint64_t newSeq = state.seq();
183 Name userName = state.name();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800184
185 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800186 Name actionNameBase = Name(userName)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800187
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800188 m_actionFetcher->Enqueue (userName, actionNameBase,
189 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback (),
190 oldSeq + 1, newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800191 }
192 }
193}
194
195void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800196Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800197{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800198 /// @todo Errors and exception checking
199 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionName: " << actionName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800200
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800201 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
202 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800203
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800204 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800205 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800206 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
207
208 Name fileNameBase = Name (deviceName)("file")(hash.GetHash (), hash.GetHashBytes ());
209
210 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
211 {
212 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir, lexical_cast<string> (hash));
213 }
214
215 m_fileFetcher->Enqueue (deviceName, fileNameBase,
216 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
217 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
218 0, action->seg_num (), FetchManager::PRIORITY_NORMAL);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800219 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800220}
221
222void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800223Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800224{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800225 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
226}
227
228void
229Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
230{
231 _LOG_DEBUG ("Action to delete " << filename);
232
233 filesystem::path absolutePath = m_rootDir / filename;
234 if (filesystem::exists(absolutePath))
235 {
236 // need some protection from local detection of removal
237 remove (absolutePath);
238 }
239 // don't exist
240}
241
242void
243Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
244{
245 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentName, segment, fileSegmentPco));
246}
247
248void
249Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
250{
251 const Bytes &hashBytes = fileSegmentName.getCompFromBack (1);
252 Hash hash (head(hashBytes), hashBytes.size());
253
254 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentName: " << fileSegmentName << ", segment: " << segment);
255
256 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
257 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800258 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800259 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800260 }
261 else
262 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800263 _LOG_ERROR ("no db available for this content object: " << fileSegmentName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800264 }
265}
266
267void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800268Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800269{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800270 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
271}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800272
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800273void
274Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
275{
276 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800277
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800278 const Bytes &hashBytes = fileBaseName.getCompFromBack (1);
279 Hash hash (head (hashBytes), hashBytes.size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800280
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800281 FileItemsPtr filesToAssemble = m_actionLog->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800282
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800283 for (FileItems::iterator file = filesToAssemble->begin ();
284 file != filesToAssemble->end ();
285 file++)
286 {
287 boost::filesystem::path filePath = m_rootDir / file->filename ();
288 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
289
290 last_write_time (filePath, file->mtime ());
291 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
292 }
293
294 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
295 {
296 // remove the db handle
297 m_objectDbMap.erase (hash);
298 }
299 else
300 {
301 _LOG_ERROR ("no db available for this file: " << hash);
302 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800303}