blob: d48bb43d93ab1dbfd1e8cdb9275d616aa2124ee9 [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);
42 m_actionLog = make_shared<ActionLog>(m_ccnx, path, m_syncLog, localUserName, sharedFolder);
43
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 Afanasyevcbda9922013-01-22 11:21:12 -0800105 if (m_actionLog->KnownFileState(relativeFilePath.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);
115 int seg_num = fileSize / MAX_FILE_SEGMENT_SIZE + ((fileSize % MAX_FILE_SEGMENT_SIZE == 0) ? 0 : 1);
116 time_t wtime = filesystem::last_write_time(absolutePath);
117 filesystem::file_status stat = filesystem::status(absolutePath);
118 int mode = stat.permissions();
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800119 m_actionLog->AddActionUpdate (relativeFilePath.string(), *hash, wtime, mode, seg_num);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800120 // publish the file
121 m_objectManager.localFileToObjects(relativeFilePath, m_localUserName);
122 // notify SyncCore to propagate the change
123 m_core->localStateChanged();
124 }
125 break;
126 }
127 else
128 {
129 BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
130 }
131 }
132 case DELETE:
133 {
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800134 m_actionLog->AddActionDelete (relativeFilePath.string());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800135 // notify SyncCore to propagate the change
136 m_core->localStateChanged();
137 break;
138 }
139 default:
140 break;
141 }
142}
143
144void
145Dispatcher::syncStateChanged(const SyncStateMsgPtr &stateMsg)
146{
147 int size = stateMsg->state_size();
148 int index = 0;
149 // iterate and fetch the actions
150 while (index < size)
151 {
152 SyncState state = stateMsg->state(index);
153 if (state.has_old_seq() && state.has_seq())
154 {
155 uint64_t oldSeq = state.old_seq();
156 uint64_t newSeq = state.seq();
157 Name userName = state.name();
158 Name locator = state.locator();
159
160 // fetch actions with oldSeq + 1 to newSeq (inclusive)
161 Name actionNameBase(userName);
162 actionNameBase.appendComp("action")
163 .appendComp(m_sharedFolder);
164
165 for (uint64_t seqNo = oldSeq + 1; seqNo <= newSeq; seqNo++)
166 {
167 Name actionName = actionNameBase;
168 actionName.appendComp(seqNo);
169
170 // TODO:
171 // use fetcher to fetch the name with callback "actionRecieved"
172 }
173 }
174 }
175}
176
177void
178Dispatcher::actionReceived(const ActionItemPtr &actionItem)
179{
180 switch (actionItem->action())
181 {
182 case ActionItem::UPDATE:
183 {
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800184 // @TODO
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800185 // need a function in ActionLog to apply received action, i.e. record remote action in ActionLog
186
187 string hashBytes = actionItem->file_hash();
188 Hash hash(hashBytes.c_str(), hashBytes.size());
189 ostringstream oss;
190 oss << hash;
191 string hashString = oss.str();
192 ObjectDbPtr db = make_shared<ObjectDb>(m_rootDir, hashString);
193 m_objectDbMap[hashString] = db;
194
195 // TODO:
196 // user fetcher to fetch the file with callback "fileSegmentReceived" for segment callback and "fileReady" for file ready callback
197 break;
198 }
199 case ActionItem::DELETE:
200 {
201 string filename = actionItem->filename();
202 // TODO:
Alexander Afanasyevcbda9922013-01-22 11:21:12 -0800203 // m_actionLog->AddRemoteActionDelete(filename);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800204 break;
205 }
206 default:
207 break;
208 }
209}
210
211void
212Dispatcher::fileSegmentReceived(const Ccnx::Name &name, const Ccnx::Bytes &content)
213{
214 int size = name.size();
215 uint64_t segment = name.getCompAsInt(size - 1);
216 Bytes hashBytes = name.getComp(size - 2);
217 Hash hash(head(hashBytes), hashBytes.size());
218 ostringstream oss;
219 oss << hash;
220 string hashString = oss.str();
221 if (m_objectDbMap.find(hashString) != m_objectDbMap.end())
222 {
223 ObjectDbPtr db = m_objectDbMap[hashString];
224 // get the device name
225 // Name deviceName = name.getPartialName();
226 // db->saveContenObject(deviceName, segment, content);
227 }
228 else
229 {
230 cout << "no db available for this content object: " << name << ", size: " << content.size() << endl;
231 }
232}
233
234void
235Dispatcher::fileReady(const Ccnx::Name &fileNamePrefix)
236{
237 int size = fileNamePrefix.size();
238 Bytes hashBytes = fileNamePrefix.getComp(size - 1);
239 Hash hash(head(hashBytes), hashBytes.size());
240 ostringstream oss;
241 oss << hash;
242 string hashString = oss.str();
243
244 if (m_objectDbMap.find(hashString) != m_objectDbMap.end())
245 {
246 // remove the db handle
247 m_objectDbMap.erase(hashString);
248 }
249 else
250 {
251 cout << "no db available for this file: " << fileNamePrefix << endl;
252 }
253
254 // query the action table to get the path on local file system
255 // m_objectManager.objectsToLocalFile(deviceName, hash, relativeFilePath);
256
257}