blob: 72dd275a0892d908c675ce1380545a53608709fd [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyev026eaf32013-02-23 16:37:14 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev026eaf32013-02-23 16:37:14 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080019 */
20
21#ifndef STATE_SERVER_H
22#define STATE_SERVER_H
23
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "action-log.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080025#include "ccnx-wrapper.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080026#include "executor.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080027#include "object-db.hpp"
28#include "object-manager.hpp"
29#include <boost/thread/locks.hpp>
30#include <boost/thread/shared_mutex.hpp>
31#include <map>
32#include <set>
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080033
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080034#include "../contrib/json_spirit/json_spirit_value.h"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080035#include "../contrib/json_spirit/json_spirit_writer_template.h"
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080036
37#ifndef JSON_SPIRIT_VALUE_ENABLED
38#error Please define JSON_SPIRIT_VALUE_ENABLED for the Value type to be enabled
39#endif
40
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080041/**
42 * @brief Class serving state information from ChronoShare
43 *
44 * Eventually, the same info/actions can be made available via a global scope prefix
45 *
46 * Information available:
47 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080048 * For now serving only locally (using <PREFIX> = /localhost/<user's-device-name>/"chronoshare"/<FOLDER>/"info")
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080049 *
50 * - state: get list of SyncNodes, their sequence numbers, and forwarding hint (almost the same as RECOVERY interest)
51 *
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080052 * <PREFIX_INFO>/"state" (@todo: authentification code or authentication code should in addition somewhere)
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080053 *
54 * - action
55 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080056 * Get list of actions for a folder (for all files under this folder)
57 *
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080058 * <PREFIX_INFO>/"actions"/"folder"/<offset> (all actions)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080059 * or
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080060 * <PREFIX_INFO>/"actions"/"folder"/<one-component-relative-file-name>/<offset>
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080061 *
62 * Actions are ordered in decreasing order (latest will go first).
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080063 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080064 * Each data packet contains up to 100 actions.
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080065 *
66 * TEMPORARILY LIMIT IS REDUCED TO 10 ! (for debug purposes)
Alexander Afanasyev94240b52013-02-27 11:57:29 -080067 * (may be even not temporarily...)
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080068 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080069 * If more items are available, application data will specify URL for the next packet
70 *
Alexander Afanasyeve1c95042013-02-27 01:02:36 -080071 * Format of returned data (JSON):
72 * {
73 * "actions": [
74 * {
75 * "id": {
76 * "userName": "<NDN-NAME-OF-THE-USER>",
77 * "seqNo": "<SEQ_NO_OF_THE_ACTION>"
78 * },
79 * "timestamp": "<ACTION-TIMESTAMP>",
80 * "filename": "<FILENAME>",
81 *
82 * "action": "UPDATE | DELETE",
83 *
84 * // only if update
85 * "update": {
86 * "hash": "<FILE-HASH>",
87 * "timestamp": "<FILE-TIMESTAMP>",
88 * "chmod": "<FILE-MODE>",
89 * "segNum": "<NUMBER-OF-SEGMENTS (~file size)>"
90 * },
91 *
92 * // if parent_device_name is set
93 * "parentId": {
94 * "userName": "<NDN-NAME-OF-THE-USER>",
95 * "seqNo": "<SEQ_NO_OF_THE_ACTION>"
96 * };
97 * },
98 *
99 * // only if there are more actions available
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800100 * "more": "next segment number"
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800101 * }
102 *
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800103 *
104 * - file
105 *
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800106 * <PREFIX_INFO>/"files"/"folder"/<offset> (full filestate)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800107 * or
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800108 * <PREFIX_INFO>/"files"/"folder"/<one-component-relative-folder-name>/<offset>
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800109 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800110 * Each Data packets contains a list of up to 100 files.
111 * If more items are available, application data will specify URL for the next packet
112 *
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800113 * TEMPORARILY LIMIT IS REDUCED TO 10 ! (for debug purposes)
114 * (may be even not temporarily...)
115 *
116 * Format of returned data (JSON):
117 * {
118 * "files": [
119 * {
120 * "filename": "<FILENAME>",
121 * "owner": {
122 * "userName": "<NDN-NAME-OF-THE-USER>",
123 * "seqNo": "<SEQ_NO_OF_THE_ACTION>"
124 * },
125 *
126 * "hash": "<FILE-HASH>",
127 * "timestamp": "<FILE-TIMESTAMP>",
128 * "chmod": "<FILE-MODE>",
129 * "segNum": "<NUMBER-OF-SEGMENTS (~file size)>"
130 * }, ...,
131 * ]
132 *
133 * // only if there are more actions available
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800134 * "more": "next segment number"
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800135 * }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800136 *
137 * Commands available:
138 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800139 * For now serving only locally (using <PREFIX_CMD> = /localhost/<user's-device-name>/"chronoshare"/<FOLDER>/"cmd")
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800140 *
141 * - restore version of the file
142 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800143 * <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>
144 * or
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800145 * <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash>
146 *
147 * - clean state log
148 * (this may not need to be here, if we implement periodic cleaning)
149 * - ? flatten action log (should be supported eventually, but not supported now)
150 */
151class StateServer
152{
153public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800154 StateServer(Ccnx::CcnxWrapperPtr ccnx, ActionLogPtr actionLog,
155 const boost::filesystem::path& rootDir, const Ccnx::Name& userName,
156 const std::string& sharedFolderName, const std::string& appName,
157 ObjectManager& objectManager, int freshness = -1);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800158 ~StateServer();
159
160private:
161 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800162 info_actions_folder(const Ccnx::Name& interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800163
164 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800165 info_actions_file(const Ccnx::Name& interest);
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800166
167 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800168 info_actions_fileOrFolder_Execute(const Ccnx::Name& interest, bool isFolder = true);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800169
170 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800171 info_files_folder(const Ccnx::Name& interest);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800172
173 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800174 info_files_folder_Execute(const Ccnx::Name& interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800175
176 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800177 cmd_restore_file(const Ccnx::Name& interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800178
179 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800180 cmd_restore_file_Execute(const Ccnx::Name& interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800181
182private:
183 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800184 registerPrefixes();
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800185
186 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800187 deregisterPrefixes();
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800188
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800189 static void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800190 formatActionJson(json_spirit::Array& actions, const Ccnx::Name& name, sqlite3_int64 seq_no,
191 const ActionItem& action);
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800192
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800193 static void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800194 formatFilestateJson(json_spirit::Array& files, const FileItem& file);
Alexander Afanasyev94240b52013-02-27 11:57:29 -0800195
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;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800199 ObjectManager& m_objectManager;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800200
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 Afanasyeveda3b7a2016-12-25 11:26:40 -0800207 Executor m_executor;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800208
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800209 Ccnx::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