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] PING: will return PONG if the key is accepted</li>
29 * <li>[key] GET_METADATA *: will return the metadata of all the stories in the
31 * <li>[key] GET_STORY [luid]: will return the given story if it exists (or NULL
33 * <li>[key] SAVE_STORY [luid]: save the story (that must be sent just after the
34 * command) with the given LUID</li>
35 * <li>[key] DELETE_STORY [luid]: delete the story of LUID luid</li>
36 * <li>[key] GET_COVER [luid]: return the cover of the story</li>
37 * <li>[key] GET_SOURCE_COVER [source]: return the cover for this source</li>
38 * <li>[key] SET_SOURCE_COVER [source], [luid]: set the default cover for the
39 * given source to the cover of the story denoted by luid</li>
40 * <li>[key] EXIT: stop the server</li>
45 public class RemoteLibraryServer
extends ServerObject
{
46 private final String key
;
49 * Create a new remote server (will not be active until
50 * {@link RemoteLibraryServer#start()} is called).
53 * the key that will restrict access to this server
55 * the port to listen on
58 * in case of I/O error
60 public RemoteLibraryServer(String key
, int port
) throws IOException
{
61 super("Fanfix remote library", port
, true);
64 setTraceHandler(Instance
.getTraceHandler());
68 protected Object
onRequest(ConnectActionServerObject action
,
69 Version clientVersion
, Object data
) throws Exception
{
72 Object
[] args
= new Object
[0];
73 if (data
instanceof Object
[]) {
74 Object
[] dataArray
= (Object
[]) data
;
75 if (dataArray
.length
>= 2) {
76 args
= new Object
[dataArray
.length
- 2];
77 for (int i
= 2; i
< dataArray
.length
; i
++) {
78 args
[i
- 2] = dataArray
[i
];
81 key
= "" + dataArray
[0];
82 command
= "" + dataArray
[1];
86 String trace
= "[" + command
+ "] ";
87 for (Object arg
: args
) {
90 getTraceHandler().trace(trace
);
92 if (!key
.equals(this.key
)) {
93 getTraceHandler().trace("Key rejected.");
97 if ("PING".equals(command
)) {
99 } else if ("GET_METADATA".equals(command
)) {
100 if (args
[0].equals("*")) {
101 List
<MetaData
> metas
= Instance
.getLibrary().getMetas(
102 createPgForwarder(action
));
103 return metas
.toArray(new MetaData
[] {});
105 throw new InvalidParameterException(
106 "only * is valid here, but you passed: " + args
[0]);
107 } else if ("GET_STORY".equals(command
)) {
108 MetaData meta
= Instance
.getLibrary().getInfo("" + args
[0]);
115 Story story
= Instance
.getLibrary().getStory("" + args
[0], null);
116 for (Object obj
: breakStory(story
)) {
120 } else if ("SAVE_STORY".equals(command
)) {
121 List
<Object
> list
= new ArrayList
<Object
>();
124 Object obj
= action
.rec();
125 while (obj
!= null) {
131 Story story
= rebuildStory(list
);
132 Instance
.getLibrary().save(story
, "" + args
[1], null);
133 } else if ("DELETE_STORY".equals(command
)) {
134 Instance
.getLibrary().delete("" + args
[0]);
135 } else if ("GET_COVER".equals(command
)) {
136 return Instance
.getLibrary().getCover("" + args
[0]);
137 } else if ("GET_SOURCE_COVER".equals(command
)) {
138 return Instance
.getLibrary().getSourceCover("" + args
[0]);
139 } else if ("SET_SOURCE_COVER".equals(command
)) {
140 Instance
.getLibrary().setSourceCover("" + args
[0], "" + args
[1]);
141 } else if ("EXIT".equals(command
)) {
149 protected void onError(Exception e
) {
150 getTraceHandler().error(e
);
154 * Break a story in multiple {@link Object}s for easier serialisation.
157 * the {@link Story} to break
159 * @return the list of {@link Object}s
161 static List
<Object
> breakStory(Story story
) {
162 List
<Object
> list
= new ArrayList
<Object
>();
164 story
= story
.clone();
167 if (story
.getMeta().isImageDocument()) {
168 for (Chapter chap
: story
) {
170 list
.addAll(chap
.getParagraphs());
171 chap
.setParagraphs(new ArrayList
<Paragraph
>());
173 story
.setChapters(new ArrayList
<Chapter
>());
180 * Rebuild a story from a list of broke up {@link Story} parts.
183 * the list of {@link Story} parts
185 * @return the reconstructed {@link Story}
187 static Story
rebuildStory(List
<Object
> list
) {
191 for (Object obj
: list
) {
192 if (obj
instanceof Story
) {
194 } else if (obj
instanceof Chapter
) {
195 chap
= (Chapter
) obj
;
196 story
.getChapters().add(chap
);
197 } else if (obj
instanceof Paragraph
) {
198 chap
.getParagraphs().add((Paragraph
) obj
);
206 * Update the {@link Progress} with the adequate {@link Object} received
207 * from the network via {@link RemoteLibraryServer}.
210 * the {@link Progress} to update
212 * the object received from the network
214 * @return TRUE if it was a progress event, FALSE if not
216 static boolean updateProgress(Progress pg
, Object rep
) {
217 if (rep
instanceof Integer
[]) {
218 Integer
[] a
= (Integer
[]) rep
;
224 if (min
>= 0 && min
<= max
) {
225 pg
.setMinMax(min
, max
);
226 pg
.setProgress(progress
);
237 * Create a {@link Progress} that will forward its progress over the
241 * the {@link ConnectActionServerObject} to use to forward it
243 * @return the {@link Progress}
245 private static Progress
createPgForwarder(
246 final ConnectActionServerObject action
) {
247 final Progress pg
= new Progress();
248 final Integer
[] p
= new Integer
[] { -1, -1, -1 };
249 pg
.addProgressListener(new ProgressListener() {
251 public void progress(Progress progress
, String name
) {
252 int min
= pg
.getMin();
253 int max
= pg
.getMax();
254 int relativeProgress
= min
255 + (int) Math
.round(pg
.getRelativeProgress()
258 // Do not re-send the same value twice over the wire
259 if (p
[0] != min
|| p
[1] != max
|| p
[2] != relativeProgress
) {
262 p
[2] = relativeProgress
;
265 action
.send(new Integer
[] { min
, max
, relativeProgress
});
267 } catch (Exception e
) {
268 Instance
.getTraceHandler().error(e
);