1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.IOException
;
5 import java
.security
.InvalidParameterException
;
6 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
33 * <li>[md5] GET_STORY [luid]: will return the given story if it exists (or NULL
35 * <li>[md5] SAVE_STORY [luid]: save the story (that must be sent just after the
36 * command) with the given LUID, then return the LUID</li>
37 * <li>[md5] IMPORT [url]: save the story found at the given URL, then return
39 * <li>[md5] DELETE_STORY [luid]: delete the story of LUID luid</li>
40 * <li>[md5] GET_COVER [luid]: return the cover of the story</li>
41 * <li>[md5] GET_SOURCE_COVER [source]: return the cover for this source</li>
42 * <li>[md5] SET_SOURCE_COVER [source], [luid]: set the default cover for the
43 * given source to the cover of the story denoted by luid</li>
44 * <li>[md5] CHANGE_SOURCE [luid] [new source]: change the source of the story
46 * <li>[md5] EXIT: stop the server</li>
51 public class RemoteLibraryServer
extends ServerObject
{
52 private final String md5
;
55 * Create a new remote server (will not be active until
56 * {@link RemoteLibraryServer#start()} is called).
59 * the key that will restrict access to this server
61 * the port to listen on
64 * in case of I/O error
66 public RemoteLibraryServer(String key
, int port
) throws IOException
{
67 super("Fanfix remote library", port
, true);
68 this.md5
= StringUtils
.getMd5Hash(key
);
70 setTraceHandler(Instance
.getTraceHandler());
74 protected Object
onRequest(ConnectActionServerObject action
,
75 Version clientVersion
, Object data
) throws Exception
{
78 Object
[] args
= new Object
[0];
79 if (data
instanceof Object
[]) {
80 Object
[] dataArray
= (Object
[]) data
;
81 if (dataArray
.length
>= 2) {
82 md5
= "" + dataArray
[0];
83 command
= "" + dataArray
[1];
85 args
= new Object
[dataArray
.length
- 2];
86 for (int i
= 2; i
< dataArray
.length
; i
++) {
87 args
[i
- 2] = dataArray
[i
];
92 String trace
= "[" + command
+ "] ";
93 for (Object arg
: args
) {
96 getTraceHandler().trace(trace
);
98 if (!md5
.equals(this.md5
)) {
99 getTraceHandler().trace("Key rejected.");
103 if ("PING".equals(command
)) {
105 } else if ("GET_METADATA".equals(command
)) {
106 if ("*".equals(args
[0])) {
107 List
<MetaData
> metas
= Instance
.getLibrary().getMetas(
108 createPgForwarder(action
));
109 return metas
.toArray(new MetaData
[] {});
111 throw new InvalidParameterException(
112 "only * is valid here, but you passed: " + args
[0]);
113 } else if ("GET_STORY".equals(command
)) {
114 MetaData meta
= Instance
.getLibrary().getInfo((String
) args
[0]);
121 Story story
= Instance
.getLibrary()
122 .getStory((String
) args
[0], null);
123 for (Object obj
: breakStory(story
)) {
127 } else if ("SAVE_STORY".equals(command
)) {
128 List
<Object
> list
= new ArrayList
<Object
>();
131 Object obj
= action
.rec();
132 while (obj
!= null) {
138 Story story
= rebuildStory(list
);
139 Instance
.getLibrary().save(story
, (String
) args
[0], null);
140 return story
.getMeta().getLuid();
141 } else if ("IMPORT".equals(command
)) {
142 Story story
= Instance
.getLibrary().imprt(
143 new URL((String
) args
[0]), createPgForwarder(action
));
144 return story
.getMeta().getLuid();
145 } else if ("DELETE_STORY".equals(command
)) {
146 Instance
.getLibrary().delete((String
) args
[0]);
147 } else if ("GET_COVER".equals(command
)) {
148 return Instance
.getLibrary().getCover((String
) args
[0]);
149 } else if ("GET_SOURCE_COVER".equals(command
)) {
150 return Instance
.getLibrary().getSourceCover((String
) args
[0]);
151 } else if ("SET_SOURCE_COVER".equals(command
)) {
152 Instance
.getLibrary().setSourceCover((String
) args
[0],
154 } else if ("CHANGE_SOURCE".equals(command
)) {
155 Instance
.getLibrary().changeSource((String
) args
[0],
156 (String
) args
[1], createPgForwarder(action
));
157 } else if ("EXIT".equals(command
)) {
165 protected void onError(Exception e
) {
166 getTraceHandler().error(e
);
170 * Break a story in multiple {@link Object}s for easier serialisation.
173 * the {@link Story} to break
175 * @return the list of {@link Object}s
177 static List
<Object
> breakStory(Story story
) {
178 List
<Object
> list
= new ArrayList
<Object
>();
180 story
= story
.clone();
183 if (story
.getMeta().isImageDocument()) {
184 for (Chapter chap
: story
) {
186 list
.addAll(chap
.getParagraphs());
187 chap
.setParagraphs(new ArrayList
<Paragraph
>());
189 story
.setChapters(new ArrayList
<Chapter
>());
196 * Rebuild a story from a list of broke up {@link Story} parts.
199 * the list of {@link Story} parts
201 * @return the reconstructed {@link Story}
203 static Story
rebuildStory(List
<Object
> list
) {
207 for (Object obj
: list
) {
208 if (obj
instanceof Story
) {
210 } else if (obj
instanceof Chapter
) {
211 chap
= (Chapter
) obj
;
212 story
.getChapters().add(chap
);
213 } else if (obj
instanceof Paragraph
) {
214 chap
.getParagraphs().add((Paragraph
) obj
);
222 * Update the {@link Progress} with the adequate {@link Object} received
223 * from the network via {@link RemoteLibraryServer}.
226 * the {@link Progress} to update
228 * the object received from the network
230 * @return TRUE if it was a progress event, FALSE if not
232 static boolean updateProgress(Progress pg
, Object rep
) {
233 if (rep
instanceof Integer
[]) {
234 Integer
[] a
= (Integer
[]) rep
;
240 if (min
>= 0 && min
<= max
) {
241 pg
.setMinMax(min
, max
);
242 pg
.setProgress(progress
);
253 * Create a {@link Progress} that will forward its progress over the
257 * the {@link ConnectActionServerObject} to use to forward it
259 * @return the {@link Progress}
261 private static Progress
createPgForwarder(
262 final ConnectActionServerObject action
) {
263 final Progress pg
= new Progress();
264 final Integer
[] p
= new Integer
[] { -1, -1, -1 };
265 pg
.addProgressListener(new ProgressListener() {
267 public void progress(Progress progress
, String name
) {
268 int min
= pg
.getMin();
269 int max
= pg
.getMax();
270 int relativeProgress
= min
271 + (int) Math
.round(pg
.getRelativeProgress()
274 // Do not re-send the same value twice over the wire
275 if (p
[0] != min
|| p
[1] != max
|| p
[2] != relativeProgress
) {
278 p
[2] = relativeProgress
;
281 action
.send(new Integer
[] { min
, max
, relativeProgress
});
283 } catch (Exception e
) {
284 Instance
.getTraceHandler().error(e
);