blob: ad88aed453117080c5487f97b49e08016be400df [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
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080041 , int poolSize
42 , bool enablePrefixDiscovery
43 )
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080044 : m_ccnx(ccnx)
45 , m_core(NULL)
46 , m_rootDir(rootDir)
47 , m_executor(poolSize)
48 , m_objectManager(ccnx, rootDir)
49 , m_localUserName(localUserName)
50 , m_sharedFolder(sharedFolder)
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080051 , m_server(NULL)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080052 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080053{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080054 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
55 m_actionLog = make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080056 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
57 ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback
58 bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
Alexander Afanasyev1b15cc62013-01-22 17:00:40 -080059 Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080060
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080061 m_server = new ContentServer(m_ccnx, m_actionLog, rootDir);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080062 m_server->registerPrefix(Name ("/"));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080063 m_server->registerPrefix(syncPrefix);
64
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080065 m_core = new SyncCore (m_syncLog, localUserName, Name ("/"), syncPrefix,
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080066 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080067
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080068 m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3);
69 m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080070
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080071 if (m_enablePrefixDiscovery)
72 {
73 Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), "dispatcher"));
74 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080075
76 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080077}
78
79Dispatcher::~Dispatcher()
80{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080081 // _LOG_DEBUG ("Enter destructor of dispatcher");
82 m_executor.shutdown ();
83
84 // _LOG_DEBUG (">>");
85
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080086 if (m_enablePrefixDiscovery)
87 {
88 Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), "dispatcher"));
89 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080090
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080091 if (m_core != NULL)
92 {
93 delete m_core;
94 m_core = NULL;
95 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080096
97 if (m_server != NULL)
98 {
99 delete m_server;
100 m_server = NULL;
101 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800102}
103
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800104void
105Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &prefix)
106{
107 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
108 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << prefix);
109
110 m_server->deregisterPrefix(prefix);
111 m_syncLog->UpdateLocalLocator (prefix);
112 m_server->deregisterPrefix(oldLocalPrefix);
113}
114
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800115/////////////////////////////////////////////////////////////////////////////////////////////////////
116/////////////////////////////////////////////////////////////////////////////////////////////////////
117/////////////////////////////////////////////////////////////////////////////////////////////////////
118
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800119void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800120Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800121{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800122 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800123}
124
125void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800126Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800127{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800128 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800129 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800130 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800131 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800132 BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800133 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800134
135 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
136 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800137 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
138 // The following two are commented out to prevent front end from reporting intermediate files
139 // should enable it if there is other way to prevent this
140 // && last_write_time (absolutePath) == currentFile->mtime ()
141 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
142 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800143 {
144 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
145 return;
146 }
147
148 int seg_num;
149 HashPtr hash;
150 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
151
152 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
153 *hash,
154 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
155
156 // notify SyncCore to propagate the change
157 m_core->localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800158}
159
160void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800161Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
162{
163 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
164}
165
166void
167Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
168{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800169 filesystem::path absolutePath = m_rootDir / relativeFilePath;
170 if (!filesystem::exists(absolutePath))
171 {
172 BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
173 }
174
175 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
176 if (!currentFile)
177 {
178 _LOG_ERROR ("File already deleted [" << relativeFilePath << "]");
179 return;
180 }
181
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800182 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
183 // notify SyncCore to propagate the change
184 m_core->localStateChanged();
185}
186
187/////////////////////////////////////////////////////////////////////////////////////////////////////
188/////////////////////////////////////////////////////////////////////////////////////////////////////
189/////////////////////////////////////////////////////////////////////////////////////////////////////
190
191/**
192 * Callbacks:
193 *
194 * - from SyncLog: when state changes -> to fetch missing actions
195 *
196 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
197 * -> to add action to the action log
198 *
199 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
200 *
201 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
202 *
203 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
204 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
205 */
206
207void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800208Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
209{
210 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
211}
212
213void
214Dispatcher::Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800215{
216 int size = stateMsg->state_size();
217 int index = 0;
218 // iterate and fetch the actions
219 while (index < size)
220 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800221 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800222 if (state.has_old_seq() && state.has_seq())
223 {
224 uint64_t oldSeq = state.old_seq();
225 uint64_t newSeq = state.seq();
226 Name userName = state.name();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800227
228 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800229 Name actionNameBase = Name(userName)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800230
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800231 m_actionFetcher->Enqueue (userName, actionNameBase,
232 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback (),
233 oldSeq + 1, newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800234 }
235 }
236}
237
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800238
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800239void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800240Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800241{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800242 /// @todo Errors and exception checking
243 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionName: " << actionName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800244
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800245 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
246 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800247
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800248 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800249 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800250 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
251
252 Name fileNameBase = Name (deviceName)("file")(hash.GetHash (), hash.GetHashBytes ());
253
254 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
255 {
256 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir, lexical_cast<string> (hash));
257 }
258
259 m_fileFetcher->Enqueue (deviceName, fileNameBase,
260 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
261 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800262 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800263 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800264}
265
266void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800267Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800268{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800269 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
270}
271
272void
273Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
274{
275 _LOG_DEBUG ("Action to delete " << filename);
276
277 filesystem::path absolutePath = m_rootDir / filename;
278 if (filesystem::exists(absolutePath))
279 {
280 // need some protection from local detection of removal
281 remove (absolutePath);
282 }
283 // don't exist
284}
285
286void
287Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
288{
289 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentName, segment, fileSegmentPco));
290}
291
292void
293Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
294{
295 const Bytes &hashBytes = fileSegmentName.getCompFromBack (1);
296 Hash hash (head(hashBytes), hashBytes.size());
297
298 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentName: " << fileSegmentName << ", segment: " << segment);
299
300 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
301 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800302 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800303 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800304 }
305 else
306 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800307 _LOG_ERROR ("no db available for this content object: " << fileSegmentName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800308 }
309}
310
311void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800312Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800313{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800314 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
315}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800316
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800317void
318Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
319{
320 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800321
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800322 const Bytes &hashBytes = fileBaseName.getCompFromBack (1);
323 Hash hash (head (hashBytes), hashBytes.size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800324
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800325 FileItemsPtr filesToAssemble = m_actionLog->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800326
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800327 for (FileItems::iterator file = filesToAssemble->begin ();
328 file != filesToAssemble->end ();
329 file++)
330 {
331 boost::filesystem::path filePath = m_rootDir / file->filename ();
332 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
333
334 last_write_time (filePath, file->mtime ());
335 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
336 }
337
338 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
339 {
340 // remove the db handle
341 m_objectDbMap.erase (hash);
342 }
343 else
344 {
345 _LOG_ERROR ("no db available for this file: " << hash);
346 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800347}