Prepare work on RemoteLibrary
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibraryServer.java
1 package be.nikiroo.fanfix.library;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import be.nikiroo.fanfix.Instance;
7 import be.nikiroo.fanfix.data.MetaData;
8 import be.nikiroo.utils.Version;
9 import be.nikiroo.utils.serial.ConnectActionServer;
10 import be.nikiroo.utils.serial.Server;
11
12 /**
13 * Create a new remote server that will listen for order on the given port.
14 * <p>
15 * The available commands are:
16 * <ul>
17 * <li>GET_METADATA *: will get the metadata of all the stories in the library</li>
18 * <li>GET_STORY [luid]: will return the given story if it exists (or NULL if
19 * not)</li>
20 * </ul>
21 *
22 * @author niki
23 */
24 public class RemoteLibraryServer extends Server {
25
26 /**
27 * Create a new remote server (will not be active until
28 * {@link RemoteLibraryServer#start()} is called).
29 *
30 * @param port
31 * the port to listen on
32 *
33 * @throws IOException
34 * in case of I/O error
35 */
36 public RemoteLibraryServer(int port) throws IOException {
37 super(port, true);
38 }
39
40 @Override
41 protected Object onRequest(ConnectActionServer action,
42 Version clientVersion, Object data) throws Exception {
43 String command = null;
44 String args = null;
45 if (data instanceof String) {
46 command = (String) data;
47 int pos = command.indexOf(" ");
48 if (pos >= 0) {
49 args = command.substring(pos + 1);
50 command = command.substring(0, pos);
51 }
52 }
53
54 System.out.println(String.format("COMMAND: [%s], ARGS: [%s]", command,
55 args));
56
57 // TODO: progress (+send name + %age info back to client)
58
59 if ("GET_METADATA".equals(command)) {
60 if (args != null && args.equals("*")) {
61 List<MetaData> metas = Instance.getLibrary().getMetas(null);
62 return metas.toArray(new MetaData[] {});
63 }
64 } else if ("GET_STORY".equals(command)) {
65 if (args != null) {
66 return Instance.getLibrary().getStory(args, null);
67 }
68 }
69
70 return null;
71 }
72 }