1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
7 import java
.util
.HashMap
;
11 import javax
.net
.ssl
.SSLException
;
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
;
27 * Create a new remote server that will listen for orders on the given port.
29 * The available commands are given as arrays of objects (first item is the
30 * command, the rest are the arguments).
32 * All the commands are always prefixed by the subkey (which can be EMPTY if
36 * <li>PING: will return the mode if the key is accepted (mode can be: "r/o" or
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
42 * <li>GET_STORY [luid]: will return the given story if it exists (or NULL if
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
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
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
56 * <li>EXIT: stop the server</li>
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
>();
69 * Create a new remote server (will not be active until
70 * {@link RemoteLibraryServer#start()} is called).
72 * Note: the key we use here is the encryption key (it must not contain a
76 * in case of I/O error
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
));
85 setTraceHandler(Instance
.getInstance().getTraceHandler());
89 protected Object
onRequest(ConnectActionServerObject action
,
90 Version clientVersion
, Object data
, long id
) throws Exception
{
91 long start
= new Date().getTime();
93 // defaults are positive (as previous versions without the feature)
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];
106 if (dataArray
.length
> 1) {
107 command
= "" + dataArray
[1];
109 args
= new Object
[dataArray
.length
- 2];
110 for (int i
= 2; i
< dataArray
.length
; i
++) {
111 args
[i
- 2] = dataArray
[i
];
116 List
<String
> whitelist
= Instance
.getInstance().getConfig()
117 .getList(Config
.SERVER_WHITELIST
);
118 if (whitelist
== null) {
119 whitelist
= new ArrayList
<String
>();
121 List
<String
> blacklist
= Instance
.getInstance().getConfig()
122 .getList(Config
.SERVER_BLACKLIST
);
123 if (blacklist
== null) {
124 blacklist
= new ArrayList
<String
>();
127 if (whitelist
.isEmpty()) {
131 rw
= Instance
.getInstance().getConfig().getBoolean(Config
.SERVER_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|")) {
140 if ((subkey
+ "|").contains("|wl|")) {
141 wl
= false; // |wl| = bypass whitelist
142 whitelist
= new ArrayList
<String
>();
144 if ((subkey
+ "|").contains("|bl|")) {
145 bl
= false; // |bl| = bypass blacklist
146 blacklist
= new ArrayList
<String
>();
151 String mode
= display(wl
, bl
, rw
);
153 String trace
= mode
+ "[ " + command
+ "] ";
154 for (Object arg
: args
) {
157 long now
= System
.currentTimeMillis();
158 System
.out
.println(StringUtils
.fromTime(now
) + ": " + trace
);
162 rep
= doRequest(action
, command
, args
, rw
, whitelist
, blacklist
);
163 } catch (IOException e
) {
164 rep
= new RemoteLibraryException(e
, true);
167 commands
.put(id
, command
);
171 times
.put(id
, (new Date().getTime() - start
));
176 private String
display(boolean whitelist
, boolean blacklist
, boolean rw
) {
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
);
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
)));
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
>();
221 if ("*".equals(args
[0])) {
222 Progress pg
= createPgForwarder(action
);
224 for (MetaData meta
: Instance
.getInstance().getLibrary()
226 metas
.add(removeCover(meta
));
231 MetaData meta
= Instance
.getInstance().getLibrary()
232 .getInfo((String
) args
[0]);
234 if (meta
.getCover() == null) {
237 light
= meta
.clone();
238 light
.setCover(null);
244 for (int i
= 0; i
< metas
.size(); i
++) {
245 if (!isAllowed(metas
.get(i
), whitelist
, blacklist
)) {
251 return metas
.toArray(new MetaData
[0]);
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
)) {
266 Story story
= Instance
.getInstance().getLibrary()
267 .getStory((String
) args
[0], null);
268 for (Object obj
: breakStory(story
)) {
272 } else if ("SAVE_STORY".equals(command
)) {
274 throw new RemoteLibraryException(
275 "Read-Only remote library: " + args
[0], false);
278 List
<Object
> list
= new ArrayList
<Object
>();
281 Object obj
= action
.rec();
282 while (obj
!= null) {
288 Story story
= rebuildStory(list
);
289 Instance
.getInstance().getLibrary().save(story
, (String
) args
[0],
291 return story
.getMeta().getLuid();
292 } else if ("IMPORT".equals(command
)) {
294 throw new RemoteLibraryException(
295 "Read-Only remote library: " + args
[0], false);
298 Progress pg
= createPgForwarder(action
);
299 MetaData meta
= Instance
.getInstance().getLibrary()
300 .imprt(new URL((String
) args
[0]), pg
);
302 return meta
.getLuid();
303 } else if ("DELETE_STORY".equals(command
)) {
305 throw new RemoteLibraryException(
306 "Read-Only remote library: " + args
[0], false);
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]);
323 } else if ("SET_COVER".equals(command
)) {
325 throw new RemoteLibraryException(
326 "Read-Only remote library: " + args
[0] + ", " + args
[1],
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]);
337 } else if ("CHANGE_STA".equals(command
)) {
339 throw new RemoteLibraryException(
340 "Read-Only remote library: " + args
[0] + ", " + args
[1],
344 Progress pg
= createPgForwarder(action
);
345 Instance
.getInstance().getLibrary().changeSTA((String
) args
[0],
346 (String
) args
[1], (String
) args
[2], (String
) args
[3], pg
);
348 } else if ("EXIT".equals(command
)) {
350 throw new RemoteLibraryException(
351 "Read-Only remote library: EXIT", false);
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)]");
367 getTraceHandler().error(e
);
372 * Break a story in multiple {@link Object}s for easier serialisation.
375 * the {@link Story} to break
377 * @return the list of {@link Object}s
379 static List
<Object
> breakStory(Story story
) {
380 List
<Object
> list
= new ArrayList
<Object
>();
382 story
= story
.clone();
385 if (story
.getMeta().isImageDocument()) {
386 for (Chapter chap
: story
) {
388 list
.addAll(chap
.getParagraphs());
389 chap
.setParagraphs(new ArrayList
<Paragraph
>());
391 story
.setChapters(new ArrayList
<Chapter
>());
398 * Rebuild a story from a list of broke up {@link Story} parts.
401 * the list of {@link Story} parts
403 * @return the reconstructed {@link Story}
405 static Story
rebuildStory(List
<Object
> list
) {
409 for (Object obj
: list
) {
410 if (obj
instanceof Story
) {
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
);
424 * Update the {@link Progress} with the adequate {@link Object} received
425 * from the network via {@link RemoteLibraryServer}.
428 * the {@link Progress} to update
430 * the object received from the network
432 * @return TRUE if it was a progress event, FALSE if not
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;
442 if (updateProgress
) {
443 Object
[] a
= (Object
[]) rep
;
446 if (a
[0] instanceof String
) {
450 int min
= (Integer
) a
[0 + offset
];
451 int max
= (Integer
) a
[1 + offset
];
452 int progress
= (Integer
) a
[2 + offset
];
455 if (a
.length
> (3 + offset
)) {
456 meta
= a
[3 + offset
];
460 if (a
.length
> (4 + offset
)) {
461 name
= a
[4 + offset
] == null ?
"" : a
[4 + offset
].toString();
464 if (min
>= 0 && min
<= max
) {
466 pg
.setMinMax(min
, max
);
467 pg
.setProgress(progress
);
469 pg
.put("meta", meta
);
480 * Create a {@link Progress} that will forward its progress over the
484 * the {@link ConnectActionServerObject} to use to forward it
486 * @return the {@link Progress}
488 private Progress
createPgForwarder(final ConnectActionServerObject action
) {
489 final Boolean
[] isDoneForwarded
= new Boolean
[] { false };
490 final Progress pg
= new Progress() {
492 public boolean isDone() {
493 return isDoneForwarded
[0];
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() {
503 public void progress(Progress progress
, String name
) {
504 Object meta
= pg
.get("meta");
505 if (meta
instanceof MetaData
) {
506 meta
= removeCover((MetaData
) meta
);
509 int min
= pg
.getMin();
510 int max
= pg
.getMax();
511 int rel
= min
+ (int) Math
512 .round(pg
.getRelativeProgress() * (max
- min
));
514 boolean samePg
= p
[0] == min
&& p
[1] == max
&& p
[2] == rel
;
516 // Do not re-send the same value twice over the wire,
517 // unless more than 2 seconds have elapsed (to maintain the
519 if (!samePg
|| !same(pMeta
[0], meta
) || !same(pName
[0], name
) //
520 || (new Date().getTime() - lastTime
[0] > 2000)) {
528 action
.send(new Object
[] { "UPDATE", min
, max
, rel
,
531 } catch (Exception e
) {
532 getTraceHandler().error(e
);
535 lastTime
[0] = new Date().getTime();
538 isDoneForwarded
[0] = (pg
.getProgress() >= pg
.getMax());
545 private boolean same(Object obj1
, Object obj2
) {
546 if (obj1
== null || obj2
== null)
547 return obj1
== null && obj2
== null;
549 return obj1
.equals(obj2
);
552 // with 30 seconds timeout
553 private void forcePgDoneSent(Progress pg
) {
554 long start
= new Date().getTime();
556 while (!pg
.isDone() && new Date().getTime() - start
< 30000) {
559 } catch (InterruptedException e
) {
560 getTraceHandler().error(e
);
565 private MetaData
removeCover(MetaData meta
) {
566 MetaData light
= null;
568 if (meta
.getCover() == null) {
571 light
= meta
.clone();
572 light
.setCover(null);
579 private boolean isAllowed(MetaData meta
, List
<String
> whitelist
,
580 List
<String
> blacklist
) {
581 if (!whitelist
.isEmpty() && !whitelist
.contains(meta
.getSource())) {
585 if (blacklist
.contains(meta
.getSource())) {