1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.IOException
;
4 import java
.security
.InvalidParameterException
;
7 import be
.nikiroo
.fanfix
.Instance
;
8 import be
.nikiroo
.fanfix
.data
.MetaData
;
9 import be
.nikiroo
.fanfix
.data
.Story
;
10 import be
.nikiroo
.utils
.Version
;
11 import be
.nikiroo
.utils
.serial
.ConnectActionServer
;
12 import be
.nikiroo
.utils
.serial
.Server
;
15 * Create a new remote server that will listen for order on the given port.
17 * The available commands are given as String arrays (first item is the command,
18 * the rest are the arguments):
20 * <li>GET_METADATA *: will return the metadata of all the stories in the
22 * <li>GET_STORY [luid]: will return the given story if it exists (or NULL if
24 * <li>SAVE_STORY [story] [luid]: save the story with the given LUID</li>
25 * <li>DELETE_STORY [luid]: delete the story of LUID luid</li>
26 * <li>GET_COVER [luid]: return the cover of the story</li>
27 * <li>GET_SOURCE_COVER [source]: return the cover for this source</li>
28 * <li>SET_SOURCE_COVER [source], [luid]: set the default cover for the given
29 * source to the cover of the story denoted by luid</li>
30 * <li>EXIT: stop the server</li>
35 public class RemoteLibraryServer
extends Server
{
38 * Create a new remote server (will not be active until
39 * {@link RemoteLibraryServer#start()} is called).
42 * the port to listen on
45 * in case of I/O error
47 public RemoteLibraryServer(int port
) throws IOException
{
52 protected Object
onRequest(ConnectActionServer action
,
53 Version clientVersion
, Object data
) throws Exception
{
56 Object
[] args
= new Object
[0];
57 if (data
instanceof Object
[]) {
58 args
= (Object
[]) data
;
59 if (args
.length
> 0) {
60 command
= "" + args
[0];
64 System
.out
.print("COMMAND: ");
65 for (Object arg
: args
) {
66 System
.out
.print(arg
+ " ");
68 System
.out
.println("");
70 // TODO: progress (+send name + %age info back to client)
72 if ("GET_METADATA".equals(command
)) {
73 if (args
[1].equals("*")) {
74 List
<MetaData
> metas
= Instance
.getLibrary().getMetas(null);
75 return metas
.toArray(new MetaData
[] {});
77 throw new InvalidParameterException(
78 "only * is valid here, but you passed: " + args
[1]);
79 } else if ("GET_STORY".equals(command
)) {
80 return Instance
.getLibrary().getStory("" + args
[1], null);
81 } else if ("SAVE_STORY".equals(command
)) {
82 Instance
.getLibrary().save((Story
) args
[1], "" + args
[2], null);
83 } else if ("DELETE_STORY".equals(command
)) {
84 Instance
.getLibrary().delete("" + args
[1]);
85 } else if ("GET_COVER".equals(command
)) {
86 return Instance
.getLibrary().getCover("" + args
[1]);
87 } else if ("GET_SOURCE_COVER".equals(command
)) {
88 return Instance
.getLibrary().getSourceCover("" + args
[1]);
89 } else if ("SET_SOURCE_COVER".equals(command
)) {
90 Instance
.getLibrary().setSourceCover("" + args
[1], "" + args
[2]);
91 } else if ("EXIT".equals(command
)) {