9ecbc41ef0d413608479eda2f3bbc5768591d4d7
[fanfix.git] / src / be / nikiroo / fanfix / RemoteLibraryServer.java
1 package be.nikiroo.fanfix;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Map;
6
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 public class RemoteLibraryServer extends Server {
13
14 public RemoteLibraryServer(int port) throws IOException {
15 super(Version.getCurrentVersion(), port, true);
16 }
17
18 @Override
19 protected Object onRequest(ConnectActionServer action,
20 Version clientVersion, Object data) throws Exception {
21 String command = null;
22 String args = null;
23 if (data instanceof String) {
24 command = (String) data;
25 int pos = command.indexOf(" ");
26 if (pos >= 0) {
27 args = command.substring(pos + 1);
28 command = command.substring(0, pos);
29 }
30 }
31
32 System.out.println(String.format("COMMAND: [%s], ARGS: [%s]", command,
33 args));
34
35 if (command != null) {
36 if (command.equals("GET_METADATA")) {
37 if (args != null && args.equals("*")) {
38 Map<MetaData, File> stories = Instance.getLibrary()
39 .getStories(null);
40 return stories.keySet().toArray(new MetaData[] {});
41 }
42 } else if (command.equals("GET_STORY")) {
43 if (args != null) {
44 return Instance.getLibrary().getStory(args, null);
45 }
46 }
47 }
48
49 return null;
50 }
51 }