1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
8 import java
.util
.HashMap
;
12 import javax
.net
.ssl
.SSLException
;
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
;
28 * Create a new remote server that will listen for orders on the given port.
30 * The available commands are given as arrays of objects (first item is the
31 * command, the rest are the arguments).
33 * All the commands are always prefixed by the subkey (which can be EMPTY if
37 * <li>PING: will return the mode if the key is accepted (mode can be: "r/o" or
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
43 * <li>GET_STORY [luid]: will return the given story if it exists (or NULL if
45 * <li>SAVE_STORY [luid]: save the story (that must be sent just after the
46 * command) with the given LUID, then return the LUID</li>
47 * <li>IMPORT [url]: save the story found at the given URL, then return the LUID
49 * <li>DELETE_STORY [luid]: delete the story of LUID luid</li>
50 * <li>GET_COVER [luid]: return the cover of the story</li>
51 * <li>GET_CUSTOM_COVER ["SOURCE"|"AUTHOR"] [source]: return the cover for this
53 * <li>SET_COVER ["SOURCE"|"AUTHOR"] [value] [luid]: set the default cover for
54 * the given source/author to the cover of the story denoted by luid</li>
55 * <li>CHANGE_SOURCE [luid] [new source]: change the source of the story of LUID
57 * <li>EXIT: stop the server</li>
62 public class RemoteLibraryServer
extends ServerObject
{
63 private Map
<Long
, String
> commands
= new HashMap
<Long
, String
>();
64 private Map
<Long
, Long
> times
= new HashMap
<Long
, Long
>();
65 private Map
<Long
, Boolean
> wls
= new HashMap
<Long
, Boolean
>();
66 private Map
<Long
, Boolean
> bls
= new HashMap
<Long
, Boolean
>();
67 private Map
<Long
, Boolean
> rws
= new HashMap
<Long
, Boolean
>();
70 * Create a new remote server (will not be active until
71 * {@link RemoteLibraryServer#start()} is called).
73 * Note: the key we use here is the encryption key (it must not contain a
77 * in case of I/O error
79 public RemoteLibraryServer() throws IOException
{
80 super("Fanfix remote library",
81 Instance
.getInstance().getConfig()
82 .getInteger(Config
.SERVER_PORT
),
83 Instance
.getInstance().getConfig()
84 .getString(Config
.SERVER_KEY
));
86 setTraceHandler(Instance
.getInstance().getTraceHandler());
90 protected Object
onRequest(ConnectActionServerObject action
,
91 Version clientVersion
, Object data
, long id
) throws Exception
{
92 long start
= new Date().getTime();
94 // defaults are positive (as previous versions without the feature)
101 Object
[] args
= new Object
[0];
102 if (data
instanceof Object
[]) {
103 Object
[] dataArray
= (Object
[]) data
;
104 if (dataArray
.length
> 0) {
105 subkey
= "" + dataArray
[0];
107 if (dataArray
.length
> 1) {
108 command
= "" + dataArray
[1];
110 args
= new Object
[dataArray
.length
- 2];
111 for (int i
= 2; i
< dataArray
.length
; i
++) {
112 args
[i
- 2] = dataArray
[i
];
117 List
<String
> whitelist
= Instance
.getInstance().getConfig()
118 .getList(Config
.SERVER_WHITELIST
);
119 if (whitelist
== null) {
120 whitelist
= new ArrayList
<String
>();
122 List
<String
> blacklist
= Instance
.getInstance().getConfig()
123 .getList(Config
.SERVER_BLACKLIST
);
124 if (blacklist
== null) {
125 blacklist
= new ArrayList
<String
>();
128 if (whitelist
.isEmpty()) {
132 rw
= Instance
.getInstance().getConfig().getBoolean(Config
.SERVER_RW
,
134 if (!subkey
.isEmpty()) {
135 List
<String
> allowed
= Instance
.getInstance().getConfig()
136 .getList(Config
.SERVER_ALLOWED_SUBKEYS
);
137 if (allowed
.contains(subkey
)) {
138 if ((subkey
+ "|").contains("|rw|")) {
141 if ((subkey
+ "|").contains("|wl|")) {
142 wl
= false; // |wl| = bypass whitelist
143 whitelist
= new ArrayList
<String
>();
145 if ((subkey
+ "|").contains("|bl|")) {
146 bl
= false; // |bl| = bypass blacklist
147 blacklist
= new ArrayList
<String
>();
152 String mode
= display(wl
, bl
, rw
);
154 String trace
= mode
+ "[ " + command
+ "] ";
155 for (Object arg
: args
) {
158 long now
= System
.currentTimeMillis();
159 System
.out
.println(StringUtils
.fromTime(now
) + ": " + trace
);
163 rep
= doRequest(action
, command
, args
, rw
, whitelist
, blacklist
);
164 } catch (IOException e
) {
165 rep
= new RemoteLibraryException(e
, true);
168 commands
.put(id
, command
);
172 times
.put(id
, (new Date().getTime() - start
));
177 private String
display(boolean whitelist
, boolean blacklist
, boolean rw
) {
193 protected void onRequestDone(long id
, long bytesReceived
, long bytesSent
) {
194 boolean whitelist
= wls
.get(id
);
195 boolean blacklist
= bls
.get(id
);
196 boolean rw
= rws
.get(id
);
201 String rec
= StringUtils
.formatNumber(bytesReceived
) + "b";
202 String sent
= StringUtils
.formatNumber(bytesSent
) + "b";
203 long now
= System
.currentTimeMillis();
204 System
.out
.println(StringUtils
.fromTime(now
) + ": "
205 + String
.format("%s[>%s]: (%s sent, %s rec) in %d ms",
206 display(whitelist
, blacklist
, rw
), commands
.get(id
),
207 sent
, rec
, times
.get(id
)));
213 private Object
doRequest(ConnectActionServerObject action
, String command
,
214 Object
[] args
, boolean rw
, List
<String
> whitelist
,
215 List
<String
> blacklist
) throws NoSuchFieldException
,
216 NoSuchMethodException
, ClassNotFoundException
, IOException
{
217 if ("PING".equals(command
)) {
218 return rw ?
"r/w" : "r/o";
219 } else if ("GET_METADATA".equals(command
)) {
220 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
222 if ("*".equals(args
[0])) {
223 Progress pg
= createPgForwarder(action
);
225 for (MetaData meta
: Instance
.getInstance().getLibrary()
227 metas
.add(removeCover(meta
));
232 MetaData meta
= Instance
.getInstance().getLibrary()
233 .getInfo((String
) args
[0]);
235 if (meta
.getCover() == null) {
238 light
= meta
.clone();
239 light
.setCover(null);
245 for (int i
= 0; i
< metas
.size(); i
++) {
246 if (!isAllowed(metas
.get(i
), whitelist
, blacklist
)) {
252 return metas
.toArray(new MetaData
[0]);
254 } else if ("GET_STORY".equals(command
)) {
255 MetaData meta
= Instance
.getInstance().getLibrary()
256 .getInfo((String
) args
[0]);
257 if (meta
== null || !isAllowed(meta
, whitelist
, blacklist
)) {
267 Story story
= Instance
.getInstance().getLibrary()
268 .getStory((String
) args
[0], null);
269 for (Object obj
: breakStory(story
)) {
273 } else if ("SAVE_STORY".equals(command
)) {
275 throw new RemoteLibraryException(
276 "Read-Only remote library: " + args
[0], false);
279 List
<Object
> list
= new ArrayList
<Object
>();
282 Object obj
= action
.rec();
283 while (obj
!= null) {
289 Story story
= rebuildStory(list
);
290 Instance
.getInstance().getLibrary().save(story
, (String
) args
[0],
292 return story
.getMeta().getLuid();
293 } else if ("IMPORT".equals(command
)) {
295 throw new RemoteLibraryException(
296 "Read-Only remote library: " + args
[0], false);
299 Progress pg
= createPgForwarder(action
);
300 MetaData meta
= Instance
.getInstance().getLibrary()
301 .imprt(new URL((String
) args
[0]), pg
);
303 return meta
.getLuid();
304 } else if ("DELETE_STORY".equals(command
)) {
306 throw new RemoteLibraryException(
307 "Read-Only remote library: " + args
[0], false);
310 Instance
.getInstance().getLibrary().delete((String
) args
[0]);
311 } else if ("GET_COVER".equals(command
)) {
312 return Instance
.getInstance().getLibrary()
313 .getCover((String
) args
[0]);
314 } else if ("GET_CUSTOM_COVER".equals(command
)) {
315 if ("SOURCE".equals(args
[0])) {
316 return Instance
.getInstance().getLibrary()
317 .getCustomSourceCover((String
) args
[1]);
318 } else if ("AUTHOR".equals(args
[0])) {
319 return Instance
.getInstance().getLibrary()
320 .getCustomAuthorCover((String
) args
[1]);
324 } else if ("SET_COVER".equals(command
)) {
326 throw new RemoteLibraryException(
327 "Read-Only remote library: " + args
[0] + ", " + args
[1],
331 if ("SOURCE".equals(args
[0])) {
332 Instance
.getInstance().getLibrary()
333 .setSourceCover((String
) args
[1], (String
) args
[2]);
334 } else if ("AUTHOR".equals(args
[0])) {
335 Instance
.getInstance().getLibrary()
336 .setAuthorCover((String
) args
[1], (String
) args
[2]);
338 } else if ("CHANGE_STA".equals(command
)) {
340 throw new RemoteLibraryException(
341 "Read-Only remote library: " + args
[0] + ", " + args
[1],
345 Progress pg
= createPgForwarder(action
);
346 Instance
.getInstance().getLibrary().changeSTA((String
) args
[0],
347 (String
) args
[1], (String
) args
[2], (String
) args
[3], pg
);
349 } else if ("EXIT".equals(command
)) {
351 throw new RemoteLibraryException(
352 "Read-Only remote library: EXIT", false);
362 protected void onError(Exception e
) {
363 if (e
instanceof SSLException
) {
364 long now
= System
.currentTimeMillis();
365 System
.out
.println(StringUtils
.fromTime(now
) + ": "
366 + "[Client connection refused (bad key)]");
368 getTraceHandler().error(e
);
373 * Break a story in multiple {@link Object}s for easier serialisation.
376 * the {@link Story} to break
378 * @return the list of {@link Object}s
380 static List
<Object
> breakStory(Story story
) {
381 List
<Object
> list
= new ArrayList
<Object
>();
383 story
= story
.clone();
386 if (story
.getMeta().isImageDocument()) {
387 for (Chapter chap
: story
) {
389 list
.addAll(chap
.getParagraphs());
390 chap
.setParagraphs(new ArrayList
<Paragraph
>());
392 story
.setChapters(new ArrayList
<Chapter
>());
399 * Rebuild a story from a list of broke up {@link Story} parts.
402 * the list of {@link Story} parts
404 * @return the reconstructed {@link Story}
406 static Story
rebuildStory(List
<Object
> list
) {
410 for (Object obj
: list
) {
411 if (obj
instanceof Story
) {
413 } else if (obj
instanceof Chapter
) {
414 chap
= (Chapter
) obj
;
415 story
.getChapters().add(chap
);
416 } else if (obj
instanceof Paragraph
) {
417 chap
.getParagraphs().add((Paragraph
) obj
);
425 * Update the {@link Progress} with the adequate {@link Object} received
426 * from the network via {@link RemoteLibraryServer}.
429 * the {@link Progress} to update
431 * the object received from the network
433 * @return TRUE if it was a progress event, FALSE if not
435 static boolean updateProgress(Progress pg
, Object rep
) {
436 boolean updateProgress
= false;
437 if (rep
instanceof Integer
[] && ((Integer
[]) rep
).length
== 3)
438 updateProgress
= true;
439 if (rep
instanceof Object
[] && ((Object
[]) rep
).length
>= 5
440 && "UPDATE".equals(((Object
[]) rep
)[0]))
441 updateProgress
= true;
443 if (updateProgress
) {
444 Object
[] a
= (Object
[]) rep
;
447 if (a
[0] instanceof String
) {
451 int min
= (Integer
) a
[0 + offset
];
452 int max
= (Integer
) a
[1 + offset
];
453 int progress
= (Integer
) a
[2 + offset
];
456 if (a
.length
> (3 + offset
)) {
457 meta
= a
[3 + offset
];
461 if (a
.length
> (4 + offset
)) {
462 name
= a
[4 + offset
] == null ?
"" : a
[4 + offset
].toString();
465 if (min
>= 0 && min
<= max
) {
467 pg
.setMinMax(min
, max
);
468 pg
.setProgress(progress
);
470 pg
.put("meta", meta
);
481 * Create a {@link Progress} that will forward its progress over the
485 * the {@link ConnectActionServerObject} to use to forward it
487 * @return the {@link Progress}
489 private Progress
createPgForwarder(final ConnectActionServerObject action
) {
490 final Boolean
[] isDoneForwarded
= new Boolean
[] { false };
491 final Progress pg
= new Progress() {
493 public boolean isDone() {
494 return isDoneForwarded
[0];
498 final Integer
[] p
= new Integer
[] { -1, -1, -1 };
499 final Object
[] pMeta
= new MetaData
[1];
500 final String
[] pName
= new String
[1];
501 final Long
[] lastTime
= new Long
[] { new Date().getTime() };
502 pg
.addProgressListener(new ProgressListener() {
504 public void progress(Progress progress
, String name
) {
505 Object meta
= pg
.get("meta");
506 if (meta
instanceof MetaData
) {
507 meta
= removeCover((MetaData
) meta
);
510 int min
= pg
.getMin();
511 int max
= pg
.getMax();
512 int rel
= min
+ (int) Math
513 .round(pg
.getRelativeProgress() * (max
- min
));
515 boolean samePg
= p
[0] == min
&& p
[1] == max
&& p
[2] == rel
;
517 // Do not re-send the same value twice over the wire,
518 // unless more than 2 seconds have elapsed (to maintain the
520 if (!samePg
|| !same(pMeta
[0], meta
) || !same(pName
[0], name
) //
521 || (new Date().getTime() - lastTime
[0] > 2000)) {
529 action
.send(new Object
[] { "UPDATE", min
, max
, rel
,
532 } catch (Exception e
) {
533 getTraceHandler().error(e
);
536 lastTime
[0] = new Date().getTime();
539 isDoneForwarded
[0] = (pg
.getProgress() >= pg
.getMax());
546 private boolean same(Object obj1
, Object obj2
) {
547 if (obj1
== null || obj2
== null)
548 return obj1
== null && obj2
== null;
550 return obj1
.equals(obj2
);
553 // with 30 seconds timeout
554 private void forcePgDoneSent(Progress pg
) {
555 long start
= new Date().getTime();
557 while (!pg
.isDone() && new Date().getTime() - start
< 30000) {
560 } catch (InterruptedException e
) {
561 getTraceHandler().error(e
);
566 private MetaData
removeCover(MetaData meta
) {
567 MetaData light
= null;
569 if (meta
.getCover() == null) {
572 light
= meta
.clone();
573 light
.setCover(null);
580 private boolean isAllowed(MetaData meta
, List
<String
> whitelist
,
581 List
<String
> blacklist
) {
582 MetaResultList one
= new MetaResultList(Arrays
.asList(meta
));
583 if (!whitelist
.isEmpty()) {
584 if (one
.filter(whitelist
, null, null).isEmpty()) {
588 if (!blacklist
.isEmpty()) {
589 if (!one
.filter(blacklist
, null, null).isEmpty()) {