blob: bdcc469824da8d8bca2ed793cdca6629af75c1e9 [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)
40{
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080041 m_syncLog = make_shared<SyncLog>(path, localUserName);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080042 m_actionLog = make_shared<ActionLog>(m_ccnx, path, m_syncLog, sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080043
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080044 Name syncPrefix(BROADCAST_DOMAIN + sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080045 m_core = new SyncCore (m_syncLog, localUserName, localPrefix, syncPrefix,
46 bind(&Dispatcher::syncStateChanged, this, _1), ccnx, scheduler);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080047}
48
49Dispatcher::~Dispatcher()
50{
51 if (m_core != NULL)
52 {
53 delete m_core;
54 m_core = NULL;
55 }
56}
57
58void
59Dispatcher::fileChangedCallback(const filesystem::path &relativeFilePath, ActionType type)
60{
61 Executor::Job job = bind(&Dispatcher::fileChanged, this, relativeFilePath, type);
62 m_executor.execute(job);
63}
64
65void
66Dispatcher::syncStateChangedCallback(const SyncStateMsgPtr &stateMsg)
67{
68 Executor::Job job = bind(&Dispatcher::syncStateChanged, this, stateMsg);
69 m_executor.execute(job);
70}
71
72void
73Dispatcher::actionReceivedCallback(const ActionItemPtr &actionItem)
74{
75 Executor::Job job = bind(&Dispatcher::actionReceived, this, actionItem);
76 m_executor.execute(job);
77}
78
79void
80Dispatcher::fileSegmentReceivedCallback(const Ccnx::Name &name, const Ccnx::Bytes &content)
81{
82 Executor::Job job = bind(&Dispatcher::fileSegmentReceived, this, name, content);
83 m_executor.execute(job);
84}
85
86void
87Dispatcher::fileReadyCallback(const Ccnx::Name &fileNamePrefix)
88{
89 Executor::Job job = bind(&Dispatcher::fileReady, this, fileNamePrefix);
90 m_executor.execute(job);
91}
92
93void
94Dispatcher::fileChanged(const filesystem::path &relativeFilePath, ActionType type)
95{
96
97 switch (type)
98 {
99 case UPDATE:
100 {
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800101 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800102 if (filesystem::exists(absolutePath))
103 {
104 HashPtr hash = Hash::FromFileContent(absolutePath);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800105 if (m_actionLog->KnownFileState(relativeFilePath.generic_string(), *hash))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800106 {
107 // the file state is known; i.e. the detected changed file is identical to
108 // the file state kept in FileState table
109 // it is the case that backend puts the file fetched from remote;
110 // we should not publish action for this.
111 }
112 else
113 {
114 uintmax_t fileSize = filesystem::file_size(absolutePath);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800115 int seg_num;
116 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
117
118 time_t wtime = filesystem::last_write_time (absolutePath);
119 filesystem::file_status stat = filesystem::status (absolutePath);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800120 int mode = stat.permissions();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800121
122 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(), *hash, wtime, mode, seg_num);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800123 // publish the file
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800124
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800125 // notify SyncCore to propagate the change
126 m_core->localStateChanged();
127 }
128 break;
129 }
130 else
131 {
132 BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
133 }
134 }
135 case DELETE:
136 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800137 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800138 // notify SyncCore to propagate the change
139 m_core->localStateChanged();
140 break;
141 }
142 default:
143 break;
144 }
145}
146
147void
148Dispatcher::syncStateChanged(const SyncStateMsgPtr &stateMsg)
149{
150 int size = stateMsg->state_size();
151 int index = 0;
152 // iterate and fetch the actions
153 while (index < size)
154 {
155 SyncState state = stateMsg->state(index);
156 if (state.has_old_seq() && state.has_seq())
157 {
158 uint64_t oldSeq = state.old_seq();
159 uint64_t newSeq = state.seq();
160 Name userName = state.name();
161 Name locator = state.locator();
162
163 // fetch actions with oldSeq + 1 to newSeq (inclusive)
164 Name actionNameBase(userName);
165 actionNameBase.appendComp("action")
166 .appendComp(m_sharedFolder);
167
168 for (uint64_t seqNo = oldSeq + 1; seqNo <= newSeq; seqNo++)
169 {
170 Name actionName = actionNameBase;
171 actionName.appendComp(seqNo);
172
173 // TODO:
174 // use fetcher to fetch the name with callback "actionRecieved"
175 }
176 }
177 }
178}
179
180void
181Dispatcher::actionReceived(const ActionItemPtr &actionItem)
182{
183 switch (actionItem->action())
184 {
185 case ActionItem::UPDATE:
186 {
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800187 // @TODO
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800188 // need a function in ActionLog to apply received action, i.e. record remote action in ActionLog
189
190 string hashBytes = actionItem->file_hash();
191 Hash hash(hashBytes.c_str(), hashBytes.size());
192 ostringstream oss;
193 oss << hash;
194 string hashString = oss.str();
195 ObjectDbPtr db = make_shared<ObjectDb>(m_rootDir, hashString);
196 m_objectDbMap[hashString] = db;
197
198 // TODO:
199 // user fetcher to fetch the file with callback "fileSegmentReceived" for segment callback and "fileReady" for file ready callback
200 break;
201 }
202 case ActionItem::DELETE:
203 {
204 string filename = actionItem->filename();
205 // TODO:
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800206 // m_actionLog->AddRemoteActionDelete(filename);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800207 break;
208 }
209 default:
210 break;
211 }
212}
213
214void
215Dispatcher::fileSegmentReceived(const Ccnx::Name &name, const Ccnx::Bytes &content)
216{
217 int size = name.size();
218 uint64_t segment = name.getCompAsInt(size - 1);
219 Bytes hashBytes = name.getComp(size - 2);
220 Hash hash(head(hashBytes), hashBytes.size());
221 ostringstream oss;
222 oss << hash;
223 string hashString = oss.str();
224 if (m_objectDbMap.find(hashString) != m_objectDbMap.end())
225 {
226 ObjectDbPtr db = m_objectDbMap[hashString];
227 // get the device name
228 // Name deviceName = name.getPartialName();
229 // db->saveContenObject(deviceName, segment, content);
230 }
231 else
232 {
233 cout << "no db available for this content object: " << name << ", size: " << content.size() << endl;
234 }
235}
236
237void
238Dispatcher::fileReady(const Ccnx::Name &fileNamePrefix)
239{
240 int size = fileNamePrefix.size();
241 Bytes hashBytes = fileNamePrefix.getComp(size - 1);
242 Hash hash(head(hashBytes), hashBytes.size());
243 ostringstream oss;
244 oss << hash;
245 string hashString = oss.str();
246
247 if (m_objectDbMap.find(hashString) != m_objectDbMap.end())
248 {
249 // remove the db handle
250 m_objectDbMap.erase(hashString);
251 }
252 else
253 {
254 cout << "no db available for this file: " << fileNamePrefix << endl;
255 }
256
257 // query the action table to get the path on local file system
258 // m_objectManager.objectsToLocalFile(deviceName, hash, relativeFilePath);
259
260}