Commit | Line | Data |
---|---|---|
b0e88ebd NR |
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.fanfix.data.Story; | |
9 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
10 | import be.nikiroo.utils.Progress; | |
11 | import be.nikiroo.utils.Version; | |
12 | import be.nikiroo.utils.serial.ConnectActionClient; | |
13 | ||
14 | public class RemoteLibrary extends Library { | |
15 | private String host; | |
16 | private int port; | |
17 | ||
18 | private Library lib; | |
19 | ||
20 | public RemoteLibrary(String host, int port) throws IOException { | |
21 | this.host = host; | |
22 | this.port = port; | |
23 | ||
24 | this.localSpeed = false; | |
25 | this.baseDir = Instance.getRemoteDir(host); | |
26 | this.baseDir.mkdirs(); | |
27 | ||
28 | this.lib = new Library(baseDir, OutputType.INFO_TEXT, OutputType.CBZ); | |
29 | } | |
30 | ||
31 | @Override | |
32 | public synchronized Story save(Story story, String luid, Progress pg) | |
33 | throws IOException { | |
34 | throw new java.lang.InternalError( | |
35 | "No write support allowed on remote Libraries"); | |
36 | } | |
37 | ||
38 | @Override | |
39 | public synchronized boolean delete(String luid) { | |
40 | throw new java.lang.InternalError( | |
41 | "No write support allowed on remote Libraries"); | |
42 | } | |
43 | ||
44 | @Override | |
45 | public synchronized boolean changeType(String luid, String newType) { | |
46 | throw new java.lang.InternalError( | |
47 | "No write support allowed on remote Libraries"); | |
48 | } | |
49 | ||
50 | @Override | |
51 | protected synchronized Map<MetaData, File> getStories(Progress pg) { | |
52 | // TODO: progress | |
53 | if (stories.isEmpty()) { | |
54 | try { | |
55 | new ConnectActionClient(host, port, true, null) { | |
56 | public void action(Version serverVersion) throws Exception { | |
57 | try { | |
58 | Object rep = send("GET_METADATA *"); | |
59 | for (MetaData meta : (MetaData[]) rep) { | |
60 | stories.put(meta, null); | |
61 | } | |
62 | } catch (Exception e) { | |
63 | e.printStackTrace(); | |
64 | } | |
65 | } | |
66 | }.connect(); | |
67 | } catch (IOException e) { | |
68 | Instance.syserr(e); | |
69 | } | |
70 | } | |
71 | ||
72 | return stories; | |
73 | } | |
74 | ||
75 | @Override | |
76 | public synchronized File getFile(final String luid) { | |
77 | File file = lib.getFile(luid); | |
78 | if (file == null) { | |
79 | final File[] tmp = new File[1]; | |
80 | try { | |
81 | new ConnectActionClient(host, port, true, null) { | |
82 | public void action(Version serverVersion) throws Exception { | |
83 | try { | |
84 | Object rep = send("GET_STORY " + luid); | |
85 | Story story = (Story) rep; | |
86 | if (story != null) { | |
87 | lib.save(story, luid, null); | |
88 | tmp[0] = lib.getFile(luid); | |
89 | } | |
90 | } catch (Exception e) { | |
91 | Instance.syserr(e); | |
92 | } | |
93 | } | |
94 | }.connect(); | |
95 | } catch (IOException e) { | |
96 | Instance.syserr(e); | |
97 | } | |
98 | ||
99 | file = tmp[0]; | |
100 | } | |
101 | ||
102 | if (file != null) { | |
103 | MetaData meta = getInfo(luid); | |
104 | stories.put(meta, file); | |
105 | } | |
106 | ||
107 | return file; | |
108 | } | |
109 | } |