allox subkeys, step 1 (keys not active)
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibraryServer.java
CommitLineData
e42573a0 1package be.nikiroo.fanfix.library;
b0e88ebd 2
b0e88ebd 3import java.io.IOException;
edf79e5e 4import java.net.URL;
74a40dfb 5import java.util.ArrayList;
9b863b20 6import java.util.Date;
9b558341 7import java.util.HashMap;
68e2c6d2 8import java.util.List;
9b558341 9import java.util.Map;
b0e88ebd 10
7e51d91c
NR
11import javax.net.ssl.SSLException;
12
e42573a0 13import be.nikiroo.fanfix.Instance;
fb25273c 14import be.nikiroo.fanfix.bundles.Config;
74a40dfb 15import be.nikiroo.fanfix.data.Chapter;
b0e88ebd 16import be.nikiroo.fanfix.data.MetaData;
74a40dfb 17import be.nikiroo.fanfix.data.Paragraph;
085a2f9a 18import be.nikiroo.fanfix.data.Story;
b9ce9cad
NR
19import be.nikiroo.utils.Progress;
20import be.nikiroo.utils.Progress.ProgressListener;
416c54f8 21import be.nikiroo.utils.StringUtils;
fb25273c 22import be.nikiroo.utils.Version;
62c63b07
NR
23import be.nikiroo.utils.serial.server.ConnectActionServerObject;
24import be.nikiroo.utils.serial.server.ServerObject;
b0e88ebd 25
a85e8077 26/**
ea734ab4 27 * Create a new remote server that will listen for orders on the given port.
a85e8077 28 * <p>
ea734ab4
N
29 * The available commands are given as arrays of objects (first item is the
30 * command, the rest are the arguments).
2a25f781 31 * <p>
fb25273c
NR
32 * All the commands are always prefixed by the subkey (which can be EMPTY if
33 * none).
34 * <p>
a85e8077 35 * <ul>
fb25273c
NR
36 * <li>PING: will return the mode if the key is accepted (mode can be: "r/o" or
37 * "r/w")</li>
ea734ab4
N
38 * <li>GET_METADATA *: will return the metadata of all the stories in the
39 * library (array)</li> *
37abe20c 40 * <li>GET_METADATA [luid]: will return the metadata of the story of LUID luid</li>
ea734ab4
N
41 * <li>GET_STORY [luid]: will return the given story if it exists (or NULL if
42 * not)</li>
43 * <li>SAVE_STORY [luid]: save the story (that must be sent just after the
0fa0fe95 44 * command) with the given LUID, then return the LUID</li>
37abe20c
NR
45 * <li>IMPORT [url]: save the story found at the given URL, then return the LUID
46 * </li>
ea734ab4
N
47 * <li>DELETE_STORY [luid]: delete the story of LUID luid</li>
48 * <li>GET_COVER [luid]: return the cover of the story</li>
49 * <li>GET_CUSTOM_COVER ["SOURCE"|"AUTHOR"] [source]: return the cover for this
50 * source/author</li>
51 * <li>SET_COVER ["SOURCE"|"AUTHOR"] [value] [luid]: set the default cover for
52 * the given source/author to the cover of the story denoted by luid</li>
53 * <li>CHANGE_SOURCE [luid] [new source]: change the source of the story of LUID
54 * luid</li>
55 * <li>EXIT: stop the server</li>
a85e8077
NR
56 * </ul>
57 *
58 * @author niki
59 */
62c63b07 60public class RemoteLibraryServer extends ServerObject {
9b558341
NR
61 private Map<Long, String> commands = new HashMap<Long, String>();
62 private Map<Long, Long> times = new HashMap<Long, Long>();
fb25273c
NR
63 private Map<Long, Boolean> wls = new HashMap<Long, Boolean>();
64 private Map<Long, Boolean> rws = new HashMap<Long, Boolean>();
9b558341 65
a85e8077
NR
66 /**
67 * Create a new remote server (will not be active until
68 * {@link RemoteLibraryServer#start()} is called).
fb25273c
NR
69 * <p>
70 * Note: the key we use here is the encryption key (it must not contain a
71 * subkey).
a85e8077 72 *
2070ced5
NR
73 * @param key
74 * the key that will restrict access to this server
a85e8077
NR
75 * @param port
76 * the port to listen on
77 *
78 * @throws IOException
79 * in case of I/O error
80 */
2070ced5 81 public RemoteLibraryServer(String key, int port) throws IOException {
3040c4f0 82 super("Fanfix remote library", port, key);
b9ce9cad 83 setTraceHandler(Instance.getTraceHandler());
b0e88ebd
NR
84 }
85
86 @Override
fb25273c
NR
87 protected Object onRequest(ConnectActionServerObject action,
88 Version clientVersion, Object data, long id) throws Exception {
ea734ab4
N
89 long start = new Date().getTime();
90
fb25273c
NR
91 // defaults are positive (as previous versions without the feature)
92 boolean rw = true;
93 boolean wl = true;
94
95 String subkey = "";
085a2f9a
NR
96 String command = "";
97 Object[] args = new Object[0];
98 if (data instanceof Object[]) {
2070ced5 99 Object[] dataArray = (Object[]) data;
27eba894 100 if (dataArray.length > 0) {
fb25273c
NR
101 subkey = "" + dataArray[0];
102 }
103 if (dataArray.length > 1) {
104 command = "" + dataArray[1];
105
106 args = new Object[dataArray.length - 2];
107 for (int i = 2; i < dataArray.length; i++) {
108 args[i - 2] = dataArray[i];
109 }
110 }
111 }
112
113 List<String> whitelist = Instance.getConfig().getList(
114 Config.SERVER_WHITELIST);
115 if (whitelist == null) {
116 whitelist = new ArrayList<String>();
117 }
118
119 if (whitelist.isEmpty()) {
120 wl = false;
121 }
edf79e5e 122
fb25273c
NR
123 rw = Instance.getConfig().getBoolean(Config.SERVER_RW, rw);
124 if (!subkey.isEmpty()) {
125 List<String> allowed = Instance.getConfig().getList(
126 Config.SERVER_ALLOWED_SUBKEYS);
127 if (allowed.contains(subkey)) {
128 if ((subkey + "|").contains("|rw|")) {
129 rw = true;
130 }
131 if ((subkey + "|").contains("|wl|")) {
132 wl = false; // |wl| = bypass whitelist
2070ced5 133 }
b0e88ebd
NR
134 }
135 }
136
fb25273c
NR
137 String mode = display(wl, rw);
138
139 String trace = mode + "[ " + command + "] ";
085a2f9a 140 for (Object arg : args) {
b9ce9cad 141 trace += arg + " ";
085a2f9a 142 }
49f3dec5 143 System.out.println(trace);
b0e88ebd 144
fb25273c 145 Object rep = doRequest(action, command, args, rw, whitelist);
9f51d8ab 146
9b558341 147 commands.put(id, command);
fb25273c
NR
148 wls.put(id, wl);
149 rws.put(id, rw);
9b558341 150 times.put(id, (new Date().getTime() - start));
9f51d8ab
NR
151
152 return rep;
153 }
154
fb25273c
NR
155 private String display(boolean whitelist, boolean rw) {
156 String mode = "";
157 if (!rw) {
158 mode += "RO: ";
159 }
160 if (whitelist) {
161 mode += "WL: ";
162 }
163
164 return mode;
165 }
166
9b558341
NR
167 @Override
168 protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
fb25273c
NR
169 boolean whitelist = wls.get(id);
170 boolean rw = rws.get(id);
171 wls.remove(id);
172 rws.remove(id);
173
9b558341
NR
174 String rec = StringUtils.formatNumber(bytesReceived) + "b";
175 String sent = StringUtils.formatNumber(bytesSent) + "b";
fb25273c
NR
176 System.out.println(String.format("%s[>%s]: (%s sent, %s rec) in %d ms",
177 display(whitelist, rw), commands.get(id), sent, rec,
178 times.get(id)));
179
9b558341
NR
180 commands.remove(id);
181 times.remove(id);
182 }
183
9f51d8ab 184 private Object doRequest(ConnectActionServerObject action, String command,
fb25273c
NR
185 Object[] args, boolean rw, List<String> whitelist)
186 throws NoSuchFieldException, NoSuchMethodException,
9f51d8ab 187 ClassNotFoundException, IOException {
3bbc86a5 188 if ("PING".equals(command)) {
fb25273c 189 return rw ? "r/w" : "r/o";
3bbc86a5 190 } else if ("GET_METADATA".equals(command)) {
7efece85 191 if ("*".equals(args[0])) {
9b863b20 192 Progress pg = createPgForwarder(action);
9f51d8ab
NR
193
194 List<MetaData> metas = new ArrayList<MetaData>();
652fd9b0 195
9f51d8ab
NR
196 for (MetaData meta : Instance.getLibrary().getMetas(pg)) {
197 MetaData light;
198 if (meta.getCover() == null) {
199 light = meta;
200 } else {
201 light = meta.clone();
202 light.setCover(null);
203 }
204
205 metas.add(light);
206 }
207
9b863b20 208 forcePgDoneSent(pg);
a85e8077
NR
209 return metas.toArray(new MetaData[] {});
210 }
e272f05f 211
37abe20c
NR
212 return new MetaData[] { Instance.getLibrary().getInfo(
213 (String) args[0]) };
a85e8077 214 } else if ("GET_STORY".equals(command)) {
7efece85 215 MetaData meta = Instance.getLibrary().getInfo((String) args[0]);
b9ce9cad
NR
216 meta = meta.clone();
217 meta.setCover(null);
218
219 action.send(meta);
220 action.rec();
221
37abe20c
NR
222 Story story = Instance.getLibrary()
223 .getStory((String) args[0], null);
b9ce9cad
NR
224 for (Object obj : breakStory(story)) {
225 action.send(obj);
226 action.rec();
227 }
085a2f9a 228 } else if ("SAVE_STORY".equals(command)) {
b9ce9cad
NR
229 List<Object> list = new ArrayList<Object>();
230
231 action.send(null);
232 Object obj = action.rec();
233 while (obj != null) {
234 list.add(obj);
235 action.send(null);
236 obj = action.rec();
237 }
238
239 Story story = rebuildStory(list);
7efece85 240 Instance.getLibrary().save(story, (String) args[0], null);
0fa0fe95 241 return story.getMeta().getLuid();
edf79e5e 242 } else if ("IMPORT".equals(command)) {
9b863b20 243 Progress pg = createPgForwarder(action);
37abe20c
NR
244 Story story = Instance.getLibrary().imprt(
245 new URL((String) args[0]), pg);
9b863b20 246 forcePgDoneSent(pg);
edf79e5e 247 return story.getMeta().getLuid();
085a2f9a 248 } else if ("DELETE_STORY".equals(command)) {
7efece85 249 Instance.getLibrary().delete((String) args[0]);
e604986c 250 } else if ("GET_COVER".equals(command)) {
7efece85 251 return Instance.getLibrary().getCover((String) args[0]);
3989dfc5
NR
252 } else if ("GET_CUSTOM_COVER".equals(command)) {
253 if ("SOURCE".equals(args[0])) {
37abe20c
NR
254 return Instance.getLibrary().getCustomSourceCover(
255 (String) args[1]);
3989dfc5 256 } else if ("AUTHOR".equals(args[0])) {
37abe20c
NR
257 return Instance.getLibrary().getCustomAuthorCover(
258 (String) args[1]);
3989dfc5
NR
259 } else {
260 return null;
261 }
262 } else if ("SET_COVER".equals(command)) {
263 if ("SOURCE".equals(args[0])) {
264 Instance.getLibrary().setSourceCover((String) args[1],
265 (String) args[2]);
266 } else if ("AUTHOR".equals(args[0])) {
267 Instance.getLibrary().setAuthorCover((String) args[1],
268 (String) args[2]);
269 }
c8d48938 270 } else if ("CHANGE_STA".equals(command)) {
9b863b20 271 Progress pg = createPgForwarder(action);
c8d48938
NR
272 Instance.getLibrary().changeSTA((String) args[0], (String) args[1],
273 (String) args[2], (String) args[3], pg);
9b863b20 274 forcePgDoneSent(pg);
5e848e6a
NR
275 } else if ("EXIT".equals(command)) {
276 stop(0, false);
b0e88ebd
NR
277 }
278
279 return null;
280 }
74a40dfb 281
b9ce9cad
NR
282 @Override
283 protected void onError(Exception e) {
7e51d91c
NR
284 if (e instanceof SSLException) {
285 System.out.println("[Client connection refused (bad key)]");
286 } else {
287 getTraceHandler().error(e);
288 }
b9ce9cad 289 }
74a40dfb 290
b9ce9cad
NR
291 /**
292 * Break a story in multiple {@link Object}s for easier serialisation.
293 *
294 * @param story
295 * the {@link Story} to break
296 *
297 * @return the list of {@link Object}s
298 */
299 static List<Object> breakStory(Story story) {
300 List<Object> list = new ArrayList<Object>();
74a40dfb
NR
301
302 story = story.clone();
b9ce9cad 303 list.add(story);
74a40dfb 304
b9ce9cad
NR
305 if (story.getMeta().isImageDocument()) {
306 for (Chapter chap : story) {
307 list.add(chap);
308 list.addAll(chap.getParagraphs());
309 chap.setParagraphs(new ArrayList<Paragraph>());
74a40dfb 310 }
b9ce9cad 311 story.setChapters(new ArrayList<Chapter>());
74a40dfb 312 }
74a40dfb 313
b9ce9cad
NR
314 return list;
315 }
74a40dfb 316
b9ce9cad
NR
317 /**
318 * Rebuild a story from a list of broke up {@link Story} parts.
319 *
320 * @param list
321 * the list of {@link Story} parts
322 *
323 * @return the reconstructed {@link Story}
324 */
325 static Story rebuildStory(List<Object> list) {
74a40dfb 326 Story story = null;
b9ce9cad 327 Chapter chap = null;
74a40dfb 328
b9ce9cad
NR
329 for (Object obj : list) {
330 if (obj instanceof Story) {
331 story = (Story) obj;
332 } else if (obj instanceof Chapter) {
333 chap = (Chapter) obj;
334 story.getChapters().add(chap);
335 } else if (obj instanceof Paragraph) {
336 chap.getParagraphs().add((Paragraph) obj);
74a40dfb
NR
337 }
338 }
339
340 return story;
341 }
342
b9ce9cad
NR
343 /**
344 * Update the {@link Progress} with the adequate {@link Object} received
345 * from the network via {@link RemoteLibraryServer}.
346 *
347 * @param pg
348 * the {@link Progress} to update
349 * @param rep
350 * the object received from the network
351 *
352 * @return TRUE if it was a progress event, FALSE if not
353 */
354 static boolean updateProgress(Progress pg, Object rep) {
355 if (rep instanceof Integer[]) {
356 Integer[] a = (Integer[]) rep;
357 if (a.length == 3) {
358 int min = a[0];
359 int max = a[1];
360 int progress = a[2];
361
362 if (min >= 0 && min <= max) {
363 pg.setMinMax(min, max);
364 pg.setProgress(progress);
365
366 return true;
367 }
368 }
74a40dfb 369 }
b9ce9cad
NR
370
371 return false;
74a40dfb
NR
372 }
373
b9ce9cad
NR
374 /**
375 * Create a {@link Progress} that will forward its progress over the
376 * network.
377 *
378 * @param action
379 * the {@link ConnectActionServerObject} to use to forward it
380 *
381 * @return the {@link Progress}
382 */
49f3dec5 383 private Progress createPgForwarder(final ConnectActionServerObject action) {
9b863b20
NR
384 final Boolean[] isDoneForwarded = new Boolean[] { false };
385 final Progress pg = new Progress() {
386 @Override
387 public boolean isDone() {
388 return isDoneForwarded[0];
389 }
390 };
391
b9ce9cad 392 final Integer[] p = new Integer[] { -1, -1, -1 };
9b863b20 393 final Long[] lastTime = new Long[] { new Date().getTime() };
b9ce9cad
NR
394 pg.addProgressListener(new ProgressListener() {
395 @Override
396 public void progress(Progress progress, String name) {
397 int min = pg.getMin();
398 int max = pg.getMax();
37abe20c
NR
399 int relativeProgress = min
400 + (int) Math.round(pg.getRelativeProgress()
401 * (max - min));
b9ce9cad 402
9b863b20
NR
403 // Do not re-send the same value twice over the wire,
404 // unless more than 2 seconds have elapsed (to maintain the
405 // connection)
406 if ((p[0] != min || p[1] != max || p[2] != relativeProgress)
407 || (new Date().getTime() - lastTime[0] > 2000)) {
b9ce9cad
NR
408 p[0] = min;
409 p[1] = max;
410 p[2] = relativeProgress;
411
412 try {
37abe20c 413 action.send(new Integer[] { min, max, relativeProgress });
b9ce9cad
NR
414 action.rec();
415 } catch (Exception e) {
49f3dec5 416 getTraceHandler().error(e);
b9ce9cad 417 }
9b863b20 418
9b863b20 419 lastTime[0] = new Date().getTime();
b9ce9cad 420 }
652fd9b0
NR
421
422 isDoneForwarded[0] = (pg.getProgress() >= pg.getMax());
b9ce9cad
NR
423 }
424 });
425
426 return pg;
74a40dfb 427 }
9b863b20
NR
428
429 // with 30 seconds timeout
49f3dec5 430 private void forcePgDoneSent(Progress pg) {
9b863b20
NR
431 long start = new Date().getTime();
432 pg.done();
433 while (!pg.isDone() && new Date().getTime() - start < 30000) {
434 try {
435 Thread.sleep(100);
436 } catch (InterruptedException e) {
49f3dec5 437 getTraceHandler().error(e);
9b863b20
NR
438 }
439 }
440 }
b0e88ebd 441}