1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
8 import be
.nikiroo
.fanfix
.Instance
;
9 import be
.nikiroo
.fanfix
.data
.Chapter
;
10 import be
.nikiroo
.fanfix
.data
.MetaData
;
11 import be
.nikiroo
.fanfix
.data
.Paragraph
;
12 import be
.nikiroo
.fanfix
.data
.Story
;
13 import be
.nikiroo
.utils
.Progress
;
14 import be
.nikiroo
.utils
.Progress
.ProgressListener
;
15 import be
.nikiroo
.utils
.StringUtils
;
16 import be
.nikiroo
.utils
.Version
;
17 import be
.nikiroo
.utils
.serial
.server
.ConnectActionServerObject
;
18 import be
.nikiroo
.utils
.serial
.server
.ServerObject
;
21 * Create a new remote server that will listen for order on the given port.
23 * The available commands are given as arrays of objects (first item is the key,
24 * second is the command, the rest are the arguments).
26 * The md5 is always a String (the MD5 hash of the access key), the commands are
27 * also Strings; the parameters vary depending upon the command.
29 * <li>[md5] PING: will return PONG if the key is accepted</li>
30 * <li>[md5] GET_METADATA *: will return the metadata of all the stories in the
31 * library (array)</li>
33 * <li>[md5] GET_METADATA [luid]: will return the metadata of the story of LUID
35 * <li>[md5] GET_STORY [luid]: will return the given story if it exists (or NULL
37 * <li>[md5] SAVE_STORY [luid]: save the story (that must be sent just after the
38 * command) with the given LUID, then return the LUID</li>
39 * <li>[md5] IMPORT [url]: save the story found at the given URL, then return
41 * <li>[md5] DELETE_STORY [luid]: delete the story of LUID luid</li>
42 * <li>[md5] GET_COVER [luid]: return the cover of the story</li>
43 * <li>[md5] GET_SOURCE_COVER [source]: return the cover for this source</li>
44 * <li>[md5] SET_SOURCE_COVER [source], [luid]: set the default cover for the
45 * given source to the cover of the story denoted by luid</li>
46 * <li>[md5] CHANGE_SOURCE [luid] [new source]: change the source of the story
48 * <li>[md5] EXIT: stop the server</li>
53 public class RemoteLibraryServer
extends ServerObject
{
54 private final String md5
;
57 * Create a new remote server (will not be active until
58 * {@link RemoteLibraryServer#start()} is called).
61 * the key that will restrict access to this server
63 * the port to listen on
66 * in case of I/O error
68 public RemoteLibraryServer(String key
, int port
) throws IOException
{
69 super("Fanfix remote library", port
, true);
70 this.md5
= StringUtils
.getMd5Hash(key
);
72 setTraceHandler(Instance
.getTraceHandler());
76 protected Object
onRequest(ConnectActionServerObject action
,
77 Version clientVersion
, Object data
) throws Exception
{
80 Object
[] args
= new Object
[0];
81 if (data
instanceof Object
[]) {
82 Object
[] dataArray
= (Object
[]) data
;
83 if (dataArray
.length
>= 2) {
84 md5
= "" + dataArray
[0];
85 command
= "" + dataArray
[1];
87 args
= new Object
[dataArray
.length
- 2];
88 for (int i
= 2; i
< dataArray
.length
; i
++) {
89 args
[i
- 2] = dataArray
[i
];
94 String trace
= "[" + command
+ "] ";
95 for (Object arg
: args
) {
98 getTraceHandler().trace(trace
);
100 if (!md5
.equals(this.md5
)) {
101 getTraceHandler().trace("Key rejected.");
105 if ("PING".equals(command
)) {
107 } else if ("GET_METADATA".equals(command
)) {
108 if ("*".equals(args
[0])) {
109 List
<MetaData
> metas
= Instance
.getLibrary().getMetas(
110 createPgForwarder(action
));
111 return metas
.toArray(new MetaData
[] {});
114 return new MetaData
[] { Instance
.getLibrary().getInfo(
116 } else if ("GET_STORY".equals(command
)) {
117 MetaData meta
= Instance
.getLibrary().getInfo((String
) args
[0]);
124 Story story
= Instance
.getLibrary()
125 .getStory((String
) args
[0], null);
126 for (Object obj
: breakStory(story
)) {
130 } else if ("SAVE_STORY".equals(command
)) {
131 List
<Object
> list
= new ArrayList
<Object
>();
134 Object obj
= action
.rec();
135 while (obj
!= null) {
141 Story story
= rebuildStory(list
);
142 Instance
.getLibrary().save(story
, (String
) args
[0], null);
143 return story
.getMeta().getLuid();
144 } else if ("IMPORT".equals(command
)) {
145 Story story
= Instance
.getLibrary().imprt(
146 new URL((String
) args
[0]), createPgForwarder(action
));
147 return story
.getMeta().getLuid();
148 } else if ("DELETE_STORY".equals(command
)) {
149 Instance
.getLibrary().delete((String
) args
[0]);
150 } else if ("GET_COVER".equals(command
)) {
151 return Instance
.getLibrary().getCover((String
) args
[0]);
152 } else if ("GET_SOURCE_COVER".equals(command
)) {
153 return Instance
.getLibrary().getSourceCover((String
) args
[0]);
154 } else if ("SET_SOURCE_COVER".equals(command
)) {
155 Instance
.getLibrary().setSourceCover((String
) args
[0],
157 } else if ("CHANGE_SOURCE".equals(command
)) {
158 Instance
.getLibrary().changeSource((String
) args
[0],
159 (String
) args
[1], createPgForwarder(action
));
160 } else if ("EXIT".equals(command
)) {
168 protected void onError(Exception e
) {
169 getTraceHandler().error(e
);
173 * Break a story in multiple {@link Object}s for easier serialisation.
176 * the {@link Story} to break
178 * @return the list of {@link Object}s
180 static List
<Object
> breakStory(Story story
) {
181 List
<Object
> list
= new ArrayList
<Object
>();
183 story
= story
.clone();
186 if (story
.getMeta().isImageDocument()) {
187 for (Chapter chap
: story
) {
189 list
.addAll(chap
.getParagraphs());
190 chap
.setParagraphs(new ArrayList
<Paragraph
>());
192 story
.setChapters(new ArrayList
<Chapter
>());
199 * Rebuild a story from a list of broke up {@link Story} parts.
202 * the list of {@link Story} parts
204 * @return the reconstructed {@link Story}
206 static Story
rebuildStory(List
<Object
> list
) {
210 for (Object obj
: list
) {
211 if (obj
instanceof Story
) {
213 } else if (obj
instanceof Chapter
) {
214 chap
= (Chapter
) obj
;
215 story
.getChapters().add(chap
);
216 } else if (obj
instanceof Paragraph
) {
217 chap
.getParagraphs().add((Paragraph
) obj
);
225 * Update the {@link Progress} with the adequate {@link Object} received
226 * from the network via {@link RemoteLibraryServer}.
229 * the {@link Progress} to update
231 * the object received from the network
233 * @return TRUE if it was a progress event, FALSE if not
235 static boolean updateProgress(Progress pg
, Object rep
) {
236 if (rep
instanceof Integer
[]) {
237 Integer
[] a
= (Integer
[]) rep
;
243 if (min
>= 0 && min
<= max
) {
244 pg
.setMinMax(min
, max
);
245 pg
.setProgress(progress
);
256 * Create a {@link Progress} that will forward its progress over the
260 * the {@link ConnectActionServerObject} to use to forward it
262 * @return the {@link Progress}
264 private static Progress
createPgForwarder(
265 final ConnectActionServerObject action
) {
266 final Progress pg
= new Progress();
267 final Integer
[] p
= new Integer
[] { -1, -1, -1 };
268 pg
.addProgressListener(new ProgressListener() {
270 public void progress(Progress progress
, String name
) {
271 int min
= pg
.getMin();
272 int max
= pg
.getMax();
273 int relativeProgress
= min
274 + (int) Math
.round(pg
.getRelativeProgress()
277 // Do not re-send the same value twice over the wire
278 if (p
[0] != min
|| p
[1] != max
|| p
[2] != relativeProgress
) {
281 p
[2] = relativeProgress
;
284 action
.send(new Integer
[] { min
, max
, relativeProgress
});
286 } catch (Exception e
) {
287 Instance
.getTraceHandler().error(e
);