jdoc fix
[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
651072f3 133 whitelist = new ArrayList<String>();
2070ced5 134 }
b0e88ebd
NR
135 }
136 }
137
fb25273c
NR
138 String mode = display(wl, rw);
139
140 String trace = mode + "[ " + command + "] ";
085a2f9a 141 for (Object arg : args) {
b9ce9cad 142 trace += arg + " ";
085a2f9a 143 }
c1bb921e
NR
144 long now = System.currentTimeMillis();
145 System.out.println(StringUtils.fromTime(now) + ": " + trace);
b0e88ebd 146
c1b31971 147 Object rep = null;
c1b31971
NR
148 try {
149 rep = doRequest(action, command, args, rw, whitelist);
5db598bc 150 } catch (IOException e) {
533dc2b8 151 rep = new RemoteLibraryException(e, true);
c1b31971 152 }
9f51d8ab 153
9b558341 154 commands.put(id, command);
fb25273c
NR
155 wls.put(id, wl);
156 rws.put(id, rw);
9b558341 157 times.put(id, (new Date().getTime() - start));
9f51d8ab
NR
158
159 return rep;
160 }
161
fb25273c
NR
162 private String display(boolean whitelist, boolean rw) {
163 String mode = "";
164 if (!rw) {
165 mode += "RO: ";
166 }
167 if (whitelist) {
168 mode += "WL: ";
169 }
170
171 return mode;
172 }
173
9b558341
NR
174 @Override
175 protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
fb25273c
NR
176 boolean whitelist = wls.get(id);
177 boolean rw = rws.get(id);
178 wls.remove(id);
179 rws.remove(id);
180
9b558341
NR
181 String rec = StringUtils.formatNumber(bytesReceived) + "b";
182 String sent = StringUtils.formatNumber(bytesSent) + "b";
c1bb921e
NR
183 long now = System.currentTimeMillis();
184 System.out.println(StringUtils.fromTime(now)
185 + ": "
186 + String.format("%s[>%s]: (%s sent, %s rec) in %d ms",
187 display(whitelist, rw), commands.get(id), sent, rec,
188 times.get(id)));
fb25273c 189
9b558341
NR
190 commands.remove(id);
191 times.remove(id);
192 }
193
9f51d8ab 194 private Object doRequest(ConnectActionServerObject action, String command,
fb25273c
NR
195 Object[] args, boolean rw, List<String> whitelist)
196 throws NoSuchFieldException, NoSuchMethodException,
9f51d8ab 197 ClassNotFoundException, IOException {
3bbc86a5 198 if ("PING".equals(command)) {
fb25273c 199 return rw ? "r/w" : "r/o";
3bbc86a5 200 } else if ("GET_METADATA".equals(command)) {
651072f3
NR
201 List<MetaData> metas = new ArrayList<MetaData>();
202
7efece85 203 if ("*".equals(args[0])) {
9b863b20 204 Progress pg = createPgForwarder(action);
9f51d8ab 205
9f51d8ab
NR
206 for (MetaData meta : Instance.getLibrary().getMetas(pg)) {
207 MetaData light;
208 if (meta.getCover() == null) {
209 light = meta;
210 } else {
211 light = meta.clone();
212 light.setCover(null);
213 }
214
215 metas.add(light);
216 }
217
9b863b20 218 forcePgDoneSent(pg);
651072f3 219 } else {
210465c3
NR
220 MetaData meta = Instance.getLibrary().getInfo((String) args[0]);
221 MetaData light;
222 if (meta.getCover() == null) {
223 light = meta;
224 } else {
225 light = meta.clone();
226 light.setCover(null);
227 }
228
229 metas.add(light);
651072f3
NR
230 }
231
232 if (!whitelist.isEmpty()) {
233 for (int i = 0; i < metas.size(); i++) {
234 if (!whitelist.contains(metas.get(i).getSource())) {
235 metas.remove(i);
236 i--;
237 }
238 }
a85e8077 239 }
e272f05f 240
651072f3 241 return metas.toArray(new MetaData[0]);
a85e8077 242 } else if ("GET_STORY".equals(command)) {
7efece85 243 MetaData meta = Instance.getLibrary().getInfo((String) args[0]);
651072f3
NR
244 if (meta == null) {
245 return null;
246 }
247
248 if (!whitelist.isEmpty()) {
249 if (!whitelist.contains(meta.getSource())) {
250 return null;
251 }
252 }
253
b9ce9cad
NR
254 meta = meta.clone();
255 meta.setCover(null);
256
257 action.send(meta);
258 action.rec();
259
37abe20c
NR
260 Story story = Instance.getLibrary()
261 .getStory((String) args[0], null);
b9ce9cad
NR
262 for (Object obj : breakStory(story)) {
263 action.send(obj);
264 action.rec();
265 }
085a2f9a 266 } else if ("SAVE_STORY".equals(command)) {
651072f3 267 if (!rw) {
533dc2b8
NR
268 throw new RemoteLibraryException("Read-Only remote library: "
269 + args[0], false);
651072f3
NR
270 }
271
b9ce9cad
NR
272 List<Object> list = new ArrayList<Object>();
273
274 action.send(null);
275 Object obj = action.rec();
276 while (obj != null) {
277 list.add(obj);
278 action.send(null);
279 obj = action.rec();
280 }
281
282 Story story = rebuildStory(list);
7efece85 283 Instance.getLibrary().save(story, (String) args[0], null);
0fa0fe95 284 return story.getMeta().getLuid();
edf79e5e 285 } else if ("IMPORT".equals(command)) {
651072f3 286 if (!rw) {
533dc2b8
NR
287 throw new RemoteLibraryException("Read-Only remote library: "
288 + args[0], false);
651072f3
NR
289 }
290
9b863b20 291 Progress pg = createPgForwarder(action);
b6b65795 292 MetaData meta = Instance.getLibrary().imprt(
37abe20c 293 new URL((String) args[0]), pg);
9b863b20 294 forcePgDoneSent(pg);
b6b65795 295 return meta.getLuid();
085a2f9a 296 } else if ("DELETE_STORY".equals(command)) {
651072f3 297 if (!rw) {
533dc2b8
NR
298 throw new RemoteLibraryException("Read-Only remote library: "
299 + args[0], false);
651072f3
NR
300 }
301
7efece85 302 Instance.getLibrary().delete((String) args[0]);
e604986c 303 } else if ("GET_COVER".equals(command)) {
7efece85 304 return Instance.getLibrary().getCover((String) args[0]);
3989dfc5
NR
305 } else if ("GET_CUSTOM_COVER".equals(command)) {
306 if ("SOURCE".equals(args[0])) {
37abe20c
NR
307 return Instance.getLibrary().getCustomSourceCover(
308 (String) args[1]);
3989dfc5 309 } else if ("AUTHOR".equals(args[0])) {
37abe20c
NR
310 return Instance.getLibrary().getCustomAuthorCover(
311 (String) args[1]);
3989dfc5
NR
312 } else {
313 return null;
314 }
315 } else if ("SET_COVER".equals(command)) {
651072f3 316 if (!rw) {
533dc2b8
NR
317 throw new RemoteLibraryException("Read-Only remote library: "
318 + args[0] + ", " + args[1], false);
651072f3
NR
319 }
320
3989dfc5
NR
321 if ("SOURCE".equals(args[0])) {
322 Instance.getLibrary().setSourceCover((String) args[1],
323 (String) args[2]);
324 } else if ("AUTHOR".equals(args[0])) {
325 Instance.getLibrary().setAuthorCover((String) args[1],
326 (String) args[2]);
327 }
c8d48938 328 } else if ("CHANGE_STA".equals(command)) {
651072f3 329 if (!rw) {
533dc2b8
NR
330 throw new RemoteLibraryException("Read-Only remote library: "
331 + args[0] + ", " + args[1], false);
651072f3
NR
332 }
333
9b863b20 334 Progress pg = createPgForwarder(action);
c8d48938
NR
335 Instance.getLibrary().changeSTA((String) args[0], (String) args[1],
336 (String) args[2], (String) args[3], pg);
9b863b20 337 forcePgDoneSent(pg);
5e848e6a 338 } else if ("EXIT".equals(command)) {
651072f3 339 if (!rw) {
533dc2b8
NR
340 throw new RemoteLibraryException(
341 "Read-Only remote library: EXIT", false);
651072f3
NR
342 }
343
5e848e6a 344 stop(0, false);
b0e88ebd
NR
345 }
346
347 return null;
348 }
74a40dfb 349
b9ce9cad
NR
350 @Override
351 protected void onError(Exception e) {
7e51d91c 352 if (e instanceof SSLException) {
c1bb921e
NR
353 long now = System.currentTimeMillis();
354 System.out.println(StringUtils.fromTime(now) + ": "
355 + "[Client connection refused (bad key)]");
7e51d91c
NR
356 } else {
357 getTraceHandler().error(e);
358 }
b9ce9cad 359 }
74a40dfb 360
b9ce9cad
NR
361 /**
362 * Break a story in multiple {@link Object}s for easier serialisation.
363 *
364 * @param story
365 * the {@link Story} to break
366 *
367 * @return the list of {@link Object}s
368 */
369 static List<Object> breakStory(Story story) {
370 List<Object> list = new ArrayList<Object>();
74a40dfb
NR
371
372 story = story.clone();
b9ce9cad 373 list.add(story);
74a40dfb 374
b9ce9cad
NR
375 if (story.getMeta().isImageDocument()) {
376 for (Chapter chap : story) {
377 list.add(chap);
378 list.addAll(chap.getParagraphs());
379 chap.setParagraphs(new ArrayList<Paragraph>());
74a40dfb 380 }
b9ce9cad 381 story.setChapters(new ArrayList<Chapter>());
74a40dfb 382 }
74a40dfb 383
b9ce9cad
NR
384 return list;
385 }
74a40dfb 386
b9ce9cad
NR
387 /**
388 * Rebuild a story from a list of broke up {@link Story} parts.
389 *
390 * @param list
391 * the list of {@link Story} parts
392 *
393 * @return the reconstructed {@link Story}
394 */
395 static Story rebuildStory(List<Object> list) {
74a40dfb 396 Story story = null;
b9ce9cad 397 Chapter chap = null;
74a40dfb 398
b9ce9cad
NR
399 for (Object obj : list) {
400 if (obj instanceof Story) {
401 story = (Story) obj;
402 } else if (obj instanceof Chapter) {
403 chap = (Chapter) obj;
404 story.getChapters().add(chap);
405 } else if (obj instanceof Paragraph) {
406 chap.getParagraphs().add((Paragraph) obj);
74a40dfb
NR
407 }
408 }
409
410 return story;
411 }
412
b9ce9cad
NR
413 /**
414 * Update the {@link Progress} with the adequate {@link Object} received
415 * from the network via {@link RemoteLibraryServer}.
416 *
417 * @param pg
418 * the {@link Progress} to update
419 * @param rep
420 * the object received from the network
421 *
422 * @return TRUE if it was a progress event, FALSE if not
423 */
424 static boolean updateProgress(Progress pg, Object rep) {
425 if (rep instanceof Integer[]) {
426 Integer[] a = (Integer[]) rep;
427 if (a.length == 3) {
428 int min = a[0];
429 int max = a[1];
430 int progress = a[2];
431
432 if (min >= 0 && min <= max) {
433 pg.setMinMax(min, max);
434 pg.setProgress(progress);
435
436 return true;
437 }
438 }
74a40dfb 439 }
b9ce9cad
NR
440
441 return false;
74a40dfb
NR
442 }
443
b9ce9cad
NR
444 /**
445 * Create a {@link Progress} that will forward its progress over the
446 * network.
447 *
448 * @param action
449 * the {@link ConnectActionServerObject} to use to forward it
450 *
451 * @return the {@link Progress}
452 */
49f3dec5 453 private Progress createPgForwarder(final ConnectActionServerObject action) {
9b863b20
NR
454 final Boolean[] isDoneForwarded = new Boolean[] { false };
455 final Progress pg = new Progress() {
456 @Override
457 public boolean isDone() {
458 return isDoneForwarded[0];
459 }
460 };
461
b9ce9cad 462 final Integer[] p = new Integer[] { -1, -1, -1 };
9b863b20 463 final Long[] lastTime = new Long[] { new Date().getTime() };
b9ce9cad
NR
464 pg.addProgressListener(new ProgressListener() {
465 @Override
466 public void progress(Progress progress, String name) {
467 int min = pg.getMin();
468 int max = pg.getMax();
37abe20c
NR
469 int relativeProgress = min
470 + (int) Math.round(pg.getRelativeProgress()
471 * (max - min));
b9ce9cad 472
9b863b20
NR
473 // Do not re-send the same value twice over the wire,
474 // unless more than 2 seconds have elapsed (to maintain the
475 // connection)
476 if ((p[0] != min || p[1] != max || p[2] != relativeProgress)
477 || (new Date().getTime() - lastTime[0] > 2000)) {
b9ce9cad
NR
478 p[0] = min;
479 p[1] = max;
480 p[2] = relativeProgress;
481
482 try {
37abe20c 483 action.send(new Integer[] { min, max, relativeProgress });
b9ce9cad
NR
484 action.rec();
485 } catch (Exception e) {
49f3dec5 486 getTraceHandler().error(e);
b9ce9cad 487 }
9b863b20 488
9b863b20 489 lastTime[0] = new Date().getTime();
b9ce9cad 490 }
652fd9b0
NR
491
492 isDoneForwarded[0] = (pg.getProgress() >= pg.getMax());
b9ce9cad
NR
493 }
494 });
495
496 return pg;
74a40dfb 497 }
9b863b20
NR
498
499 // with 30 seconds timeout
49f3dec5 500 private void forcePgDoneSent(Progress pg) {
9b863b20
NR
501 long start = new Date().getTime();
502 pg.done();
503 while (!pg.isDone() && new Date().getTime() - start < 30000) {
504 try {
505 Thread.sleep(100);
506 } catch (InterruptedException e) {
49f3dec5 507 getTraceHandler().error(e);
9b863b20
NR
508 }
509 }
510 }
b0e88ebd 511}