Commit | Line | Data |
---|---|---|
e42573a0 | 1 | package be.nikiroo.fanfix.library; |
b0e88ebd | 2 | |
b0e88ebd | 3 | import java.io.IOException; |
085a2f9a | 4 | import java.security.InvalidParameterException; |
68e2c6d2 | 5 | import java.util.List; |
b0e88ebd | 6 | |
e42573a0 | 7 | import be.nikiroo.fanfix.Instance; |
b0e88ebd | 8 | import be.nikiroo.fanfix.data.MetaData; |
085a2f9a | 9 | import be.nikiroo.fanfix.data.Story; |
b0e88ebd NR |
10 | import be.nikiroo.utils.Version; |
11 | import be.nikiroo.utils.serial.ConnectActionServer; | |
12 | import be.nikiroo.utils.serial.Server; | |
13 | ||
a85e8077 NR |
14 | /** |
15 | * Create a new remote server that will listen for order on the given port. | |
16 | * <p> | |
085a2f9a NR |
17 | * The available commands are given as String arrays (first item is the command, |
18 | * the rest are the arguments): | |
a85e8077 | 19 | * <ul> |
085a2f9a NR |
20 | * <li>GET_METADATA *: will return the metadata of all the stories in the |
21 | * library</li> | |
a85e8077 NR |
22 | * <li>GET_STORY [luid]: will return the given story if it exists (or NULL if |
23 | * not)</li> | |
085a2f9a NR |
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> | |
a85e8077 NR |
30 | * </ul> |
31 | * | |
32 | * @author niki | |
33 | */ | |
b0e88ebd NR |
34 | public class RemoteLibraryServer extends Server { |
35 | ||
a85e8077 NR |
36 | /** |
37 | * Create a new remote server (will not be active until | |
38 | * {@link RemoteLibraryServer#start()} is called). | |
39 | * | |
40 | * @param port | |
41 | * the port to listen on | |
42 | * | |
43 | * @throws IOException | |
44 | * in case of I/O error | |
45 | */ | |
b0e88ebd | 46 | public RemoteLibraryServer(int port) throws IOException { |
a85e8077 | 47 | super(port, true); |
b0e88ebd NR |
48 | } |
49 | ||
50 | @Override | |
51 | protected Object onRequest(ConnectActionServer action, | |
52 | Version clientVersion, Object data) throws Exception { | |
085a2f9a NR |
53 | |
54 | String command = ""; | |
55 | Object[] args = new Object[0]; | |
56 | if (data instanceof Object[]) { | |
57 | args = (Object[]) data; | |
58 | if (args.length > 0) { | |
59 | command = "" + args[0]; | |
b0e88ebd NR |
60 | } |
61 | } | |
62 | ||
085a2f9a NR |
63 | System.out.print("COMMAND: "); |
64 | for (Object arg : args) { | |
65 | System.out.print(arg + " "); | |
66 | } | |
67 | System.out.println(""); | |
b0e88ebd | 68 | |
a85e8077 NR |
69 | // TODO: progress (+send name + %age info back to client) |
70 | ||
71 | if ("GET_METADATA".equals(command)) { | |
085a2f9a | 72 | if (args[1].equals("*")) { |
a85e8077 NR |
73 | List<MetaData> metas = Instance.getLibrary().getMetas(null); |
74 | return metas.toArray(new MetaData[] {}); | |
75 | } | |
085a2f9a NR |
76 | throw new InvalidParameterException( |
77 | "only * is valid here, but you passed: " + args[1]); | |
a85e8077 | 78 | } else if ("GET_STORY".equals(command)) { |
085a2f9a NR |
79 | return Instance.getLibrary().getStory("" + args[1], null); |
80 | } else if ("SAVE_STORY".equals(command)) { | |
81 | Instance.getLibrary().save((Story) args[1], "" + args[2], null); | |
82 | } else if ("DELETE_STORY".equals(command)) { | |
83 | Instance.getLibrary().delete("" + args[1]); | |
e604986c | 84 | } else if ("GET_COVER".equals(command)) { |
085a2f9a NR |
85 | return Instance.getLibrary().getCover("" + args[1]); |
86 | } else if ("GET_SOURCE_COVER".equals(command)) { | |
87 | return Instance.getLibrary().getSourceCover("" + args[1]); | |
88 | } else if ("SET_SOURCE_COVER".equals(command)) { | |
89 | Instance.getLibrary().setSourceCover("" + args[1], "" + args[2]); | |
b0e88ebd NR |
90 | } |
91 | ||
92 | return null; | |
93 | } | |
94 | } |