blob: b4d69e032220b4c64e004de14cf9247472949066 [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
25#include "ccnx-wrapper.h"
26#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 *
53 * <PREFIX_INFO>/"state"/<nonce> (nonce should probably be the authentification code or authentication code should in addition somewhere)
54 *
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 *
59 * <PREFIX_INFO>/"actions"/"folder"/<nonce>/<offset> (all actions)
60 * or
61 * <PREFIX_INFO>/"actions"/"folder"/<one-component-relative-file-name>/<nonce>/<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)
68 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080069 * If more items are available, application data will specify URL for the next packet
70 *
71 * @todo SPECIFY FORMAT OF THIS FIELD
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
101 * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-ACTION>"
102 * }
103 *
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800104 *
105 * - file
106 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800107 * <PREFIX_INFO>/"filestate"/"folder"/<nonce>/<offset> (full filestate)
108 * or
109 * <PREFIX_INFO>/"filestate"/"folder"/<one-component-relative-folder-name>/<nonce>/<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 *
114 * @todo SPECIFY FORMAT OF THIS FIELD
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800115 *
116 * Commands available:
117 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800118 * For now serving only locally (using <PREFIX_CMD> = /localhost/<user's-device-name>/"chronoshare"/<FOLDER>/"cmd")
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800119 *
120 * - restore version of the file
121 *
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800122 * <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>
123 * or
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800124 * <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash>
125 *
126 * - clean state log
127 * (this may not need to be here, if we implement periodic cleaning)
128 * - ? flatten action log (should be supported eventually, but not supported now)
129 */
130class StateServer
131{
132public:
133 StateServer(Ccnx::CcnxWrapperPtr ccnx, ActionLogPtr actionLog, const boost::filesystem::path &rootDir,
134 const Ccnx::Name &userName, const std::string &sharedFolderName, const std::string &appName,
135 ObjectManager &objectManager,
136 int freshness = -1);
137 ~StateServer();
138
139private:
140 void
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800141 info_actions_folder (const Ccnx::Name &interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800142
143 void
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800144 info_actions_folder_Execute (const Ccnx::Name &interest);
145
146 void
147 info_filestate_folder (const Ccnx::Name &interest);
148
149 void
150 info_filestate_folder_Execute (const Ccnx::Name &interest);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800151
152 void
153 cmd_restore_file (const Ccnx::Name &interest);
154
155 void
156 cmd_restore_file_Execute (const Ccnx::Name &interest);
157
158private:
159 void
160 registerPrefixes ();
161
162 void
163 deregisterPrefixes ();
164
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800165 static void
166 formatActionJson (json_spirit::Array &actions, const Ccnx::Name &name, sqlite3_int64 seq_no, const ActionItem &action);
167
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800168private:
169 Ccnx::CcnxWrapperPtr m_ccnx;
170 ActionLogPtr m_actionLog;
171 ObjectManager &m_objectManager;
172
173 Ccnx::Name m_PREFIX_INFO;
174 Ccnx::Name m_PREFIX_CMD;
175
176 boost::filesystem::path m_rootDir;
177 int m_freshness;
178
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800179 Executor m_executor;
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800180
181 Ccnx::Name m_userName;
182 std::string m_sharedFolderName;
183 std::string m_appName;
184};
185#endif // CONTENT_SERVER_H