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