1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.IOException
;
4 import java
.security
.InvalidParameterException
;
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
32 * <li>[md5] GET_STORY [luid]: will return the given story if it exists (or NULL
34 * <li>[md5] SAVE_STORY [luid]: save the story (that must be sent just after the
35 * command) with the given LUID, then return the LUID</li>
36 * <li>[md5] DELETE_STORY [luid]: delete the story of LUID luid</li>
37 * <li>[md5] GET_COVER [luid]: return the cover of the story</li>
38 * <li>[md5] GET_SOURCE_COVER [source]: return the cover for this source</li>
39 * <li>[md5] SET_SOURCE_COVER [source], [luid]: set the default cover for the
40 * given source to the cover of the story denoted by luid</li>
41 * <li>[md5] EXIT: stop the server</li>
46 public class RemoteLibraryServer
extends ServerObject
{
47 private final String md5
;
50 * Create a new remote server (will not be active until
51 * {@link RemoteLibraryServer#start()} is called).
54 * the key that will restrict access to this server
56 * the port to listen on
59 * in case of I/O error
61 public RemoteLibraryServer(String key
, int port
) throws IOException
{
62 super("Fanfix remote library", port
, true);
63 this.md5
= StringUtils
.getMd5Hash(key
);
65 setTraceHandler(Instance
.getTraceHandler());
69 protected Object
onRequest(ConnectActionServerObject action
,
70 Version clientVersion
, Object data
) throws Exception
{
73 Object
[] args
= new Object
[0];
74 if (data
instanceof Object
[]) {
75 Object
[] dataArray
= (Object
[]) data
;
76 if (dataArray
.length
>= 2) {
77 args
= new Object
[dataArray
.length
- 2];
78 for (int i
= 2; i
< dataArray
.length
; i
++) {
79 args
[i
- 2] = dataArray
[i
];
82 md5
= "" + dataArray
[0];
83 command
= "" + dataArray
[1];
87 String trace
= "[" + command
+ "] ";
88 for (Object arg
: args
) {
91 getTraceHandler().trace(trace
);
93 if (!md5
.equals(this.md5
)) {
94 getTraceHandler().trace("Key rejected.");
98 if ("PING".equals(command
)) {
100 } else if ("GET_METADATA".equals(command
)) {
101 if ("*".equals(args
[0])) {
102 List
<MetaData
> metas
= Instance
.getLibrary().getMetas(
103 createPgForwarder(action
));
104 return metas
.toArray(new MetaData
[] {});
106 throw new InvalidParameterException(
107 "only * is valid here, but you passed: " + args
[0]);
108 } else if ("GET_STORY".equals(command
)) {
109 MetaData meta
= Instance
.getLibrary().getInfo((String
) args
[0]);
116 Story story
= Instance
.getLibrary()
117 .getStory((String
) args
[0], null);
118 for (Object obj
: breakStory(story
)) {
122 } else if ("SAVE_STORY".equals(command
)) {
123 List
<Object
> list
= new ArrayList
<Object
>();
126 Object obj
= action
.rec();
127 while (obj
!= null) {
133 Story story
= rebuildStory(list
);
134 Instance
.getLibrary().save(story
, (String
) args
[0], null);
135 return story
.getMeta().getLuid();
136 } else if ("DELETE_STORY".equals(command
)) {
137 Instance
.getLibrary().delete((String
) args
[0]);
138 } else if ("GET_COVER".equals(command
)) {
139 return Instance
.getLibrary().getCover((String
) args
[0]);
140 } else if ("GET_SOURCE_COVER".equals(command
)) {
141 return Instance
.getLibrary().getSourceCover((String
) args
[0]);
142 } else if ("SET_SOURCE_COVER".equals(command
)) {
143 Instance
.getLibrary().setSourceCover((String
) args
[0],
145 } else if ("EXIT".equals(command
)) {
153 protected void onError(Exception e
) {
154 getTraceHandler().error(e
);
158 * Break a story in multiple {@link Object}s for easier serialisation.
161 * the {@link Story} to break
163 * @return the list of {@link Object}s
165 static List
<Object
> breakStory(Story story
) {
166 List
<Object
> list
= new ArrayList
<Object
>();
168 story
= story
.clone();
171 if (story
.getMeta().isImageDocument()) {
172 for (Chapter chap
: story
) {
174 list
.addAll(chap
.getParagraphs());
175 chap
.setParagraphs(new ArrayList
<Paragraph
>());
177 story
.setChapters(new ArrayList
<Chapter
>());
184 * Rebuild a story from a list of broke up {@link Story} parts.
187 * the list of {@link Story} parts
189 * @return the reconstructed {@link Story}
191 static Story
rebuildStory(List
<Object
> list
) {
195 for (Object obj
: list
) {
196 if (obj
instanceof Story
) {
198 } else if (obj
instanceof Chapter
) {
199 chap
= (Chapter
) obj
;
200 story
.getChapters().add(chap
);
201 } else if (obj
instanceof Paragraph
) {
202 chap
.getParagraphs().add((Paragraph
) obj
);
210 * Update the {@link Progress} with the adequate {@link Object} received
211 * from the network via {@link RemoteLibraryServer}.
214 * the {@link Progress} to update
216 * the object received from the network
218 * @return TRUE if it was a progress event, FALSE if not
220 static boolean updateProgress(Progress pg
, Object rep
) {
221 if (rep
instanceof Integer
[]) {
222 Integer
[] a
= (Integer
[]) rep
;
228 if (min
>= 0 && min
<= max
) {
229 pg
.setMinMax(min
, max
);
230 pg
.setProgress(progress
);
241 * Create a {@link Progress} that will forward its progress over the
245 * the {@link ConnectActionServerObject} to use to forward it
247 * @return the {@link Progress}
249 private static Progress
createPgForwarder(
250 final ConnectActionServerObject action
) {
251 final Progress pg
= new Progress();
252 final Integer
[] p
= new Integer
[] { -1, -1, -1 };
253 pg
.addProgressListener(new ProgressListener() {
255 public void progress(Progress progress
, String name
) {
256 int min
= pg
.getMin();
257 int max
= pg
.getMax();
258 int relativeProgress
= min
259 + (int) Math
.round(pg
.getRelativeProgress()
262 // Do not re-send the same value twice over the wire
263 if (p
[0] != min
|| p
[1] != max
|| p
[2] != relativeProgress
) {
266 p
[2] = relativeProgress
;
269 action
.send(new Integer
[] { min
, max
, relativeProgress
});
271 } catch (Exception e
) {
272 Instance
.getTraceHandler().error(e
);