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
.Version
;
16 import be
.nikiroo
.utils
.serial
.server
.ConnectActionServerObject
;
17 import be
.nikiroo
.utils
.serial
.server
.ServerObject
;
20 * Create a new remote server that will listen for order on the given port.
22 * The available commands are given as arrays of objects (first item is the key,
23 * second is the command, the rest are the arguments).
25 * The key is always a String, the commands are also Strings; the parameters
26 * vary depending upon the command.
28 * <li>[key] GET_METADATA *: will return the metadata of all the stories in the
30 * <li>[key] GET_STORY [luid]: will return the given story if it exists (or NULL
32 * <li>[key] SAVE_STORY [luid]: save the story (that must be sent just after the
33 * command) with the given LUID</li>
34 * <li>[key] DELETE_STORY [luid]: delete the story of LUID luid</li>
35 * <li>[key] GET_COVER [luid]: return the cover of the story</li>
36 * <li>[key] GET_SOURCE_COVER [source]: return the cover for this source</li>
37 * <li>[key] SET_SOURCE_COVER [source], [luid]: set the default cover for the
38 * given source to the cover of the story denoted by luid</li>
39 * <li>[key] EXIT: stop the server</li>
44 public class RemoteLibraryServer
extends ServerObject
{
45 private final String key
;
48 * Create a new remote server (will not be active until
49 * {@link RemoteLibraryServer#start()} is called).
52 * the key that will restrict access to this server
54 * the port to listen on
57 * in case of I/O error
59 public RemoteLibraryServer(String key
, int port
) throws IOException
{
60 super("Fanfix remote library", port
, true);
63 setTraceHandler(Instance
.getTraceHandler());
67 protected Object
onRequest(ConnectActionServerObject action
,
68 Version clientVersion
, Object data
) throws Exception
{
71 Object
[] args
= new Object
[0];
72 if (data
instanceof Object
[]) {
73 Object
[] dataArray
= (Object
[]) data
;
74 if (dataArray
.length
>= 2) {
75 args
= new Object
[dataArray
.length
- 2];
76 for (int i
= 2; i
< dataArray
.length
; i
++) {
77 args
[i
- 2] = dataArray
[i
];
80 key
= "" + dataArray
[0];
81 command
= "" + dataArray
[1];
85 String trace
= "[" + command
+ "] ";
86 for (Object arg
: args
) {
89 getTraceHandler().trace(trace
);
91 if (!key
.equals(this.key
)) {
92 getTraceHandler().trace("Key rejected.");
96 if ("GET_METADATA".equals(command
)) {
97 if (args
[0].equals("*")) {
98 List
<MetaData
> metas
= Instance
.getLibrary().getMetas(
99 createPgForwarder(action
));
100 return metas
.toArray(new MetaData
[] {});
102 throw new InvalidParameterException(
103 "only * is valid here, but you passed: " + args
[0]);
104 } else if ("GET_STORY".equals(command
)) {
105 MetaData meta
= Instance
.getLibrary().getInfo("" + args
[0]);
112 Story story
= Instance
.getLibrary().getStory("" + args
[0], null);
113 for (Object obj
: breakStory(story
)) {
117 } else if ("SAVE_STORY".equals(command
)) {
118 List
<Object
> list
= new ArrayList
<Object
>();
121 Object obj
= action
.rec();
122 while (obj
!= null) {
128 Story story
= rebuildStory(list
);
129 Instance
.getLibrary().save(story
, "" + args
[1], null);
130 } else if ("DELETE_STORY".equals(command
)) {
131 Instance
.getLibrary().delete("" + args
[0]);
132 } else if ("GET_COVER".equals(command
)) {
133 return Instance
.getLibrary().getCover("" + args
[0]);
134 } else if ("GET_SOURCE_COVER".equals(command
)) {
135 return Instance
.getLibrary().getSourceCover("" + args
[0]);
136 } else if ("SET_SOURCE_COVER".equals(command
)) {
137 Instance
.getLibrary().setSourceCover("" + args
[0], "" + args
[1]);
138 } else if ("EXIT".equals(command
)) {
146 protected void onError(Exception e
) {
147 getTraceHandler().error(e
);
151 * Break a story in multiple {@link Object}s for easier serialisation.
154 * the {@link Story} to break
156 * @return the list of {@link Object}s
158 static List
<Object
> breakStory(Story story
) {
159 List
<Object
> list
= new ArrayList
<Object
>();
161 story
= story
.clone();
164 if (story
.getMeta().isImageDocument()) {
165 for (Chapter chap
: story
) {
167 list
.addAll(chap
.getParagraphs());
168 chap
.setParagraphs(new ArrayList
<Paragraph
>());
170 story
.setChapters(new ArrayList
<Chapter
>());
177 * Rebuild a story from a list of broke up {@link Story} parts.
180 * the list of {@link Story} parts
182 * @return the reconstructed {@link Story}
184 static Story
rebuildStory(List
<Object
> list
) {
188 for (Object obj
: list
) {
189 if (obj
instanceof Story
) {
191 } else if (obj
instanceof Chapter
) {
192 chap
= (Chapter
) obj
;
193 story
.getChapters().add(chap
);
194 } else if (obj
instanceof Paragraph
) {
195 chap
.getParagraphs().add((Paragraph
) obj
);
203 * Update the {@link Progress} with the adequate {@link Object} received
204 * from the network via {@link RemoteLibraryServer}.
207 * the {@link Progress} to update
209 * the object received from the network
211 * @return TRUE if it was a progress event, FALSE if not
213 static boolean updateProgress(Progress pg
, Object rep
) {
214 if (rep
instanceof Integer
[]) {
215 Integer
[] a
= (Integer
[]) rep
;
221 if (min
>= 0 && min
<= max
) {
222 pg
.setMinMax(min
, max
);
223 pg
.setProgress(progress
);
234 * Create a {@link Progress} that will forward its progress over the
238 * the {@link ConnectActionServerObject} to use to forward it
240 * @return the {@link Progress}
242 private static Progress
createPgForwarder(
243 final ConnectActionServerObject action
) {
244 final Progress pg
= new Progress();
245 final Integer
[] p
= new Integer
[] { -1, -1, -1 };
246 pg
.addProgressListener(new ProgressListener() {
248 public void progress(Progress progress
, String name
) {
249 int min
= pg
.getMin();
250 int max
= pg
.getMax();
251 int relativeProgress
= min
252 + (int) Math
.round(pg
.getRelativeProgress()
255 // Do not re-send the same value twice over the wire
256 if (p
[0] != min
|| p
[1] != max
|| p
[2] != relativeProgress
) {
259 p
[2] = relativeProgress
;
262 action
.send(new Integer
[] { min
, max
, relativeProgress
});
264 } catch (Exception e
) {
265 Instance
.getTraceHandler().error(e
);