Commit | Line | Data |
---|---|---|
e42573a0 | 1 | package be.nikiroo.fanfix.library; |
b0e88ebd | 2 | |
b0e88ebd | 3 | import java.io.IOException; |
68e2c6d2 | 4 | import java.util.List; |
b0e88ebd | 5 | |
e42573a0 | 6 | import be.nikiroo.fanfix.Instance; |
b0e88ebd NR |
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 | ||
a85e8077 NR |
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 | */ | |
b0e88ebd NR |
24 | public class RemoteLibraryServer extends Server { |
25 | ||
a85e8077 NR |
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 | */ | |
b0e88ebd | 36 | public RemoteLibraryServer(int port) throws IOException { |
a85e8077 | 37 | super(port, true); |
b0e88ebd NR |
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 | ||
a85e8077 NR |
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); | |
b0e88ebd | 67 | } |
e604986c NR |
68 | } else if ("GET_COVER".equals(command)) { |
69 | if (args != null) { | |
70 | return Instance.getLibrary().getCover(args); | |
71 | } | |
b0e88ebd NR |
72 | } |
73 | ||
74 | return null; | |
75 | } | |
76 | } |