Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * 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 Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * 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 Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * 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 Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #ifndef STATE_SERVER_H |
| 22 | #define STATE_SERVER_H |
| 23 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 24 | #include "core/chronoshare-common.hpp" |
| 25 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 26 | #include "action-log.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include "object-db.hpp" |
| 28 | #include "object-manager.hpp" |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 30 | #include <map> |
| 31 | #include <set> |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 33 | #include "../contrib/json_spirit/json_spirit_value.h" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 34 | #include "../contrib/json_spirit/json_spirit_writer_template.h" |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 35 | |
| 36 | #ifndef JSON_SPIRIT_VALUE_ENABLED |
| 37 | #error Please define JSON_SPIRIT_VALUE_ENABLED for the Value type to be enabled |
| 38 | #endif |
| 39 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 40 | namespace ndn { |
| 41 | namespace chronoshare { |
| 42 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 43 | /** |
| 44 | * @brief Class serving state information from ChronoShare |
| 45 | * |
| 46 | * Eventually, the same info/actions can be made available via a global scope prefix |
| 47 | * |
| 48 | * Information available: |
| 49 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 50 | * For now serving only locally(using <PREFIX> = |
| 51 | * ndn:/localhop/<user's-device-name>/"chronoshare"/<FOLDER>/"info") |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 52 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 53 | * - state: get list of SyncNodes, their sequence numbers, and forwarding hint(almost the same as |
| 54 | * RECOVERY interest) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 55 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 56 | * <PREFIX_INFO>/"state" (@todo: authentification code or authentication code should in addition |
| 57 | * somewhere) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 58 | * |
| 59 | * - action |
| 60 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 61 | * Get list of actions for a folder(for all files under this folder) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 62 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 63 | * <PREFIX_INFO>/"actions"/"folder"/<offset> (all actions) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 64 | * or |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 65 | * <PREFIX_INFO>/"actions"/"folder"/<one-component-relative-file-name>/<offset> |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 66 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 67 | * Actions are ordered in decreasing order(latest will go first). |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 68 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 69 | * Each data packet contains up to 100 actions. |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 70 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 71 | * TEMPORARILY LIMIT IS REDUCED TO 10 !(for debug purposes) |
| 72 | * (may be even not temporarily...) |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 73 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 74 | * If more items are available, application data will specify URL for the next packet |
| 75 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 76 | * Format of returned data(JSON): |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 77 | * { |
| 78 | * "actions": [ |
| 79 | * { |
| 80 | * "id": { |
| 81 | * "userName": "<NDN-NAME-OF-THE-USER>", |
| 82 | * "seqNo": "<SEQ_NO_OF_THE_ACTION>" |
| 83 | * }, |
| 84 | * "timestamp": "<ACTION-TIMESTAMP>", |
| 85 | * "filename": "<FILENAME>", |
| 86 | * |
| 87 | * "action": "UPDATE | DELETE", |
| 88 | * |
| 89 | * // only if update |
| 90 | * "update": { |
| 91 | * "hash": "<FILE-HASH>", |
| 92 | * "timestamp": "<FILE-TIMESTAMP>", |
| 93 | * "chmod": "<FILE-MODE>", |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 94 | * "segNum": "<NUMBER-OF-SEGMENTS(~file size)>" |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 95 | * }, |
| 96 | * |
| 97 | * // if parent_device_name is set |
| 98 | * "parentId": { |
| 99 | * "userName": "<NDN-NAME-OF-THE-USER>", |
| 100 | * "seqNo": "<SEQ_NO_OF_THE_ACTION>" |
| 101 | * }; |
| 102 | * }, |
| 103 | * |
| 104 | * // only if there are more actions available |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 105 | * "more": "next segment number" |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 106 | * } |
| 107 | * |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 108 | * |
| 109 | * - file |
| 110 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 111 | * <PREFIX_INFO>/"files"/"folder"/<offset> (full filestate) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 112 | * or |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 113 | * <PREFIX_INFO>/"files"/"folder"/<one-component-relative-folder-name>/<offset> |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 114 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 115 | * Each Data packets contains a list of up to 100 files. |
| 116 | * If more items are available, application data will specify URL for the next packet |
| 117 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 118 | * TEMPORARILY LIMIT IS REDUCED TO 10 !(for debug purposes) |
| 119 | * (may be even not temporarily...) |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 120 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 121 | * Format of returned data(JSON): |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 122 | * { |
| 123 | * "files": [ |
| 124 | * { |
| 125 | * "filename": "<FILENAME>", |
| 126 | * "owner": { |
| 127 | * "userName": "<NDN-NAME-OF-THE-USER>", |
| 128 | * "seqNo": "<SEQ_NO_OF_THE_ACTION>" |
| 129 | * }, |
| 130 | * |
| 131 | * "hash": "<FILE-HASH>", |
| 132 | * "timestamp": "<FILE-TIMESTAMP>", |
| 133 | * "chmod": "<FILE-MODE>", |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 134 | * "segNum": "<NUMBER-OF-SEGMENTS(~file size)>" |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 135 | * }, ..., |
| 136 | * ] |
| 137 | * |
| 138 | * // only if there are more actions available |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 139 | * "more": "next segment number" |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 140 | * } |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 141 | * |
| 142 | * Commands available: |
| 143 | * |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 144 | * For now serving only locally(using <PREFIX_CMD> = |
| 145 | * ndn:/localhop/<user's-device-name>/"chronoshare"/<FOLDER>/"cmd") |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 146 | * |
| 147 | * - restore version of the file |
| 148 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 149 | * <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version> |
| 150 | * or |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 151 | * <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash> |
| 152 | * |
| 153 | * - clean state log |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 154 | * (this may not need to be here, if we implement periodic cleaning) |
| 155 | * - ? flatten action log(should be supported eventually, but not supported now) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 156 | */ |
| 157 | class StateServer |
| 158 | { |
| 159 | public: |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 160 | StateServer(Face& face, ActionLogPtr actionLog, const boost::filesystem::path& rootDir, |
| 161 | const Name& userName, const std::string& sharedFolderName, const name::Component& appName, |
| 162 | ObjectManager& objectManager, KeyChain& keyChain, |
| 163 | time::milliseconds freshness = time::seconds(5)); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 164 | ~StateServer(); |
| 165 | |
| 166 | private: |
| 167 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 168 | info_actions_folder(const InterestFilter&, const Interest&); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 169 | |
| 170 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 171 | info_actions_file(const InterestFilter&, const Interest&); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 172 | |
| 173 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 174 | info_actions_fileOrFolder_Execute(const Name& interest, bool isFolder = true); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 175 | |
| 176 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 177 | info_files_folder(const InterestFilter&, const Interest&); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 178 | |
| 179 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 180 | info_files_folder_Execute(const Name& interest); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 181 | |
| 182 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 183 | cmd_restore_file(const InterestFilter&, const Interest&); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 184 | |
| 185 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 186 | cmd_restore_file_Execute(const Name& interest); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 187 | |
| 188 | private: |
| 189 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 190 | registerPrefixes(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 191 | |
| 192 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 193 | deregisterPrefixes(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 195 | static void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 196 | formatActionJson(json_spirit::Array& actions, const Name& name, sqlite3_int64 seq_no, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 197 | const ActionItem& action); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 198 | |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 199 | static void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 200 | formatFilestateJson(json_spirit::Array& files, const FileItem& file); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 201 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 202 | private: |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 203 | Face& m_face; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 204 | ActionLogPtr m_actionLog; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 205 | ObjectManager& m_objectManager; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 206 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 207 | Name m_PREFIX_INFO; |
| 208 | Name m_PREFIX_CMD; |
| 209 | |
| 210 | const RegisteredPrefixId* actionsFolderId; |
| 211 | const RegisteredPrefixId* actionsFileId; |
| 212 | const RegisteredPrefixId* filesFolderId; |
| 213 | const RegisteredPrefixId* restoreFileId; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 214 | |
| 215 | boost::filesystem::path m_rootDir; |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 216 | time::milliseconds m_freshness; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 217 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 218 | Name m_userName; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 219 | std::string m_sharedFolderName; |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 220 | name::Component m_appName; |
| 221 | KeyChain& m_keyChain; |
| 222 | |
| 223 | boost::asio::io_service& m_ioService; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 224 | }; |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 225 | |
| 226 | } // namespace chronoshare |
| 227 | } // namespace ndn |
| 228 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 229 | #endif // CONTENT_SERVER_H |