blob: 48b63de4ea057ab57475ba42008d1f551d78ba11 [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>
Alexander Afanasyev94240b52013-02-27 11:57:29 -080084 m_ccnx->setInterestFilter (Name (m_PREFIX_INFO)("files")("folder"), bind(&StateServer::info_files_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"));
Alexander Afanasyev94240b52013-02-27 11:57:29 -080094 m_ccnx->clearInterestFilter (Name (m_PREFIX_INFO)("files")("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
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800167 json.push_back (Pair ("timestamp", to_iso_extended_string (from_time_t (action.timestamp ()))));
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800168 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 ()))));
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800175 update.push_back (Pair ("timestamp", to_iso_extended_string (from_time_t (action.mtime ()))));
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800176
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 Afanasyev94240b52013-02-27 11:57:29 -0800266void
267StateServer::formatFilestateJson (json_spirit::Array &files, const FileItem &file)
268{
269/**
270 * {
271 * "filestate": [
272 * {
273 * "filename": "<FILENAME>",
274 * "owner": {
275 * "userName": "<NDN-NAME-OF-THE-USER>",
276 * "seqNo": "<SEQ_NO_OF_THE_ACTION>"
277 * },
278 *
279 * "hash": "<FILE-HASH>",
280 * "timestamp": "<FILE-TIMESTAMP>",
281 * "chmod": "<FILE-MODE>",
282 * "segNum": "<NUMBER-OF-SEGMENTS (~file size)>"
283 * }, ...,
284 * ]
285 *
286 * // only if there are more actions available
287 * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-FILESTATE>"
288 * }
289 */
290 using namespace json_spirit;
291 using namespace boost::posix_time;
292
293 Object json;
294
295 json.push_back (Pair ("filename", file.filename ()));
296 json.push_back (Pair ("version", file.version ()));
297 {
298 Object owner;
299 Ccnx::Name device_name (file.device_name ().c_str (), file.device_name ().size ());
300 owner.push_back (Pair ("userName", boost::lexical_cast<string> (device_name)));
301 owner.push_back (Pair ("seqNo", file.seq_no ()));
302
303 json.push_back (Pair ("owner", owner));
304 }
305
306 json.push_back (Pair ("hash", boost::lexical_cast<string> (Hash (file.file_hash ().c_str (), file.file_hash ().size ()))));
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800307 json.push_back (Pair ("timestamp", to_iso_extended_string (from_time_t (file.mtime ()))));
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800308
309 ostringstream chmod;
310 chmod << setbase (8) << setfill ('0') << setw (4) << file.mode ();
311 json.push_back (Pair ("chmod", chmod.str ()));
312
313 json.push_back (Pair ("segNum", file.seg_num ()));
314
315 files.push_back (json);
316}
317
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800318void debugFileState (const FileItem &file)
319{
320 std::cout << file.filename () << std::endl;
321}
322
323void
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800324StateServer::info_files_folder (const Ccnx::Name &interest)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800325{
326 if (interest.size () - m_PREFIX_INFO.size () != 4 &&
327 interest.size () - m_PREFIX_INFO.size () != 5)
328 {
329 _LOG_DEBUG ("Invalid interest: " << interest << ", " << interest.size () - m_PREFIX_INFO.size ());
330 return;
331 }
332
333 _LOG_DEBUG (">> info_filestate_folder: " << interest);
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800334 m_executor.execute (bind (&StateServer::info_files_folder_Execute, this, interest));
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800335}
336
337
338void
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800339StateServer::info_files_folder_Execute (const Ccnx::Name &interest)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800340{
341 // <PREFIX_INFO>/"filestate"/"folder"/<one-component-relative-folder-name>/<nonce>/<offset>
342 try
343 {
344 int offset = interest.getCompFromBackAsInt (0);
345
346 // /// @todo !!! add security checking
347
348 string folder;
349 if (interest.size () - m_PREFIX_INFO.size () == 5)
350 folder = interest.getCompFromBackAsString (2);
351 else // == 4
352 folder = "";
353
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800354/*
355 * {
356 * "files": [
357 * ...
358 * ],
359 *
360 * // only if there are more actions available
361 * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-ACTION>"
362 * }
363 */
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800364
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800365 using namespace json_spirit;
366 Object json;
367
368 Array files;
369 bool more = m_actionLog
370 ->GetFileState ()
371 ->LookupFilesInFolderRecursively
372 (boost::bind (StateServer::formatFilestateJson, boost::ref (files), _1),
373 folder, offset*10, 10);
374
375 json.push_back (Pair ("files", files));
376
377 if (more)
378 {
379 Ccnx::Name more = Name (interest.getPartialName (0, interest.size () - 1))(offset + 1);
380 json.push_back (Pair ("more", lexical_cast<string> (more)));
381 }
382
383 ostringstream os;
384 write_stream (Value (json), os, pretty_print | raw_utf8);
385 m_ccnx->publishData (interest, os.str (), 1);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800386 }
387 catch (Ccnx::NameException &ne)
388 {
389 // ignore any unexpected interests and errors
390 _LOG_ERROR (*boost::get_error_info<Ccnx::error_info_str>(ne));
391 }
392}
393
394
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800395void
396StateServer::cmd_restore_file (const Ccnx::Name &interest)
397{
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800398 if (interest.size () - m_PREFIX_CMD.size () != 4 &&
399 interest.size () - m_PREFIX_CMD.size () != 5)
400 {
401 _LOG_DEBUG ("Invalid interest: " << interest);
402 return;
403 }
404
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800405 _LOG_DEBUG (">> cmd_restore_file: " << interest);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800406 m_executor.execute (bind (&StateServer::cmd_restore_file_Execute, this, interest));
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800407}
408
409void
410StateServer::cmd_restore_file_Execute (const Ccnx::Name &interest)
411{
412 // <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash>
413
414 /// @todo !!! add security checking
415
416 try
417 {
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800418 FileItemPtr file;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800419
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800420 if (interest.size () - m_PREFIX_CMD.size () == 5)
421 {
422 Hash hash (head(interest.getCompFromBack (0)), interest.getCompFromBack (0).size());
423 int64_t version = interest.getCompFromBackAsInt (1);
424 string filename = interest.getCompFromBackAsString (2); // should be safe even with full relative path
425
426 file = m_actionLog->LookupAction (filename, version, hash);
427 if (!file)
428 {
429 _LOG_ERROR ("Requested file is not found: [" << filename << "] version [" << version << "] hash [" << hash.shortHash () << "]");
430 }
431 }
432 else
433 {
434 int64_t version = interest.getCompFromBackAsInt (0);
435 string filename = interest.getCompFromBackAsString (1); // should be safe even with full relative path
436
437 file = m_actionLog->LookupAction (filename, version, Hash (0,0));
438 if (!file)
439 {
440 _LOG_ERROR ("Requested file is not found: [" << filename << "] version [" << version << "]");
441 }
442 }
443
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800444 if (!file)
445 {
446 m_ccnx->publishData (interest, "FAIL: Requested file is not found", 1);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800447 return;
448 }
449
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800450 Hash hash = Hash (file->file_hash ().c_str (), file->file_hash ().size ());
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800451
452 ///////////////////
453 // now the magic //
454 ///////////////////
455
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800456 boost::filesystem::path filePath = m_rootDir / file->filename ();
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800457 Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
458
459 try
460 {
461 if (filesystem::exists (filePath) &&
462 filesystem::last_write_time (filePath) == file->mtime () &&
463 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
464 *Hash::FromFileContent (filePath) == hash)
465 {
466 m_ccnx->publishData (interest, "OK: File already exists", 1);
467 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
468 return;
469 }
470 }
471 catch (filesystem::filesystem_error &error)
472 {
473 m_ccnx->publishData (interest, "FAIL: File operation failed", 1);
474 _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
475 }
476
477 _LOG_TRACE ("Restoring file [" << filePath << "]");
478 if (m_objectManager.objectsToLocalFile (deviceName, hash, filePath))
479 {
480 last_write_time (filePath, file->mtime ());
481 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
482 m_ccnx->publishData (interest, "OK", 1);
483 }
484 else
485 {
486 m_ccnx->publishData (interest, "FAIL: Unknown error while restoring file", 1);
487 }
488 }
489 catch (Ccnx::NameException &ne)
490 {
491 // ignore any unexpected interests and errors
492 _LOG_ERROR(*boost::get_error_info<Ccnx::error_info_str>(ne));
493 }
494}