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; | |
e6249b0f | 6 | import java.net.UnknownHostException; |
68e2c6d2 NR |
7 | import java.util.ArrayList; |
8 | import java.util.List; | |
b0e88ebd | 9 | |
e42573a0 | 10 | import be.nikiroo.fanfix.Instance; |
b0e88ebd NR |
11 | import be.nikiroo.fanfix.data.MetaData; |
12 | import be.nikiroo.fanfix.data.Story; | |
b0e88ebd NR |
13 | import be.nikiroo.utils.Progress; |
14 | import be.nikiroo.utils.Version; | |
62c63b07 | 15 | import be.nikiroo.utils.serial.server.ConnectActionClientObject; |
b0e88ebd | 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; | |
2070ced5 | 27 | private final String key; |
68e2c6d2 NR |
28 | |
29 | /** | |
30 | * Create a {@link RemoteLibrary} linked to the given server. | |
31 | * | |
2070ced5 NR |
32 | * @param key |
33 | * the key that will allow us to exchange information with the | |
34 | * server | |
68e2c6d2 NR |
35 | * @param host |
36 | * the host to contact or NULL for localhost | |
37 | * @param port | |
38 | * the port to contact it on | |
39 | */ | |
2070ced5 NR |
40 | public RemoteLibrary(String key, String host, int port) { |
41 | this.key = key; | |
b0e88ebd NR |
42 | this.host = host; |
43 | this.port = port; | |
b0e88ebd NR |
44 | } |
45 | ||
99ccbdf6 NR |
46 | @Override |
47 | public String getLibraryName() { | |
48 | return host + ":" + port; | |
49 | } | |
50 | ||
e6249b0f NR |
51 | @Override |
52 | public Status getStatus() { | |
53 | final Status[] result = new Status[1]; | |
54 | ||
55 | result[0] = Status.INVALID; | |
56 | ||
57 | ConnectActionClientObject action = null; | |
58 | try { | |
59 | action = new ConnectActionClientObject(host, port, true) { | |
60 | @Override | |
61 | public void action(Version serverVersion) throws Exception { | |
62 | Object rep = send(new Object[] { key, "PING" }); | |
63 | if ("PONG".equals(rep)) { | |
64 | result[0] = Status.READY; | |
65 | } else { | |
66 | result[0] = Status.UNAUTORIZED; | |
67 | } | |
68 | } | |
69 | ||
70 | @Override | |
71 | protected void onError(Exception e) { | |
72 | result[0] = Status.UNAVAILABLE; | |
73 | } | |
74 | }; | |
75 | ||
76 | } catch (UnknownHostException e) { | |
77 | result[0] = Status.INVALID; | |
78 | } catch (IllegalArgumentException e) { | |
79 | result[0] = Status.INVALID; | |
80 | } catch (Exception e) { | |
81 | result[0] = Status.UNAVAILABLE; | |
82 | } | |
83 | ||
84 | if (action != null) { | |
85 | try { | |
86 | action.connect(); | |
87 | } catch (Exception e) { | |
88 | result[0] = Status.UNAVAILABLE; | |
89 | } | |
90 | } | |
91 | ||
92 | return result[0]; | |
93 | } | |
94 | ||
b0e88ebd | 95 | @Override |
b9ce9cad NR |
96 | public BufferedImage getCover(final String luid) { |
97 | final BufferedImage[] result = new BufferedImage[1]; | |
b0e88ebd | 98 | |
b9ce9cad NR |
99 | try { |
100 | new ConnectActionClientObject(host, port, true) { | |
101 | @Override | |
102 | public void action(Version serverVersion) throws Exception { | |
103 | Object rep = send(new Object[] { key, "GET_COVER", luid }); | |
104 | result[0] = (BufferedImage) rep; | |
105 | } | |
b0e88ebd | 106 | |
b9ce9cad NR |
107 | @Override |
108 | protected void onError(Exception e) { | |
109 | Instance.getTraceHandler().error(e); | |
110 | } | |
111 | }.connect(); | |
112 | } catch (Exception e) { | |
113 | Instance.getTraceHandler().error(e); | |
114 | } | |
b0e88ebd | 115 | |
b9ce9cad | 116 | return result[0]; |
085a2f9a NR |
117 | } |
118 | ||
119 | @Override | |
120 | public BufferedImage getSourceCover(final String source) { | |
b9ce9cad NR |
121 | final BufferedImage[] result = new BufferedImage[1]; |
122 | ||
123 | try { | |
124 | new ConnectActionClientObject(host, port, true) { | |
125 | @Override | |
126 | public void action(Version serverVersion) throws Exception { | |
127 | Object rep = send(new Object[] { key, "GET_SOURCE_COVER", | |
128 | source }); | |
129 | result[0] = (BufferedImage) rep; | |
130 | } | |
131 | ||
132 | @Override | |
133 | protected void onError(Exception e) { | |
134 | Instance.getTraceHandler().error(e); | |
135 | } | |
136 | }.connect(); | |
137 | } catch (Exception e) { | |
138 | Instance.getTraceHandler().error(e); | |
139 | } | |
140 | ||
141 | return result[0]; | |
b0e88ebd | 142 | } |
68e2c6d2 NR |
143 | |
144 | @Override | |
ff05b828 | 145 | public synchronized Story getStory(final String luid, Progress pg) { |
b9ce9cad NR |
146 | final Progress pgF = pg; |
147 | final Story[] result = new Story[1]; | |
68e2c6d2 | 148 | |
b9ce9cad NR |
149 | try { |
150 | new ConnectActionClientObject(host, port, true) { | |
151 | @Override | |
152 | public void action(Version serverVersion) throws Exception { | |
153 | Progress pg = pgF; | |
154 | if (pg == null) { | |
155 | pg = new Progress(); | |
156 | } | |
157 | ||
158 | Object rep = send(new Object[] { key, "GET_STORY", luid }); | |
159 | ||
160 | MetaData meta = null; | |
161 | if (rep instanceof MetaData) { | |
162 | meta = (MetaData) rep; | |
163 | if (meta.getWords() <= Integer.MAX_VALUE) { | |
164 | pg.setMinMax(0, (int) meta.getWords()); | |
165 | } | |
166 | } | |
167 | ||
168 | List<Object> list = new ArrayList<Object>(); | |
169 | for (Object obj = send(null); obj != null; obj = send(null)) { | |
170 | list.add(obj); | |
171 | pg.add(1); | |
172 | } | |
173 | ||
174 | result[0] = RemoteLibraryServer.rebuildStory(list); | |
175 | pg.done(); | |
176 | } | |
177 | ||
178 | @Override | |
179 | protected void onError(Exception e) { | |
180 | Instance.getTraceHandler().error(e); | |
181 | } | |
182 | }.connect(); | |
183 | } catch (Exception e) { | |
184 | Instance.getTraceHandler().error(e); | |
185 | } | |
186 | ||
187 | return result[0]; | |
68e2c6d2 NR |
188 | } |
189 | ||
190 | @Override | |
b9ce9cad NR |
191 | public synchronized Story save(final Story story, final String luid, |
192 | Progress pg) throws IOException { | |
193 | final Progress pgF = pg; | |
194 | ||
195 | new ConnectActionClientObject(host, port, true) { | |
196 | @Override | |
197 | public void action(Version serverVersion) throws Exception { | |
198 | Progress pg = pgF; | |
199 | if (pg == null) { | |
200 | pg = new Progress(); | |
201 | } | |
202 | ||
203 | if (story.getMeta().getWords() <= Integer.MAX_VALUE) { | |
204 | pg.setMinMax(0, (int) story.getMeta().getWords()); | |
205 | } | |
206 | ||
207 | send(new Object[] { key, "SAVE_STORY", luid }); | |
208 | ||
209 | List<Object> list = RemoteLibraryServer.breakStory(story); | |
210 | for (Object obj : list) { | |
211 | send(obj); | |
212 | pg.add(1); | |
213 | } | |
214 | ||
215 | send(null); | |
216 | pg.done(); | |
217 | } | |
218 | ||
219 | @Override | |
220 | protected void onError(Exception e) { | |
221 | Instance.getTraceHandler().error(e); | |
222 | } | |
223 | }.connect(); | |
085a2f9a NR |
224 | |
225 | // because the meta changed: | |
226 | clearCache(); | |
227 | story.setMeta(getInfo(luid)); | |
228 | ||
229 | return story; | |
68e2c6d2 NR |
230 | } |
231 | ||
232 | @Override | |
b9ce9cad NR |
233 | public synchronized void delete(final String luid) throws IOException { |
234 | new ConnectActionClientObject(host, port, true) { | |
235 | @Override | |
236 | public void action(Version serverVersion) throws Exception { | |
237 | send(new Object[] { key, "DELETE_STORY", luid }); | |
238 | } | |
239 | ||
240 | @Override | |
241 | protected void onError(Exception e) { | |
242 | Instance.getTraceHandler().error(e); | |
243 | } | |
244 | }.connect(); | |
68e2c6d2 NR |
245 | } |
246 | ||
247 | @Override | |
b9ce9cad NR |
248 | public void setSourceCover(final String source, final String luid) { |
249 | try { | |
250 | new ConnectActionClientObject(host, port, true) { | |
251 | @Override | |
252 | public void action(Version serverVersion) throws Exception { | |
253 | send(new Object[] { key, "SET_SOURCE_COVER", source, luid }); | |
254 | } | |
255 | ||
256 | @Override | |
257 | protected void onError(Exception e) { | |
258 | Instance.getTraceHandler().error(e); | |
259 | } | |
260 | }.connect(); | |
261 | } catch (IOException e) { | |
262 | Instance.getTraceHandler().error(e); | |
263 | } | |
68e2c6d2 NR |
264 | } |
265 | ||
ff05b828 | 266 | @Override |
2249988a | 267 | public synchronized File getFile(final String luid, Progress pg) { |
ff05b828 NR |
268 | throw new java.lang.InternalError( |
269 | "Operation not supportorted on remote Libraries"); | |
270 | } | |
271 | ||
14b57448 | 272 | @Override |
b9ce9cad NR |
273 | protected List<MetaData> getMetas(Progress pg) { |
274 | final Progress pgF = pg; | |
275 | final List<MetaData> metas = new ArrayList<MetaData>(); | |
74a40dfb | 276 | |
ff05b828 | 277 | try { |
62c63b07 | 278 | new ConnectActionClientObject(host, port, true) { |
ff05b828 NR |
279 | @Override |
280 | public void action(Version serverVersion) throws Exception { | |
b9ce9cad NR |
281 | Progress pg = pgF; |
282 | if (pg == null) { | |
283 | pg = new Progress(); | |
851dd538 | 284 | } |
74a40dfb | 285 | |
b9ce9cad | 286 | Object rep = send(new Object[] { key, "GET_METADATA", "*" }); |
74a40dfb | 287 | |
b9ce9cad NR |
288 | while (true) { |
289 | if (!RemoteLibraryServer.updateProgress(pg, rep)) { | |
290 | break; | |
291 | } | |
74a40dfb | 292 | |
b9ce9cad | 293 | rep = send(null); |
ff05b828 | 294 | } |
851dd538 | 295 | |
b9ce9cad NR |
296 | for (MetaData meta : (MetaData[]) rep) { |
297 | metas.add(meta); | |
298 | } | |
851dd538 NR |
299 | } |
300 | ||
301 | @Override | |
302 | protected void onError(Exception e) { | |
303 | Instance.getTraceHandler().error(e); | |
ff05b828 NR |
304 | } |
305 | }.connect(); | |
ff05b828 | 306 | } catch (Exception e) { |
62c63b07 | 307 | Instance.getTraceHandler().error(e); |
ff05b828 | 308 | } |
b9ce9cad NR |
309 | |
310 | return metas; | |
311 | } | |
312 | ||
313 | @Override | |
314 | protected void clearCache() { | |
315 | } | |
316 | ||
317 | // The following methods are only used by Save and Delete in BasicLibrary: | |
318 | ||
319 | @Override | |
320 | protected int getNextId() { | |
321 | throw new java.lang.InternalError("Should not have been called"); | |
322 | } | |
323 | ||
324 | @Override | |
325 | protected void doDelete(String luid) throws IOException { | |
326 | throw new java.lang.InternalError("Should not have been called"); | |
327 | } | |
328 | ||
329 | @Override | |
330 | protected Story doSave(Story story, Progress pg) throws IOException { | |
331 | throw new java.lang.InternalError("Should not have been called"); | |
e604986c | 332 | } |
b0e88ebd | 333 | } |