e54f105d6bf9233c4f1c4383c948630b57b79555
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibraryServer.java
1 package be.nikiroo.fanfix.library;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.net.ssl.SSLException;
12
13 import be.nikiroo.fanfix.Instance;
14 import be.nikiroo.fanfix.bundles.Config;
15 import be.nikiroo.fanfix.data.Chapter;
16 import be.nikiroo.fanfix.data.MetaData;
17 import be.nikiroo.fanfix.data.Paragraph;
18 import be.nikiroo.fanfix.data.Story;
19 import be.nikiroo.utils.Progress;
20 import be.nikiroo.utils.Progress.ProgressListener;
21 import be.nikiroo.utils.StringUtils;
22 import be.nikiroo.utils.Version;
23 import be.nikiroo.utils.serial.server.ConnectActionServerObject;
24 import be.nikiroo.utils.serial.server.ServerObject;
25
26 /**
27 * Create a new remote server that will listen for orders on the given port.
28 * <p>
29 * The available commands are given as arrays of objects (first item is the
30 * command, the rest are the arguments).
31 * <p>
32 * All the commands are always prefixed by the subkey (which can be EMPTY if
33 * none).
34 * <p>
35 * <ul>
36 * <li>PING: will return the mode if the key is accepted (mode can be: "r/o" or
37 * "r/w")</li>
38 * <li>GET_METADATA *: will return the metadata of all the stories in the
39 * library (array)</li> *
40 * <li>GET_METADATA [luid]: will return the metadata of the story of LUID
41 * luid</li>
42 * <li>GET_STORY [luid]: will return the given story if it exists (or NULL if
43 * not)</li>
44 * <li>SAVE_STORY [luid]: save the story (that must be sent just after the
45 * command) with the given LUID, then return the LUID</li>
46 * <li>IMPORT [url]: save the story found at the given URL, then return the LUID
47 * </li>
48 * <li>DELETE_STORY [luid]: delete the story of LUID luid</li>
49 * <li>GET_COVER [luid]: return the cover of the story</li>
50 * <li>GET_CUSTOM_COVER ["SOURCE"|"AUTHOR"] [source]: return the cover for this
51 * source/author</li>
52 * <li>SET_COVER ["SOURCE"|"AUTHOR"] [value] [luid]: set the default cover for
53 * the given source/author to the cover of the story denoted by luid</li>
54 * <li>CHANGE_SOURCE [luid] [new source]: change the source of the story of LUID
55 * luid</li>
56 * <li>EXIT: stop the server</li>
57 * </ul>
58 *
59 * @author niki
60 */
61 public class RemoteLibraryServer extends ServerObject {
62 private Map<Long, String> commands = new HashMap<Long, String>();
63 private Map<Long, Long> times = new HashMap<Long, Long>();
64 private Map<Long, Boolean> wls = new HashMap<Long, Boolean>();
65 private Map<Long, Boolean> bls = new HashMap<Long, Boolean>();
66 private Map<Long, Boolean> rws = new HashMap<Long, Boolean>();
67
68 /**
69 * Create a new remote server (will not be active until
70 * {@link RemoteLibraryServer#start()} is called).
71 * <p>
72 * Note: the key we use here is the encryption key (it must not contain a
73 * subkey).
74 *
75 * @throws IOException
76 * in case of I/O error
77 */
78 public RemoteLibraryServer() throws IOException {
79 super("Fanfix remote library",
80 Instance.getInstance().getConfig()
81 .getInteger(Config.SERVER_PORT),
82 Instance.getInstance().getConfig()
83 .getString(Config.SERVER_KEY));
84
85 setTraceHandler(Instance.getInstance().getTraceHandler());
86 }
87
88 @Override
89 protected Object onRequest(ConnectActionServerObject action,
90 Version clientVersion, Object data, long id) throws Exception {
91 long start = new Date().getTime();
92
93 // defaults are positive (as previous versions without the feature)
94 boolean rw = true;
95 boolean wl = true;
96 boolean bl = true;
97
98 String subkey = "";
99 String command = "";
100 Object[] args = new Object[0];
101 if (data instanceof Object[]) {
102 Object[] dataArray = (Object[]) data;
103 if (dataArray.length > 0) {
104 subkey = "" + dataArray[0];
105 }
106 if (dataArray.length > 1) {
107 command = "" + dataArray[1];
108
109 args = new Object[dataArray.length - 2];
110 for (int i = 2; i < dataArray.length; i++) {
111 args[i - 2] = dataArray[i];
112 }
113 }
114 }
115
116 List<String> whitelist = Instance.getInstance().getConfig()
117 .getList(Config.SERVER_WHITELIST);
118 if (whitelist == null) {
119 whitelist = new ArrayList<String>();
120 }
121 List<String> blacklist = Instance.getInstance().getConfig()
122 .getList(Config.SERVER_BLACKLIST);
123 if (blacklist == null) {
124 blacklist = new ArrayList<String>();
125 }
126
127 if (whitelist.isEmpty()) {
128 wl = false;
129 }
130
131 rw = Instance.getInstance().getConfig().getBoolean(Config.SERVER_RW,
132 rw);
133 if (!subkey.isEmpty()) {
134 List<String> allowed = Instance.getInstance().getConfig()
135 .getList(Config.SERVER_ALLOWED_SUBKEYS);
136 if (allowed.contains(subkey)) {
137 if ((subkey + "|").contains("|rw|")) {
138 rw = true;
139 }
140 if ((subkey + "|").contains("|wl|")) {
141 wl = false; // |wl| = bypass whitelist
142 whitelist = new ArrayList<String>();
143 }
144 if ((subkey + "|").contains("|bl|")) {
145 bl = false; // |bl| = bypass blacklist
146 blacklist = new ArrayList<String>();
147 }
148 }
149 }
150
151 String mode = display(wl, bl, rw);
152
153 String trace = mode + "[ " + command + "] ";
154 for (Object arg : args) {
155 trace += arg + " ";
156 }
157 long now = System.currentTimeMillis();
158 System.out.println(StringUtils.fromTime(now) + ": " + trace);
159
160 Object rep = null;
161 try {
162 rep = doRequest(action, command, args, rw, whitelist, blacklist);
163 } catch (IOException e) {
164 rep = new RemoteLibraryException(e, true);
165 }
166
167 commands.put(id, command);
168 wls.put(id, wl);
169 bls.put(id, bl);
170 rws.put(id, rw);
171 times.put(id, (new Date().getTime() - start));
172
173 return rep;
174 }
175
176 private String display(boolean whitelist, boolean blacklist, boolean rw) {
177 String mode = "";
178 if (!rw) {
179 mode += "RO: ";
180 }
181 if (whitelist) {
182 mode += "WL: ";
183 }
184 if (blacklist) {
185 mode += "BL: ";
186 }
187
188 return mode;
189 }
190
191 @Override
192 protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
193 boolean whitelist = wls.get(id);
194 boolean blacklist = bls.get(id);
195 boolean rw = rws.get(id);
196 wls.remove(id);
197 bls.remove(id);
198 rws.remove(id);
199
200 String rec = StringUtils.formatNumber(bytesReceived) + "b";
201 String sent = StringUtils.formatNumber(bytesSent) + "b";
202 long now = System.currentTimeMillis();
203 System.out.println(StringUtils.fromTime(now) + ": "
204 + String.format("%s[>%s]: (%s sent, %s rec) in %d ms",
205 display(whitelist, blacklist, rw), commands.get(id),
206 sent, rec, times.get(id)));
207
208 commands.remove(id);
209 times.remove(id);
210 }
211
212 private Object doRequest(ConnectActionServerObject action, String command,
213 Object[] args, boolean rw, List<String> whitelist,
214 List<String> blacklist) throws NoSuchFieldException,
215 NoSuchMethodException, ClassNotFoundException, IOException {
216 if ("PING".equals(command)) {
217 return rw ? "r/w" : "r/o";
218 } else if ("GET_METADATA".equals(command)) {
219 List<MetaData> metas = new ArrayList<MetaData>();
220
221 if ("*".equals(args[0])) {
222 Progress pg = createPgForwarder(action);
223
224 for (MetaData meta : Instance.getInstance().getLibrary()
225 .getMetas(pg)) {
226 metas.add(removeCover(meta));
227 }
228
229 forcePgDoneSent(pg);
230 } else {
231 MetaData meta = Instance.getInstance().getLibrary()
232 .getInfo((String) args[0]);
233 MetaData light;
234 if (meta.getCover() == null) {
235 light = meta;
236 } else {
237 light = meta.clone();
238 light.setCover(null);
239 }
240
241 metas.add(light);
242 }
243
244 for (int i = 0; i < metas.size(); i++) {
245 if (!isAllowed(metas.get(i), whitelist, blacklist)) {
246 metas.remove(i);
247 i--;
248 }
249 }
250
251 return metas.toArray(new MetaData[0]);
252
253 } else if ("GET_STORY".equals(command)) {
254 MetaData meta = Instance.getInstance().getLibrary()
255 .getInfo((String) args[0]);
256 if (meta == null || !isAllowed(meta, whitelist, blacklist)) {
257 return null;
258 }
259
260 meta = meta.clone();
261 meta.setCover(null);
262
263 action.send(meta);
264 action.rec();
265
266 Story story = Instance.getInstance().getLibrary()
267 .getStory((String) args[0], null);
268 for (Object obj : breakStory(story)) {
269 action.send(obj);
270 action.rec();
271 }
272 } else if ("SAVE_STORY".equals(command)) {
273 if (!rw) {
274 throw new RemoteLibraryException(
275 "Read-Only remote library: " + args[0], false);
276 }
277
278 List<Object> list = new ArrayList<Object>();
279
280 action.send(null);
281 Object obj = action.rec();
282 while (obj != null) {
283 list.add(obj);
284 action.send(null);
285 obj = action.rec();
286 }
287
288 Story story = rebuildStory(list);
289 Instance.getInstance().getLibrary().save(story, (String) args[0],
290 null);
291 return story.getMeta().getLuid();
292 } else if ("IMPORT".equals(command)) {
293 if (!rw) {
294 throw new RemoteLibraryException(
295 "Read-Only remote library: " + args[0], false);
296 }
297
298 Progress pg = createPgForwarder(action);
299 MetaData meta = Instance.getInstance().getLibrary()
300 .imprt(new URL((String) args[0]), pg);
301 forcePgDoneSent(pg);
302 return meta.getLuid();
303 } else if ("DELETE_STORY".equals(command)) {
304 if (!rw) {
305 throw new RemoteLibraryException(
306 "Read-Only remote library: " + args[0], false);
307 }
308
309 Instance.getInstance().getLibrary().delete((String) args[0]);
310 } else if ("GET_COVER".equals(command)) {
311 return Instance.getInstance().getLibrary()
312 .getCover((String) args[0]);
313 } else if ("GET_CUSTOM_COVER".equals(command)) {
314 if ("SOURCE".equals(args[0])) {
315 return Instance.getInstance().getLibrary()
316 .getCustomSourceCover((String) args[1]);
317 } else if ("AUTHOR".equals(args[0])) {
318 return Instance.getInstance().getLibrary()
319 .getCustomAuthorCover((String) args[1]);
320 } else {
321 return null;
322 }
323 } else if ("SET_COVER".equals(command)) {
324 if (!rw) {
325 throw new RemoteLibraryException(
326 "Read-Only remote library: " + args[0] + ", " + args[1],
327 false);
328 }
329
330 if ("SOURCE".equals(args[0])) {
331 Instance.getInstance().getLibrary()
332 .setSourceCover((String) args[1], (String) args[2]);
333 } else if ("AUTHOR".equals(args[0])) {
334 Instance.getInstance().getLibrary()
335 .setAuthorCover((String) args[1], (String) args[2]);
336 }
337 } else if ("CHANGE_STA".equals(command)) {
338 if (!rw) {
339 throw new RemoteLibraryException(
340 "Read-Only remote library: " + args[0] + ", " + args[1],
341 false);
342 }
343
344 Progress pg = createPgForwarder(action);
345 Instance.getInstance().getLibrary().changeSTA((String) args[0],
346 (String) args[1], (String) args[2], (String) args[3], pg);
347 forcePgDoneSent(pg);
348 } else if ("EXIT".equals(command)) {
349 if (!rw) {
350 throw new RemoteLibraryException(
351 "Read-Only remote library: EXIT", false);
352 }
353
354 stop(10000, false);
355 }
356
357 return null;
358 }
359
360 @Override
361 protected void onError(Exception e) {
362 if (e instanceof SSLException) {
363 long now = System.currentTimeMillis();
364 System.out.println(StringUtils.fromTime(now) + ": "
365 + "[Client connection refused (bad key)]");
366 } else {
367 getTraceHandler().error(e);
368 }
369 }
370
371 /**
372 * Break a story in multiple {@link Object}s for easier serialisation.
373 *
374 * @param story
375 * the {@link Story} to break
376 *
377 * @return the list of {@link Object}s
378 */
379 static List<Object> breakStory(Story story) {
380 List<Object> list = new ArrayList<Object>();
381
382 story = story.clone();
383 list.add(story);
384
385 if (story.getMeta().isImageDocument()) {
386 for (Chapter chap : story) {
387 list.add(chap);
388 list.addAll(chap.getParagraphs());
389 chap.setParagraphs(new ArrayList<Paragraph>());
390 }
391 story.setChapters(new ArrayList<Chapter>());
392 }
393
394 return list;
395 }
396
397 /**
398 * Rebuild a story from a list of broke up {@link Story} parts.
399 *
400 * @param list
401 * the list of {@link Story} parts
402 *
403 * @return the reconstructed {@link Story}
404 */
405 static Story rebuildStory(List<Object> list) {
406 Story story = null;
407 Chapter chap = null;
408
409 for (Object obj : list) {
410 if (obj instanceof Story) {
411 story = (Story) obj;
412 } else if (obj instanceof Chapter) {
413 chap = (Chapter) obj;
414 story.getChapters().add(chap);
415 } else if (obj instanceof Paragraph) {
416 chap.getParagraphs().add((Paragraph) obj);
417 }
418 }
419
420 return story;
421 }
422
423 /**
424 * Update the {@link Progress} with the adequate {@link Object} received
425 * from the network via {@link RemoteLibraryServer}.
426 *
427 * @param pg
428 * the {@link Progress} to update
429 * @param rep
430 * the object received from the network
431 *
432 * @return TRUE if it was a progress event, FALSE if not
433 */
434 static boolean updateProgress(Progress pg, Object rep) {
435 boolean updateProgress = false;
436 if (rep instanceof Integer[] && ((Integer[]) rep).length == 3)
437 updateProgress = true;
438 if (rep instanceof Object[] && ((Object[]) rep).length >= 5
439 && "UPDATE".equals(((Object[]) rep)[0]))
440 updateProgress = true;
441
442 if (updateProgress) {
443 Object[] a = (Object[]) rep;
444
445 int offset = 0;
446 if (a[0] instanceof String) {
447 offset = 1;
448 }
449
450 int min = (Integer) a[0 + offset];
451 int max = (Integer) a[1 + offset];
452 int progress = (Integer) a[2 + offset];
453
454 Object meta = null;
455 if (a.length > (3 + offset)) {
456 meta = a[3 + offset];
457 }
458
459 String name = null;
460 if (a.length > (4 + offset)) {
461 name = a[4 + offset] == null ? "" : a[4 + offset].toString();
462 }
463
464 if (min >= 0 && min <= max) {
465 pg.setName(name);
466 pg.setMinMax(min, max);
467 pg.setProgress(progress);
468 if (meta != null) {
469 pg.put("meta", meta);
470 }
471
472 return true;
473 }
474 }
475
476 return false;
477 }
478
479 /**
480 * Create a {@link Progress} that will forward its progress over the
481 * network.
482 *
483 * @param action
484 * the {@link ConnectActionServerObject} to use to forward it
485 *
486 * @return the {@link Progress}
487 */
488 private Progress createPgForwarder(final ConnectActionServerObject action) {
489 final Boolean[] isDoneForwarded = new Boolean[] { false };
490 final Progress pg = new Progress() {
491 @Override
492 public boolean isDone() {
493 return isDoneForwarded[0];
494 }
495 };
496
497 final Integer[] p = new Integer[] { -1, -1, -1 };
498 final Object[] pMeta = new MetaData[1];
499 final String[] pName = new String[1];
500 final Long[] lastTime = new Long[] { new Date().getTime() };
501 pg.addProgressListener(new ProgressListener() {
502 @Override
503 public void progress(Progress progress, String name) {
504 Object meta = pg.get("meta");
505 if (meta instanceof MetaData) {
506 meta = removeCover((MetaData) meta);
507 }
508
509 int min = pg.getMin();
510 int max = pg.getMax();
511 int rel = min + (int) Math
512 .round(pg.getRelativeProgress() * (max - min));
513
514 boolean samePg = p[0] == min && p[1] == max && p[2] == rel;
515
516 // Do not re-send the same value twice over the wire,
517 // unless more than 2 seconds have elapsed (to maintain the
518 // connection)
519 if (!samePg || !same(pMeta[0], meta) || !same(pName[0], name) //
520 || (new Date().getTime() - lastTime[0] > 2000)) {
521 p[0] = min;
522 p[1] = max;
523 p[2] = rel;
524 pMeta[0] = meta;
525 pName[0] = name;
526
527 try {
528 action.send(new Object[] { "UPDATE", min, max, rel,
529 meta, name });
530 action.rec();
531 } catch (Exception e) {
532 getTraceHandler().error(e);
533 }
534
535 lastTime[0] = new Date().getTime();
536 }
537
538 isDoneForwarded[0] = (pg.getProgress() >= pg.getMax());
539 }
540 });
541
542 return pg;
543 }
544
545 private boolean same(Object obj1, Object obj2) {
546 if (obj1 == null || obj2 == null)
547 return obj1 == null && obj2 == null;
548
549 return obj1.equals(obj2);
550 }
551
552 // with 30 seconds timeout
553 private void forcePgDoneSent(Progress pg) {
554 long start = new Date().getTime();
555 pg.done();
556 while (!pg.isDone() && new Date().getTime() - start < 30000) {
557 try {
558 Thread.sleep(100);
559 } catch (InterruptedException e) {
560 getTraceHandler().error(e);
561 }
562 }
563 }
564
565 private MetaData removeCover(MetaData meta) {
566 MetaData light = null;
567 if (meta != null) {
568 if (meta.getCover() == null) {
569 light = meta;
570 } else {
571 light = meta.clone();
572 light.setCover(null);
573 }
574 }
575
576 return light;
577 }
578
579 private boolean isAllowed(MetaData meta, List<String> whitelist,
580 List<String> blacklist) {
581 if (!whitelist.isEmpty() && !whitelist.contains(meta.getSource())) {
582 return false;
583 }
584
585 if (blacklist.contains(meta.getSource())) {
586 return false;
587 }
588
589 return true;
590 }
591 }