1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
9 import be
.nikiroo
.fanfix
.Instance
;
10 import be
.nikiroo
.fanfix
.data
.Chapter
;
11 import be
.nikiroo
.fanfix
.data
.MetaData
;
12 import be
.nikiroo
.fanfix
.data
.Paragraph
;
13 import be
.nikiroo
.fanfix
.data
.Story
;
14 import be
.nikiroo
.utils
.Progress
;
15 import be
.nikiroo
.utils
.Progress
.ProgressListener
;
16 import be
.nikiroo
.utils
.StringUtils
;
17 import be
.nikiroo
.utils
.Version
;
18 import be
.nikiroo
.utils
.serial
.server
.ConnectActionServerObject
;
19 import be
.nikiroo
.utils
.serial
.server
.ServerObject
;
22 * Create a new remote server that will listen for order on the given port.
24 * The available commands are given as arrays of objects (first item is the key,
25 * second is the command, the rest are the arguments).
27 * The md5 is always a String (the MD5 hash of the access key), the commands are
28 * also Strings; the parameters vary depending upon the command.
30 * <li>[md5] PING: will return PONG if the key is accepted</li>
31 * <li>[md5] GET_METADATA *: will return the metadata of all the stories in the
32 * library (array)</li>
34 * <li>[md5] GET_METADATA [luid]: will return the metadata of the story of LUID
36 * <li>[md5] GET_STORY [luid]: will return the given story if it exists (or NULL
38 * <li>[md5] SAVE_STORY [luid]: save the story (that must be sent just after the
39 * command) with the given LUID, then return the LUID</li>
40 * <li>[md5] IMPORT [url]: save the story found at the given URL, then return
42 * <li>[md5] DELETE_STORY [luid]: delete the story of LUID luid</li>
43 * <li>[md5] GET_COVER [luid]: return the cover of the story</li>
44 * <li>[md5] GET_CUSTOM_SOURCE_COVER [source]: return the cover for this source</li>
45 * <li>[md5] SET_SOURCE_COVER [source], [luid]: set the default cover for the
46 * given source to the cover of the story denoted by luid</li>
47 * <li>[md5] CHANGE_SOURCE [luid] [new source]: change the source of the story
49 * <li>[md5] EXIT: stop the server</li>
54 public class RemoteLibraryServer
extends ServerObject
{
55 private final String md5
;
58 * Create a new remote server (will not be active until
59 * {@link RemoteLibraryServer#start()} is called).
62 * the key that will restrict access to this server
64 * the port to listen on
67 * in case of I/O error
69 public RemoteLibraryServer(String key
, int port
) throws IOException
{
70 super("Fanfix remote library", port
, true);
71 this.md5
= StringUtils
.getMd5Hash(key
);
73 setTraceHandler(Instance
.getTraceHandler());
77 protected Object
onRequest(ConnectActionServerObject action
,
78 Version clientVersion
, Object data
) throws Exception
{
81 Object
[] args
= new Object
[0];
82 if (data
instanceof Object
[]) {
83 Object
[] dataArray
= (Object
[]) data
;
84 if (dataArray
.length
>= 2) {
85 md5
= "" + dataArray
[0];
86 command
= "" + dataArray
[1];
88 args
= new Object
[dataArray
.length
- 2];
89 for (int i
= 2; i
< dataArray
.length
; i
++) {
90 args
[i
- 2] = dataArray
[i
];
95 String trace
= "[" + command
+ "] ";
96 for (Object arg
: args
) {
99 getTraceHandler().trace(trace
);
101 if (!md5
.equals(this.md5
)) {
102 getTraceHandler().trace("Key rejected.");
106 long start
= new Date().getTime();
107 Object rep
= doRequest(action
, command
, args
);
109 getTraceHandler().trace(
110 String
.format("[/%s]: %d ms", command
,
111 (new Date().getTime() - start
)));
116 private Object
doRequest(ConnectActionServerObject action
, String command
,
117 Object
[] args
) throws NoSuchFieldException
, NoSuchMethodException
,
118 ClassNotFoundException
, IOException
{
119 if ("PING".equals(command
)) {
121 } else if ("GET_METADATA".equals(command
)) {
122 if ("*".equals(args
[0])) {
123 Progress pg
= createPgForwarder(action
);
125 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
126 for (MetaData meta
: Instance
.getLibrary().getMetas(pg
)) {
128 if (meta
.getCover() == null) {
131 light
= meta
.clone();
132 light
.setCover(null);
139 return metas
.toArray(new MetaData
[] {});
142 return new MetaData
[] { Instance
.getLibrary().getInfo(
144 } else if ("GET_STORY".equals(command
)) {
145 MetaData meta
= Instance
.getLibrary().getInfo((String
) args
[0]);
152 Story story
= Instance
.getLibrary()
153 .getStory((String
) args
[0], null);
154 for (Object obj
: breakStory(story
)) {
158 } else if ("SAVE_STORY".equals(command
)) {
159 List
<Object
> list
= new ArrayList
<Object
>();
162 Object obj
= action
.rec();
163 while (obj
!= null) {
169 Story story
= rebuildStory(list
);
170 Instance
.getLibrary().save(story
, (String
) args
[0], null);
171 return story
.getMeta().getLuid();
172 } else if ("IMPORT".equals(command
)) {
173 Progress pg
= createPgForwarder(action
);
174 Story story
= Instance
.getLibrary().imprt(
175 new URL((String
) args
[0]), pg
);
177 return story
.getMeta().getLuid();
178 } else if ("DELETE_STORY".equals(command
)) {
179 Instance
.getLibrary().delete((String
) args
[0]);
180 } else if ("GET_COVER".equals(command
)) {
181 return Instance
.getLibrary().getCover((String
) args
[0]);
182 } else if ("GET_CUSTOM_SOURCE_COVER".equals(command
)) {
183 return Instance
.getLibrary().getCustomSourceCover((String
) args
[0]);
184 } else if ("SET_SOURCE_COVER".equals(command
)) {
185 Instance
.getLibrary().setSourceCover((String
) args
[0],
187 } else if ("CHANGE_SOURCE".equals(command
)) {
188 Progress pg
= createPgForwarder(action
);
189 Instance
.getLibrary().changeSource((String
) args
[0],
190 (String
) args
[1], pg
);
192 } else if ("EXIT".equals(command
)) {
200 protected void onError(Exception e
) {
201 getTraceHandler().error(e
);
205 * Break a story in multiple {@link Object}s for easier serialisation.
208 * the {@link Story} to break
210 * @return the list of {@link Object}s
212 static List
<Object
> breakStory(Story story
) {
213 List
<Object
> list
= new ArrayList
<Object
>();
215 story
= story
.clone();
218 if (story
.getMeta().isImageDocument()) {
219 for (Chapter chap
: story
) {
221 list
.addAll(chap
.getParagraphs());
222 chap
.setParagraphs(new ArrayList
<Paragraph
>());
224 story
.setChapters(new ArrayList
<Chapter
>());
231 * Rebuild a story from a list of broke up {@link Story} parts.
234 * the list of {@link Story} parts
236 * @return the reconstructed {@link Story}
238 static Story
rebuildStory(List
<Object
> list
) {
242 for (Object obj
: list
) {
243 if (obj
instanceof Story
) {
245 } else if (obj
instanceof Chapter
) {
246 chap
= (Chapter
) obj
;
247 story
.getChapters().add(chap
);
248 } else if (obj
instanceof Paragraph
) {
249 chap
.getParagraphs().add((Paragraph
) obj
);
257 * Update the {@link Progress} with the adequate {@link Object} received
258 * from the network via {@link RemoteLibraryServer}.
261 * the {@link Progress} to update
263 * the object received from the network
265 * @return TRUE if it was a progress event, FALSE if not
267 static boolean updateProgress(Progress pg
, Object rep
) {
268 if (rep
instanceof Integer
[]) {
269 Integer
[] a
= (Integer
[]) rep
;
275 if (min
>= 0 && min
<= max
) {
276 pg
.setMinMax(min
, max
);
277 pg
.setProgress(progress
);
288 * Create a {@link Progress} that will forward its progress over the
292 * the {@link ConnectActionServerObject} to use to forward it
294 * @return the {@link Progress}
296 private static Progress
createPgForwarder(
297 final ConnectActionServerObject action
) {
298 final Boolean
[] isDoneForwarded
= new Boolean
[] { false };
299 final Progress pg
= new Progress() {
301 public boolean isDone() {
302 return isDoneForwarded
[0];
306 final Integer
[] p
= new Integer
[] { -1, -1, -1 };
307 final Long
[] lastTime
= new Long
[] { new Date().getTime() };
308 pg
.addProgressListener(new ProgressListener() {
310 public void progress(Progress progress
, String name
) {
311 int min
= pg
.getMin();
312 int max
= pg
.getMax();
313 int relativeProgress
= min
314 + (int) Math
.round(pg
.getRelativeProgress()
317 // Do not re-send the same value twice over the wire,
318 // unless more than 2 seconds have elapsed (to maintain the
320 if ((p
[0] != min
|| p
[1] != max
|| p
[2] != relativeProgress
)
321 || (new Date().getTime() - lastTime
[0] > 2000)) {
324 p
[2] = relativeProgress
;
327 action
.send(new Integer
[] { min
, max
, relativeProgress
});
329 } catch (Exception e
) {
330 Instance
.getTraceHandler().error(e
);
333 isDoneForwarded
[0] = pg
.isDone();
334 lastTime
[0] = new Date().getTime();
342 // with 30 seconds timeout
343 private static void forcePgDoneSent(Progress pg
) {
344 long start
= new Date().getTime();
346 while (!pg
.isDone() && new Date().getTime() - start
< 30000) {
349 } catch (InterruptedException e
) {
350 Instance
.getTraceHandler().error(e
);