blob: c264edf3b7c15b9f757cc8c2c5611d3f4dc641c0 [file] [log] [blame]
Alexander Afanasyev026eaf32013-02-23 16:37:14 -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 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
20 */
21
22#include "state-server.h"
23#include "logging.h"
24#include <boost/make_shared.hpp>
25#include <utility>
26#include "task.h"
27#include "periodic-task.h"
28#include "simple-interval-generator.h"
29#include <boost/lexical_cast.hpp>
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080030#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080031
32INIT_LOGGER ("StateServer");
33
34using namespace Ccnx;
35using namespace std;
36using namespace boost;
37
38StateServer::StateServer(CcnxWrapperPtr ccnx, ActionLogPtr actionLog,
39 const boost::filesystem::path &rootDir,
40 const Ccnx::Name &userName, const std::string &sharedFolderName,
41 const std::string &appName,
42 ObjectManager &objectManager,
43 int freshness/* = -1*/)
44 : m_ccnx(ccnx)
45 , m_actionLog(actionLog)
46 , m_objectManager (objectManager)
47 , m_rootDir(rootDir)
48 , m_freshness(freshness)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080049 , m_executor (1)
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080050 , m_userName (userName)
51 , m_sharedFolderName (sharedFolderName)
52 , m_appName (appName)
53{
54 // may be later /localhost should be replaced with /%C1.M.S.localhost
55
56 // <PREFIX_INFO> = /localhost/<user's-device-name>/"chronoshare"/"info"
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080057 m_PREFIX_INFO = Name ("/localhost")(m_userName)("chronoshare")(m_sharedFolderName)("info");
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080058
59 // <PREFIX_CMD> = /localhost/<user's-device-name>/"chronoshare"/"cmd"
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080060 m_PREFIX_CMD = Name ("/localhost")(m_userName)("chronoshare")(m_sharedFolderName)("cmd");
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080061
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080062 m_executor.start ();
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080063
64 registerPrefixes ();
65}
66
67StateServer::~StateServer()
68{
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080069 m_executor.shutdown ();
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080070
71 deregisterPrefixes ();
72}
73
74void
75StateServer::registerPrefixes ()
76{
77 // currently supporting limited number of command.
78 // will be extended to support all planned commands later
79
80 // <PREFIX_INFO>/"actions"/"all"/<nonce>/<segment> get list of all actions
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080081 m_ccnx->setInterestFilter (Name (m_PREFIX_INFO)("actions")("folder"), bind(&StateServer::info_actions_folder, this, _1));
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080082
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080083 // <PREFIX_INFO>/"filestate"/"all"/<nonce>/<segment>
84 m_ccnx->setInterestFilter (Name (m_PREFIX_INFO)("filestate")("folder"), bind(&StateServer::info_filestate_folder, this, _1));
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080085
86 // <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash>
87 m_ccnx->setInterestFilter (Name (m_PREFIX_CMD)("restore")("file"), bind(&StateServer::cmd_restore_file, this, _1));
88}
89
90void
91StateServer::deregisterPrefixes ()
92{
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080093 m_ccnx->clearInterestFilter (Name (m_PREFIX_INFO)("actions")("folder"));
94 m_ccnx->clearInterestFilter (Name (m_PREFIX_INFO)("filestate")("folder"));
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080095 m_ccnx->clearInterestFilter (Name (m_PREFIX_CMD) ("restore")("file"));
96}
97
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080098// void
99// StateServer::info_actions_all (const Name &interest)
100// {
101// _LOG_DEBUG (">> info_actions_all: " << interest);
102// m_executor.execute (bind (&StateServer::info_actions_all_Execute, this, interest));
103// }
104
105// void
106// StateServer::info_actions_all_Execute (const Name &interest)
107// {
108// // <PREFIX_INFO>/"actions"/"all"/<nonce>/<offset> get list of all actions
109
110// try
111// {
112// int offset = interest.getCompFromBackAsInt (0);
113
114// // LookupActionsInFolderRecursively
115// /// @todo !!! add security checking
116// m_ccnx->publishData (interest, "FAIL: Not implemented", 1);
117// }
118// catch (Ccnx::NameException &ne)
119// {
120// // ignore any unexpected interests and errors
121// _LOG_ERROR (*boost::get_error_info<Ccnx::error_info_str>(ne));
122// }
123// }
124
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800125void
126StateServer::formatActionJson (json_spirit::Array &actions,
127 const Ccnx::Name &name, sqlite3_int64 seq_no, const ActionItem &action)
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800128{
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800129/*
130 * {
131 * "id": {
132 * "userName": "<NDN-NAME-OF-THE-USER>",
133 * "seqNo": "<SEQ_NO_OF_THE_ACTION>"
134 * },
135 * "timestamp": "<ACTION-TIMESTAMP>",
136 * "filename": "<FILENAME>",
137 *
138 * "action": "UPDATE | DELETE",
139 *
140 * // only if update
141 * "update": {
142 * "hash": "<FILE-HASH>",
143 * "timestamp": "<FILE-TIMESTAMP>",
144 * "chmod": "<FILE-MODE>",
145 * "segNum": "<NUMBER-OF-SEGMENTS (~file size)>"
146 * },
147 *
148 * // if parent_device_name is set
149 * "parentId": {
150 * "userName": "<NDN-NAME-OF-THE-USER>",
151 * "seqNo": "<SEQ_NO_OF_THE_ACTION>"
152 * }
153 * }
154 */
155
156 using namespace json_spirit;
157 using namespace boost::posix_time;
158
159 Object json;
160 Object id;
161
162 id.push_back (Pair ("userName", boost::lexical_cast<string> (name)));
163 id.push_back (Pair ("seqNo", seq_no));
164
165 json.push_back (Pair ("id", id));
166
167 json.push_back (Pair ("timestamp", to_iso_string (from_time_t (action.timestamp ()))));
168 json.push_back (Pair ("filename", action.filename ()));
169 json.push_back (Pair ("action", (action.action () == 0) ? "UPDATE" : "DELETE"));
170
171 if (action.action () == 0)
172 {
173 Object update;
174 update.push_back (Pair ("hash", boost::lexical_cast<string> (Hash (action.file_hash ().c_str (), action.file_hash ().size ()))));
175 update.push_back (Pair ("timestamp", to_iso_string (from_time_t (action.mtime ()))));
176
177 ostringstream chmod;
178 chmod << setbase (8) << setfill ('0') << setw (4) << action.mode ();
179 update.push_back (Pair ("chmod", chmod.str ()));
180
181 update.push_back (Pair ("segNum", action.seg_num ()));
182 json.push_back (Pair ("update", update));
183 }
184
185 if (action.has_parent_device_name ())
186 {
187 Object parentId;
188 Ccnx::Name parent_device_name (action.parent_device_name ().c_str (), action.parent_device_name ().size ());
189 id.push_back (Pair ("userName", boost::lexical_cast<string> (parent_device_name)));
190 id.push_back (Pair ("seqNo", action.parent_seq_no ()));
191
192 json.push_back (Pair ("parentId", parentId));
193 }
194
195 actions.push_back (json);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800196}
197
198void
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800199StateServer::info_actions_folder (const Name &interest)
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800200{
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800201 if (interest.size () - m_PREFIX_INFO.size () != 4 &&
202 interest.size () - m_PREFIX_INFO.size () != 5)
203 {
204 _LOG_DEBUG ("Invalid interest: " << interest);
205 return;
206 }
207
208 _LOG_DEBUG (">> info_actions_all: " << interest);
209 m_executor.execute (bind (&StateServer::info_actions_folder_Execute, this, interest));
210}
211
212void
213StateServer::info_actions_folder_Execute (const Name &interest)
214{
215 // <PREFIX_INFO>/"actions"/"folder"/<folder>/<nonce>/<offset> get list of all actions
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800216
217 try
218 {
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800219 int offset = interest.getCompFromBackAsInt (0);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800220
221 /// @todo !!! add security checking
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800222
223 string folder;
224 if (interest.size () - m_PREFIX_INFO.size () == 5)
225 folder = interest.getCompFromBackAsString (2);
226 else // == 4
227 folder = "";
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800228/*
229 * {
230 * "actions": [
231 * ...
232 * ],
233 *
234 * // only if there are more actions available
235 * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-ACTION>"
236 * }
237 */
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800238
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800239 using namespace json_spirit;
240 Object json;
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800241
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800242 Array actions;
243 bool more = m_actionLog->LookupActionsInFolderRecursively
244 (boost::bind (StateServer::formatActionJson, boost::ref(actions), _1, _2, _3),
245 folder, offset*10, 10);
246
247 json.push_back (Pair ("actions", actions));
248
249 if (more)
250 {
251 Ccnx::Name more = Name (interest.getPartialName (0, interest.size () - 1))(offset + 1);
252 json.push_back (Pair ("more", lexical_cast<string> (more)));
253 }
254
255 ostringstream os;
256 write_stream (Value (json), os, pretty_print | raw_utf8);
257 m_ccnx->publishData (interest, os.str (), 1);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800258 }
259 catch (Ccnx::NameException &ne)
260 {
261 // ignore any unexpected interests and errors
262 _LOG_ERROR (*boost::get_error_info<Ccnx::error_info_str>(ne));
263 }
264}
265
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800266void debugFileState (const FileItem &file)
267{
268 std::cout << file.filename () << std::endl;
269}
270
271void
272StateServer::info_filestate_folder (const Ccnx::Name &interest)
273{
274 if (interest.size () - m_PREFIX_INFO.size () != 4 &&
275 interest.size () - m_PREFIX_INFO.size () != 5)
276 {
277 _LOG_DEBUG ("Invalid interest: " << interest << ", " << interest.size () - m_PREFIX_INFO.size ());
278 return;
279 }
280
281 _LOG_DEBUG (">> info_filestate_folder: " << interest);
282 m_executor.execute (bind (&StateServer::info_filestate_folder_Execute, this, interest));
283}
284
285
286void
287StateServer::info_filestate_folder_Execute (const Ccnx::Name &interest)
288{
289 // <PREFIX_INFO>/"filestate"/"folder"/<one-component-relative-folder-name>/<nonce>/<offset>
290 try
291 {
292 int offset = interest.getCompFromBackAsInt (0);
293
294 // /// @todo !!! add security checking
295
296 string folder;
297 if (interest.size () - m_PREFIX_INFO.size () == 5)
298 folder = interest.getCompFromBackAsString (2);
299 else // == 4
300 folder = "";
301
302 FileStatePtr fileState = m_actionLog->GetFileState ();
303 fileState->LookupFilesInFolderRecursively (debugFileState, folder, offset*100, 100);
304
305 // m_ccnx->publishData (interest, "FAIL: Not implemented", 1);
306 }
307 catch (Ccnx::NameException &ne)
308 {
309 // ignore any unexpected interests and errors
310 _LOG_ERROR (*boost::get_error_info<Ccnx::error_info_str>(ne));
311 }
312}
313
314
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800315void
316StateServer::cmd_restore_file (const Ccnx::Name &interest)
317{
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800318 if (interest.size () - m_PREFIX_CMD.size () != 4 &&
319 interest.size () - m_PREFIX_CMD.size () != 5)
320 {
321 _LOG_DEBUG ("Invalid interest: " << interest);
322 return;
323 }
324
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800325 _LOG_DEBUG (">> cmd_restore_file: " << interest);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800326 m_executor.execute (bind (&StateServer::cmd_restore_file_Execute, this, interest));
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800327}
328
329void
330StateServer::cmd_restore_file_Execute (const Ccnx::Name &interest)
331{
332 // <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash>
333
334 /// @todo !!! add security checking
335
336 try
337 {
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800338 FileItemPtr file;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800339
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800340 if (interest.size () - m_PREFIX_CMD.size () == 5)
341 {
342 Hash hash (head(interest.getCompFromBack (0)), interest.getCompFromBack (0).size());
343 int64_t version = interest.getCompFromBackAsInt (1);
344 string filename = interest.getCompFromBackAsString (2); // should be safe even with full relative path
345
346 file = m_actionLog->LookupAction (filename, version, hash);
347 if (!file)
348 {
349 _LOG_ERROR ("Requested file is not found: [" << filename << "] version [" << version << "] hash [" << hash.shortHash () << "]");
350 }
351 }
352 else
353 {
354 int64_t version = interest.getCompFromBackAsInt (0);
355 string filename = interest.getCompFromBackAsString (1); // should be safe even with full relative path
356
357 file = m_actionLog->LookupAction (filename, version, Hash (0,0));
358 if (!file)
359 {
360 _LOG_ERROR ("Requested file is not found: [" << filename << "] version [" << version << "]");
361 }
362 }
363
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800364 if (!file)
365 {
366 m_ccnx->publishData (interest, "FAIL: Requested file is not found", 1);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800367 return;
368 }
369
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800370 Hash hash = Hash (file->file_hash ().c_str (), file->file_hash ().size ());
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800371
372 ///////////////////
373 // now the magic //
374 ///////////////////
375
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800376 boost::filesystem::path filePath = m_rootDir / file->filename ();
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800377 Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
378
379 try
380 {
381 if (filesystem::exists (filePath) &&
382 filesystem::last_write_time (filePath) == file->mtime () &&
383 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
384 *Hash::FromFileContent (filePath) == hash)
385 {
386 m_ccnx->publishData (interest, "OK: File already exists", 1);
387 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
388 return;
389 }
390 }
391 catch (filesystem::filesystem_error &error)
392 {
393 m_ccnx->publishData (interest, "FAIL: File operation failed", 1);
394 _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
395 }
396
397 _LOG_TRACE ("Restoring file [" << filePath << "]");
398 if (m_objectManager.objectsToLocalFile (deviceName, hash, filePath))
399 {
400 last_write_time (filePath, file->mtime ());
401 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
402 m_ccnx->publishData (interest, "OK", 1);
403 }
404 else
405 {
406 m_ccnx->publishData (interest, "FAIL: Unknown error while restoring file", 1);
407 }
408 }
409 catch (Ccnx::NameException &ne)
410 {
411 // ignore any unexpected interests and errors
412 _LOG_ERROR(*boost::get_error_info<Ccnx::error_info_str>(ne));
413 }
414}