Remote: new command "EXIT"
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibraryServer.java
CommitLineData
e42573a0 1package be.nikiroo.fanfix.library;
b0e88ebd 2
b0e88ebd 3import java.io.IOException;
085a2f9a 4import java.security.InvalidParameterException;
68e2c6d2 5import java.util.List;
b0e88ebd 6
e42573a0 7import be.nikiroo.fanfix.Instance;
b0e88ebd 8import be.nikiroo.fanfix.data.MetaData;
085a2f9a 9import be.nikiroo.fanfix.data.Story;
b0e88ebd
NR
10import be.nikiroo.utils.Version;
11import be.nikiroo.utils.serial.ConnectActionServer;
12import 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>
5e848e6a 30 * <li>EXIT: stop the server</li>
a85e8077
NR
31 * </ul>
32 *
33 * @author niki
34 */
b0e88ebd
NR
35public class RemoteLibraryServer extends Server {
36
a85e8077
NR
37 /**
38 * Create a new remote server (will not be active until
39 * {@link RemoteLibraryServer#start()} is called).
40 *
41 * @param port
42 * the port to listen on
43 *
44 * @throws IOException
45 * in case of I/O error
46 */
b0e88ebd 47 public RemoteLibraryServer(int port) throws IOException {
a85e8077 48 super(port, true);
b0e88ebd
NR
49 }
50
51 @Override
52 protected Object onRequest(ConnectActionServer action,
53 Version clientVersion, Object data) throws Exception {
085a2f9a
NR
54
55 String command = "";
56 Object[] args = new Object[0];
57 if (data instanceof Object[]) {
58 args = (Object[]) data;
59 if (args.length > 0) {
60 command = "" + args[0];
b0e88ebd
NR
61 }
62 }
63
085a2f9a
NR
64 System.out.print("COMMAND: ");
65 for (Object arg : args) {
66 System.out.print(arg + " ");
67 }
68 System.out.println("");
b0e88ebd 69
a85e8077
NR
70 // TODO: progress (+send name + %age info back to client)
71
72 if ("GET_METADATA".equals(command)) {
085a2f9a 73 if (args[1].equals("*")) {
a85e8077
NR
74 List<MetaData> metas = Instance.getLibrary().getMetas(null);
75 return metas.toArray(new MetaData[] {});
76 }
085a2f9a
NR
77 throw new InvalidParameterException(
78 "only * is valid here, but you passed: " + args[1]);
a85e8077 79 } else if ("GET_STORY".equals(command)) {
085a2f9a
NR
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]);
e604986c 85 } else if ("GET_COVER".equals(command)) {
085a2f9a
NR
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]);
5e848e6a
NR
91 } else if ("EXIT".equals(command)) {
92 stop(0, false);
b0e88ebd
NR
93 }
94
95 return null;
96 }
97}