Revert "new yiffstar, part 1"
[nikiroo-utils.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 luid</li>
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
44 * command) with the given LUID, then return the LUID</li>
45 * <li>IMPORT [url]: save the story found at the given URL, then return the LUID
46 * </li>
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>
56 * </ul>
57 *
58 * @author niki
59 */
60 public class RemoteLibraryServer extends ServerObject {
61 private Map<Long, String> commands = new HashMap<Long, String>();
62 private Map<Long, Long> times = new HashMap<Long, Long>();
63 private Map<Long, Boolean> wls = new HashMap<Long, Boolean>();
64 private Map<Long, Boolean> rws = new HashMap<Long, Boolean>();
65
66 /**
67 * Create a new remote server (will not be active until
68 * {@link RemoteLibraryServer#start()} is called).
69 * <p>
70 * Note: the key we use here is the encryption key (it must not contain a
71 * subkey).
72 *
73 * @param key
74 * the key that will restrict access to this server
75 * @param port
76 * the port to listen on
77 *
78 * @throws IOException
79 * in case of I/O error
80 */
81 public RemoteLibraryServer(String key, int port) throws IOException {
82 super("Fanfix remote library", port, key);
83 setTraceHandler(Instance.getTraceHandler());
84 }
85
86 @Override
87 protected Object onRequest(ConnectActionServerObject action,
88 Version clientVersion, Object data, long id) throws Exception {
89 long start = new Date().getTime();
90
91 // defaults are positive (as previous versions without the feature)
92 boolean rw = true;
93 boolean wl = true;
94
95 String subkey = "";
96 String command = "";
97 Object[] args = new Object[0];
98 if (data instanceof Object[]) {
99 Object[] dataArray = (Object[]) data;
100 if (dataArray.length > 0) {
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 }
122
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
133 whitelist = new ArrayList<String>();
134 }
135 }
136 }
137
138 String mode = display(wl, rw);
139
140 String trace = mode + "[ " + command + "] ";
141 for (Object arg : args) {
142 trace += arg + " ";
143 }
144 long now = System.currentTimeMillis();
145 System.out.println(StringUtils.fromTime(now) + ": " + trace);
146
147 Object rep = null;
148 try {
149 rep = doRequest(action, command, args, rw, whitelist);
150 } catch (IOException e) {
151 rep = new RemoteLibraryException(e, true);
152 }
153
154 commands.put(id, command);
155 wls.put(id, wl);
156 rws.put(id, rw);
157 times.put(id, (new Date().getTime() - start));
158
159 return rep;
160 }
161
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
174 @Override
175 protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
176 boolean whitelist = wls.get(id);
177 boolean rw = rws.get(id);
178 wls.remove(id);
179 rws.remove(id);
180
181 String rec = StringUtils.formatNumber(bytesReceived) + "b";
182 String sent = StringUtils.formatNumber(bytesSent) + "b";
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)));
189
190 commands.remove(id);
191 times.remove(id);
192 }
193
194 private Object doRequest(ConnectActionServerObject action, String command,
195 Object[] args, boolean rw, List<String> whitelist)
196 throws NoSuchFieldException, NoSuchMethodException,
197 ClassNotFoundException, IOException {
198 if ("PING".equals(command)) {
199 return rw ? "r/w" : "r/o";
200 } else if ("GET_METADATA".equals(command)) {
201 List<MetaData> metas = new ArrayList<MetaData>();
202
203 if ("*".equals(args[0])) {
204 Progress pg = createPgForwarder(action);
205
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
218 forcePgDoneSent(pg);
219 } else {
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);
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 }
239 }
240
241 return metas.toArray(new MetaData[0]);
242 } else if ("GET_STORY".equals(command)) {
243 MetaData meta = Instance.getLibrary().getInfo((String) args[0]);
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
254 meta = meta.clone();
255 meta.setCover(null);
256
257 action.send(meta);
258 action.rec();
259
260 Story story = Instance.getLibrary()
261 .getStory((String) args[0], null);
262 for (Object obj : breakStory(story)) {
263 action.send(obj);
264 action.rec();
265 }
266 } else if ("SAVE_STORY".equals(command)) {
267 if (!rw) {
268 throw new RemoteLibraryException("Read-Only remote library: "
269 + args[0], false);
270 }
271
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);
283 Instance.getLibrary().save(story, (String) args[0], null);
284 return story.getMeta().getLuid();
285 } else if ("IMPORT".equals(command)) {
286 if (!rw) {
287 throw new RemoteLibraryException("Read-Only remote library: "
288 + args[0], false);
289 }
290
291 Progress pg = createPgForwarder(action);
292 Story story = Instance.getLibrary().imprt(
293 new URL((String) args[0]), pg);
294 forcePgDoneSent(pg);
295 return story.getMeta().getLuid();
296 } else if ("DELETE_STORY".equals(command)) {
297 if (!rw) {
298 throw new RemoteLibraryException("Read-Only remote library: "
299 + args[0], false);
300 }
301
302 Instance.getLibrary().delete((String) args[0]);
303 } else if ("GET_COVER".equals(command)) {
304 return Instance.getLibrary().getCover((String) args[0]);
305 } else if ("GET_CUSTOM_COVER".equals(command)) {
306 if ("SOURCE".equals(args[0])) {
307 return Instance.getLibrary().getCustomSourceCover(
308 (String) args[1]);
309 } else if ("AUTHOR".equals(args[0])) {
310 return Instance.getLibrary().getCustomAuthorCover(
311 (String) args[1]);
312 } else {
313 return null;
314 }
315 } else if ("SET_COVER".equals(command)) {
316 if (!rw) {
317 throw new RemoteLibraryException("Read-Only remote library: "
318 + args[0] + ", " + args[1], false);
319 }
320
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 }
328 } else if ("CHANGE_STA".equals(command)) {
329 if (!rw) {
330 throw new RemoteLibraryException("Read-Only remote library: "
331 + args[0] + ", " + args[1], false);
332 }
333
334 Progress pg = createPgForwarder(action);
335 Instance.getLibrary().changeSTA((String) args[0], (String) args[1],
336 (String) args[2], (String) args[3], pg);
337 forcePgDoneSent(pg);
338 } else if ("EXIT".equals(command)) {
339 if (!rw) {
340 throw new RemoteLibraryException(
341 "Read-Only remote library: EXIT", false);
342 }
343
344 stop(0, false);
345 }
346
347 return null;
348 }
349
350 @Override
351 protected void onError(Exception e) {
352 if (e instanceof SSLException) {
353 long now = System.currentTimeMillis();
354 System.out.println(StringUtils.fromTime(now) + ": "
355 + "[Client connection refused (bad key)]");
356 } else {
357 getTraceHandler().error(e);
358 }
359 }
360
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>();
371
372 story = story.clone();
373 list.add(story);
374
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>());
380 }
381 story.setChapters(new ArrayList<Chapter>());
382 }
383
384 return list;
385 }
386
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) {
396 Story story = null;
397 Chapter chap = null;
398
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);
407 }
408 }
409
410 return story;
411 }
412
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 }
439 }
440
441 return false;
442 }
443
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 */
453 private Progress createPgForwarder(final ConnectActionServerObject action) {
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
462 final Integer[] p = new Integer[] { -1, -1, -1 };
463 final Long[] lastTime = new Long[] { new Date().getTime() };
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();
469 int relativeProgress = min
470 + (int) Math.round(pg.getRelativeProgress()
471 * (max - min));
472
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)) {
478 p[0] = min;
479 p[1] = max;
480 p[2] = relativeProgress;
481
482 try {
483 action.send(new Integer[] { min, max, relativeProgress });
484 action.rec();
485 } catch (Exception e) {
486 getTraceHandler().error(e);
487 }
488
489 lastTime[0] = new Date().getTime();
490 }
491
492 isDoneForwarded[0] = (pg.getProgress() >= pg.getMax());
493 }
494 });
495
496 return pg;
497 }
498
499 // with 30 seconds timeout
500 private void forcePgDoneSent(Progress pg) {
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) {
507 getTraceHandler().error(e);
508 }
509 }
510 }
511 }