blob: 26a6697ae7064e15e1af4d730112ca758cbfe4db [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: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22#ifndef STATE_SERVER_H
23#define STATE_SERVER_H
24
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070025#include "ndnx-wrapper.h"
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080026#include "object-manager.h"
27#include "object-db.h"
28#include "action-log.h"
29#include <set>
30#include <map>
31#include <boost/thread/shared_mutex.hpp>
32#include <boost/thread/locks.hpp>
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080033#include "executor.h"
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080034
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080035#include "../contrib/json_spirit/json_spirit_writer_template.h"
36#include "../contrib/json_spirit/json_spirit_value.h"
37
38#ifndef JSON_SPIRIT_VALUE_ENABLED
39#error Please define JSON_SPIRIT_VALUE_ENABLED for the Value type to be enabled
40#endif
41
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080042/**
43 * @brief Class serving state information from ChronoShare
44 *
45 * Eventually, the same info/actions can be made available via a global scope prefix
46 *
47 * Information available:
48 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080049 * For now serving only locally (using <PREFIX> = /localhost/<user's-device-name>/"chronoshare"/<FOLDER>/"info")
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080050 *
51 * - state: get list of SyncNodes, their sequence numbers, and forwarding hint (almost the same as RECOVERY interest)
52 *
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080053 * <PREFIX_INFO>/"state" (@todo: authentification code or authentication code should in addition somewhere)
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080054 *
55 * - action
56 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080057 * Get list of actions for a folder (for all files under this folder)
58 *
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080059 * <PREFIX_INFO>/"actions"/"folder"/<offset> (all actions)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080060 * or
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080061 * <PREFIX_INFO>/"actions"/"folder"/<one-component-relative-file-name>/<offset>
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080062 *
63 * Actions are ordered in decreasing order (latest will go first).
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080064 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080065 * Each data packet contains up to 100 actions.
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080066 *
67 * TEMPORARILY LIMIT IS REDUCED TO 10 ! (for debug purposes)
Alexander Afanasyev94240b52013-02-27 11:57:29 -080068 * (may be even not temporarily...)
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080069 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080070 * If more items are available, application data will specify URL for the next packet
71 *
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080072 * Format of returned data (JSON):
73 * {
74 * "actions": [
75 * {
76 * "id": {
77 * "userName": "<NDN-NAME-OF-THE-USER>",
78 * "seqNo": "<SEQ_NO_OF_THE_ACTION>"
79 * },
80 * "timestamp": "<ACTION-TIMESTAMP>",
81 * "filename": "<FILENAME>",
82 *
83 * "action": "UPDATE | DELETE",
84 *
85 * // only if update
86 * "update": {
87 * "hash": "<FILE-HASH>",
88 * "timestamp": "<FILE-TIMESTAMP>",
89 * "chmod": "<FILE-MODE>",
90 * "segNum": "<NUMBER-OF-SEGMENTS (~file size)>"
91 * },
92 *
93 * // if parent_device_name is set
94 * "parentId": {
95 * "userName": "<NDN-NAME-OF-THE-USER>",
96 * "seqNo": "<SEQ_NO_OF_THE_ACTION>"
97 * };
98 * },
99 *
100 * // only if there are more actions available
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800101 * "more": "next segment number"
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800102 * }
103 *
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800104 *
105 * - file
106 *
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800107 * <PREFIX_INFO>/"files"/"folder"/<offset> (full filestate)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800108 * or
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800109 * <PREFIX_INFO>/"files"/"folder"/<one-component-relative-folder-name>/<offset>
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800110 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800111 * Each Data packets contains a list of up to 100 files.
112 * If more items are available, application data will specify URL for the next packet
113 *
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800114 * TEMPORARILY LIMIT IS REDUCED TO 10 ! (for debug purposes)
115 * (may be even not temporarily...)
116 *
117 * Format of returned data (JSON):
118 * {
119 * "files": [
120 * {
121 * "filename": "<FILENAME>",
122 * "owner": {
123 * "userName": "<NDN-NAME-OF-THE-USER>",
124 * "seqNo": "<SEQ_NO_OF_THE_ACTION>"
125 * },
126 *
127 * "hash": "<FILE-HASH>",
128 * "timestamp": "<FILE-TIMESTAMP>",
129 * "chmod": "<FILE-MODE>",
130 * "segNum": "<NUMBER-OF-SEGMENTS (~file size)>"
131 * }, ...,
132 * ]
133 *
134 * // only if there are more actions available
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800135 * "more": "next segment number"
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800136 * }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800137 *
138 * Commands available:
139 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800140 * For now serving only locally (using <PREFIX_CMD> = /localhost/<user's-device-name>/"chronoshare"/<FOLDER>/"cmd")
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800141 *
142 * - restore version of the file
143 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800144 * <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>
145 * or
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800146 * <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash>
147 *
148 * - clean state log
149 * (this may not need to be here, if we implement periodic cleaning)
150 * - ? flatten action log (should be supported eventually, but not supported now)
151 */
152class StateServer
153{
154public:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700155 StateServer(Ndnx::NdnxWrapperPtr ndnx, ActionLogPtr actionLog, const boost::filesystem::path &rootDir,
156 const Ndnx::Name &userName, const std::string &sharedFolderName, const std::string &appName,
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800157 ObjectManager &objectManager,
158 int freshness = -1);
159 ~StateServer();
160
161private:
162 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700163 info_actions_folder (const Ndnx::Name &interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800164
165 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700166 info_actions_file (const Ndnx::Name &interest);
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800167
168 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700169 info_actions_fileOrFolder_Execute (const Ndnx::Name &interest, bool isFolder = true);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800170
171 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700172 info_files_folder (const Ndnx::Name &interest);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800173
174 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700175 info_files_folder_Execute (const Ndnx::Name &interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800176
177 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700178 cmd_restore_file (const Ndnx::Name &interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800179
180 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700181 cmd_restore_file_Execute (const Ndnx::Name &interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800182
183private:
184 void
185 registerPrefixes ();
186
187 void
188 deregisterPrefixes ();
189
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800190 static void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700191 formatActionJson (json_spirit::Array &actions, const Ndnx::Name &name, sqlite3_int64 seq_no, const ActionItem &action);
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800192
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800193 static void
194 formatFilestateJson (json_spirit::Array &files, const FileItem &file);
195
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800196private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700197 Ndnx::NdnxWrapperPtr m_ndnx;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800198 ActionLogPtr m_actionLog;
199 ObjectManager &m_objectManager;
200
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700201 Ndnx::Name m_PREFIX_INFO;
202 Ndnx::Name m_PREFIX_CMD;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800203
204 boost::filesystem::path m_rootDir;
205 int m_freshness;
206
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800207 Executor m_executor;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800208
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700209 Ndnx::Name m_userName;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800210 std::string m_sharedFolderName;
211 std::string m_appName;
212};
213#endif // CONTENT_SERVER_H