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