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