Commit | Line | Data |
---|---|---|
e42573a0 | 1 | package be.nikiroo.fanfix.library; |
b0e88ebd | 2 | |
68e2c6d2 | 3 | import java.awt.image.BufferedImage; |
b0e88ebd NR |
4 | import java.io.File; |
5 | import java.io.IOException; | |
68e2c6d2 NR |
6 | import java.util.ArrayList; |
7 | import java.util.List; | |
b0e88ebd | 8 | |
e42573a0 | 9 | import be.nikiroo.fanfix.Instance; |
b0e88ebd NR |
10 | import be.nikiroo.fanfix.data.MetaData; |
11 | import be.nikiroo.fanfix.data.Story; | |
12 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
13 | import be.nikiroo.utils.Progress; | |
14 | import be.nikiroo.utils.Version; | |
15 | import be.nikiroo.utils.serial.ConnectActionClient; | |
16 | ||
68e2c6d2 NR |
17 | /** |
18 | * This {@link BasicLibrary} will access a remote server to list the available | |
a85e8077 | 19 | * stories, and download the ones you try to load to the local directory |
68e2c6d2 NR |
20 | * specified in the configuration. |
21 | * | |
22 | * @author niki | |
23 | */ | |
24 | public class RemoteLibrary extends BasicLibrary { | |
b0e88ebd NR |
25 | private String host; |
26 | private int port; | |
68e2c6d2 NR |
27 | private File baseDir; |
28 | ||
29 | private LocalLibrary lib; | |
30 | private List<MetaData> metas; | |
31 | ||
32 | /** | |
33 | * Create a {@link RemoteLibrary} linked to the given server. | |
34 | * | |
35 | * @param host | |
36 | * the host to contact or NULL for localhost | |
37 | * @param port | |
38 | * the port to contact it on | |
39 | */ | |
40 | public RemoteLibrary(String host, int port) { | |
b0e88ebd NR |
41 | this.host = host; |
42 | this.port = port; | |
43 | ||
b0e88ebd NR |
44 | this.baseDir = Instance.getRemoteDir(host); |
45 | this.baseDir.mkdirs(); | |
46 | ||
68e2c6d2 NR |
47 | this.lib = new LocalLibrary(baseDir, OutputType.INFO_TEXT, |
48 | OutputType.CBZ); | |
b0e88ebd NR |
49 | } |
50 | ||
99ccbdf6 NR |
51 | @Override |
52 | public String getLibraryName() { | |
53 | return host + ":" + port; | |
54 | } | |
55 | ||
b0e88ebd | 56 | @Override |
68e2c6d2 NR |
57 | protected List<MetaData> getMetas(Progress pg) { |
58 | // TODO: progress | |
b0e88ebd | 59 | |
68e2c6d2 NR |
60 | if (metas == null) { |
61 | metas = new ArrayList<MetaData>(); | |
b0e88ebd | 62 | |
b0e88ebd NR |
63 | try { |
64 | new ConnectActionClient(host, port, true, null) { | |
211f7ddb | 65 | @Override |
b0e88ebd NR |
66 | public void action(Version serverVersion) throws Exception { |
67 | try { | |
68 | Object rep = send("GET_METADATA *"); | |
69 | for (MetaData meta : (MetaData[]) rep) { | |
68e2c6d2 | 70 | metas.add(meta); |
b0e88ebd NR |
71 | } |
72 | } catch (Exception e) { | |
68e2c6d2 | 73 | Instance.syserr(e); |
b0e88ebd NR |
74 | } |
75 | } | |
76 | }.connect(); | |
77 | } catch (IOException e) { | |
78 | Instance.syserr(e); | |
79 | } | |
80 | } | |
81 | ||
68e2c6d2 | 82 | return metas; |
b0e88ebd NR |
83 | } |
84 | ||
85 | @Override | |
86 | public synchronized File getFile(final String luid) { | |
87 | File file = lib.getFile(luid); | |
88 | if (file == null) { | |
89 | final File[] tmp = new File[1]; | |
90 | try { | |
91 | new ConnectActionClient(host, port, true, null) { | |
211f7ddb | 92 | @Override |
b0e88ebd NR |
93 | public void action(Version serverVersion) throws Exception { |
94 | try { | |
95 | Object rep = send("GET_STORY " + luid); | |
96 | Story story = (Story) rep; | |
97 | if (story != null) { | |
98 | lib.save(story, luid, null); | |
99 | tmp[0] = lib.getFile(luid); | |
100 | } | |
101 | } catch (Exception e) { | |
102 | Instance.syserr(e); | |
103 | } | |
104 | } | |
105 | }.connect(); | |
106 | } catch (IOException e) { | |
107 | Instance.syserr(e); | |
108 | } | |
109 | ||
110 | file = tmp[0]; | |
111 | } | |
112 | ||
113 | if (file != null) { | |
114 | MetaData meta = getInfo(luid); | |
68e2c6d2 | 115 | metas.add(meta); |
b0e88ebd NR |
116 | } |
117 | ||
118 | return file; | |
119 | } | |
68e2c6d2 NR |
120 | |
121 | @Override | |
122 | public BufferedImage getCover(String luid) { | |
123 | // Retrieve it from the network if needed: | |
124 | if (lib.getInfo(luid) == null) { | |
125 | getFile(luid); | |
126 | } | |
127 | ||
128 | return lib.getCover(luid); | |
129 | } | |
130 | ||
131 | @Override | |
132 | protected void clearCache() { | |
133 | metas = null; | |
134 | lib.clearCache(); | |
135 | } | |
136 | ||
137 | @Override | |
138 | public synchronized Story save(Story story, String luid, Progress pg) | |
139 | throws IOException { | |
140 | throw new java.lang.InternalError( | |
141 | "No write support allowed on remote Libraries"); | |
142 | } | |
143 | ||
144 | @Override | |
a85e8077 | 145 | public synchronized void delete(String luid) throws IOException { |
68e2c6d2 NR |
146 | throw new java.lang.InternalError( |
147 | "No write support allowed on remote Libraries"); | |
148 | } | |
149 | ||
150 | @Override | |
a85e8077 | 151 | public void setSourceCover(String source, String luid) { |
68e2c6d2 NR |
152 | throw new java.lang.InternalError( |
153 | "No write support allowed on remote Libraries"); | |
154 | } | |
155 | ||
a85e8077 NR |
156 | // All the following methods are only used by Save and Delete in |
157 | // BasicLibrary: | |
158 | ||
68e2c6d2 | 159 | @Override |
a85e8077 NR |
160 | protected int getNextId() { |
161 | throw new java.lang.InternalError("Should not have been called"); | |
68e2c6d2 | 162 | } |
14b57448 NR |
163 | |
164 | @Override | |
a85e8077 NR |
165 | protected void doDelete(String luid) throws IOException { |
166 | throw new java.lang.InternalError("Should not have been called"); | |
167 | } | |
168 | ||
169 | @Override | |
170 | protected Story doSave(Story story, Progress pg) throws IOException { | |
171 | throw new java.lang.InternalError("Should not have been called"); | |
14b57448 | 172 | } |
b0e88ebd | 173 | } |