Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, 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 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 24 | #include "action-log.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 25 | #include "ccnx-wrapper.hpp" |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 26 | #include "executor.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #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 Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 34 | #include "../contrib/json_spirit/json_spirit_value.h" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 35 | #include "../contrib/json_spirit/json_spirit_writer_template.h" |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 36 | |
| 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 Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 41 | /** |
| 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 Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 48 | * For now serving only locally (using <PREFIX> = /localhost/<user's-device-name>/"chronoshare"/<FOLDER>/"info") |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 49 | * |
| 50 | * - state: get list of SyncNodes, their sequence numbers, and forwarding hint (almost the same as RECOVERY interest) |
| 51 | * |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 52 | * <PREFIX_INFO>/"state" (@todo: authentification code or authentication code should in addition somewhere) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 53 | * |
| 54 | * - action |
| 55 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 56 | * Get list of actions for a folder (for all files under this folder) |
| 57 | * |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 58 | * <PREFIX_INFO>/"actions"/"folder"/<offset> (all actions) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 59 | * or |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 60 | * <PREFIX_INFO>/"actions"/"folder"/<one-component-relative-file-name>/<offset> |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 61 | * |
| 62 | * Actions are ordered in decreasing order (latest will go first). |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 63 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 64 | * Each data packet contains up to 100 actions. |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 65 | * |
| 66 | * TEMPORARILY LIMIT IS REDUCED TO 10 ! (for debug purposes) |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 67 | * (may be even not temporarily...) |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 68 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 69 | * If more items are available, application data will specify URL for the next packet |
| 70 | * |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 71 | * 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 Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 100 | * "more": "next segment number" |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 101 | * } |
| 102 | * |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 103 | * |
| 104 | * - file |
| 105 | * |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 106 | * <PREFIX_INFO>/"files"/"folder"/<offset> (full filestate) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 107 | * or |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 108 | * <PREFIX_INFO>/"files"/"folder"/<one-component-relative-folder-name>/<offset> |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 109 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 110 | * 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 Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 113 | * 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 Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 134 | * "more": "next segment number" |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 135 | * } |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 136 | * |
| 137 | * Commands available: |
| 138 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 139 | * For now serving only locally (using <PREFIX_CMD> = /localhost/<user's-device-name>/"chronoshare"/<FOLDER>/"cmd") |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 140 | * |
| 141 | * - restore version of the file |
| 142 | * |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 143 | * <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version> |
| 144 | * or |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 145 | * <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 | */ |
| 151 | class StateServer |
| 152 | { |
| 153 | public: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 154 | 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 Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 158 | ~StateServer(); |
| 159 | |
| 160 | private: |
| 161 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 162 | info_actions_folder(const Ccnx::Name& interest); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 163 | |
| 164 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 165 | info_actions_file(const Ccnx::Name& interest); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 166 | |
| 167 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 168 | info_actions_fileOrFolder_Execute(const Ccnx::Name& interest, bool isFolder = true); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 169 | |
| 170 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 171 | info_files_folder(const Ccnx::Name& interest); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 172 | |
| 173 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 174 | info_files_folder_Execute(const Ccnx::Name& interest); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 175 | |
| 176 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 177 | cmd_restore_file(const Ccnx::Name& interest); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 178 | |
| 179 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 180 | cmd_restore_file_Execute(const Ccnx::Name& interest); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 181 | |
| 182 | private: |
| 183 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 184 | registerPrefixes(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 185 | |
| 186 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 187 | deregisterPrefixes(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 189 | static void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 190 | formatActionJson(json_spirit::Array& actions, const Ccnx::Name& name, sqlite3_int64 seq_no, |
| 191 | const ActionItem& action); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 192 | |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 193 | static void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 194 | formatFilestateJson(json_spirit::Array& files, const FileItem& file); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 195 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 196 | private: |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 197 | Ndnx::NdnxWrapperPtr m_ndnx; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 198 | ActionLogPtr m_actionLog; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 199 | ObjectManager& m_objectManager; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 200 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 201 | Ndnx::Name m_PREFIX_INFO; |
| 202 | Ndnx::Name m_PREFIX_CMD; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 203 | |
| 204 | boost::filesystem::path m_rootDir; |
| 205 | int m_freshness; |
| 206 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 207 | Executor m_executor; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 208 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 209 | Ccnx::Name m_userName; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 210 | std::string m_sharedFolderName; |
| 211 | std::string m_appName; |
| 212 | }; |
| 213 | #endif // CONTENT_SERVER_H |