Merge branch 'master' of github.com:nikiroo/fanfix
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.library;
2
3import java.io.File;
4import java.io.IOException;
5import java.net.URL;
6import java.net.UnknownHostException;
7import java.util.ArrayList;
8import java.util.List;
9
10import be.nikiroo.fanfix.Instance;
11import be.nikiroo.fanfix.data.MetaData;
12import be.nikiroo.fanfix.data.Story;
13import be.nikiroo.utils.Image;
14import be.nikiroo.utils.Progress;
15import be.nikiroo.utils.StringUtils;
16import be.nikiroo.utils.Version;
17import be.nikiroo.utils.serial.server.ConnectActionClientObject;
18
19/**
20 * This {@link BasicLibrary} will access a remote server to list the available
21 * stories, and download the ones you try to load to the local directory
22 * specified in the configuration.
23 *
24 * @author niki
25 */
26public class RemoteLibrary extends BasicLibrary {
27 private String host;
28 private int port;
29 private final String md5;
30
31 /**
32 * Create a {@link RemoteLibrary} linked to the given server.
33 *
34 * @param key
35 * the key that will allow us to exchange information with the
36 * server
37 * @param host
38 * the host to contact or NULL for localhost
39 * @param port
40 * the port to contact it on
41 */
42 public RemoteLibrary(String key, String host, int port) {
43 this.md5 = StringUtils.getMd5Hash(key);
44 this.host = host;
45 this.port = port;
46 }
47
48 @Override
49 public String getLibraryName() {
50 return host + ":" + port;
51 }
52
53 @Override
54 public Status getStatus() {
55 final Status[] result = new Status[1];
56
57 result[0] = Status.INVALID;
58
59 ConnectActionClientObject action = null;
60 try {
61 action = new ConnectActionClientObject(host, port, true) {
62 @Override
63 public void action(Version serverVersion) throws Exception {
64 Object rep = send(new Object[] { md5, "PING" });
65 if ("PONG".equals(rep)) {
66 result[0] = Status.READY;
67 } else {
68 result[0] = Status.UNAUTORIZED;
69 }
70 }
71
72 @Override
73 protected void onError(Exception e) {
74 result[0] = Status.UNAVAILABLE;
75 }
76 };
77
78 } catch (UnknownHostException e) {
79 result[0] = Status.INVALID;
80 } catch (IllegalArgumentException e) {
81 result[0] = Status.INVALID;
82 } catch (Exception e) {
83 result[0] = Status.UNAVAILABLE;
84 }
85
86 if (action != null) {
87 try {
88 action.connect();
89 } catch (Exception e) {
90 result[0] = Status.UNAVAILABLE;
91 }
92 }
93
94 return result[0];
95 }
96
97 @Override
98 public Image getCover(final String luid) {
99 final Image[] result = new Image[1];
100
101 try {
102 new ConnectActionClientObject(host, port, true) {
103 @Override
104 public void action(Version serverVersion) throws Exception {
105 Object rep = send(new Object[] { md5, "GET_COVER", luid });
106 result[0] = (Image) rep;
107 }
108
109 @Override
110 protected void onError(Exception e) {
111 Instance.getTraceHandler().error(e);
112 }
113 }.connect();
114 } catch (Exception e) {
115 Instance.getTraceHandler().error(e);
116 }
117
118 return result[0];
119 }
120
121 @Override
122 public Image getCustomSourceCover(final String source) {
123 final Image[] result = new Image[1];
124
125 try {
126 new ConnectActionClientObject(host, port, true) {
127 @Override
128 public void action(Version serverVersion) throws Exception {
129 Object rep = send(new Object[] { md5,
130 "GET_CUSTOM_SOURCE_COVER", source });
131 result[0] = (Image) rep;
132 }
133
134 @Override
135 protected void onError(Exception e) {
136 Instance.getTraceHandler().error(e);
137 }
138 }.connect();
139 } catch (Exception e) {
140 Instance.getTraceHandler().error(e);
141 }
142
143 return result[0];
144 }
145
146 @Override
147 public synchronized Story getStory(final String luid, Progress pg) {
148 final Progress pgF = pg;
149 final Story[] result = new Story[1];
150
151 try {
152 new ConnectActionClientObject(host, port, true) {
153 @Override
154 public void action(Version serverVersion) throws Exception {
155 Progress pg = pgF;
156 if (pg == null) {
157 pg = new Progress();
158 }
159
160 Object rep = send(new Object[] { md5, "GET_STORY", luid });
161
162 MetaData meta = null;
163 if (rep instanceof MetaData) {
164 meta = (MetaData) rep;
165 if (meta.getWords() <= Integer.MAX_VALUE) {
166 pg.setMinMax(0, (int) meta.getWords());
167 }
168 }
169
170 List<Object> list = new ArrayList<Object>();
171 for (Object obj = send(null); obj != null; obj = send(null)) {
172 list.add(obj);
173 pg.add(1);
174 }
175
176 result[0] = RemoteLibraryServer.rebuildStory(list);
177 pg.done();
178 }
179
180 @Override
181 protected void onError(Exception e) {
182 Instance.getTraceHandler().error(e);
183 }
184 }.connect();
185 } catch (Exception e) {
186 Instance.getTraceHandler().error(e);
187 }
188
189 return result[0];
190 }
191
192 @Override
193 public synchronized Story save(final Story story, final String luid,
194 Progress pg) throws IOException {
195 final String[] luidSaved = new String[1];
196 Progress pgSave = new Progress();
197 Progress pgRefresh = new Progress();
198 if (pg == null) {
199 pg = new Progress();
200 }
201
202 pg.setMinMax(0, 10);
203 pg.addProgress(pgSave, 9);
204 pg.addProgress(pgRefresh, 1);
205
206 final Progress pgF = pgSave;
207
208 new ConnectActionClientObject(host, port, true) {
209 @Override
210 public void action(Version serverVersion) throws Exception {
211 Progress pg = pgF;
212 if (story.getMeta().getWords() <= Integer.MAX_VALUE) {
213 pg.setMinMax(0, (int) story.getMeta().getWords());
214 }
215
216 send(new Object[] { md5, "SAVE_STORY", luid });
217
218 List<Object> list = RemoteLibraryServer.breakStory(story);
219 for (Object obj : list) {
220 send(obj);
221 pg.add(1);
222 }
223
224 luidSaved[0] = (String) send(null);
225
226 pg.done();
227 }
228
229 @Override
230 protected void onError(Exception e) {
231 Instance.getTraceHandler().error(e);
232 }
233 }.connect();
234
235 // because the meta changed:
236 MetaData meta = getInfo(luidSaved[0]);
237 if (story.getMeta().getClass() != null) {
238 // If already available locally:
239 meta.setCover(story.getMeta().getCover());
240 } else {
241 // If required:
242 meta.setCover(getCover(meta.getLuid()));
243 }
244 story.setMeta(meta);
245
246 pg.done();
247
248 return story;
249 }
250
251 @Override
252 public synchronized void delete(final String luid) throws IOException {
253 new ConnectActionClientObject(host, port, true) {
254 @Override
255 public void action(Version serverVersion) throws Exception {
256 send(new Object[] { md5, "DELETE_STORY", luid });
257 }
258
259 @Override
260 protected void onError(Exception e) {
261 Instance.getTraceHandler().error(e);
262 }
263 }.connect();
264 }
265
266 @Override
267 public void setSourceCover(final String source, final String luid) {
268 try {
269 new ConnectActionClientObject(host, port, true) {
270 @Override
271 public void action(Version serverVersion) throws Exception {
272 send(new Object[] { md5, "SET_SOURCE_COVER", source, luid });
273 }
274
275 @Override
276 protected void onError(Exception e) {
277 Instance.getTraceHandler().error(e);
278 }
279 }.connect();
280 } catch (IOException e) {
281 Instance.getTraceHandler().error(e);
282 }
283 }
284
285 @Override
286 // Could work (more slowly) without it
287 public Story imprt(final URL url, Progress pg) throws IOException {
288 // Import the file locally if it is actually a file
289 if (url == null || url.getProtocol().equalsIgnoreCase("file")) {
290 return super.imprt(url, pg);
291 }
292
293 // Import it remotely if it is an URL
294
295 if (pg == null) {
296 pg = new Progress();
297 }
298
299 pg.setMinMax(0, 2);
300 Progress pgImprt = new Progress();
301 Progress pgGet = new Progress();
302 pg.addProgress(pgImprt, 1);
303 pg.addProgress(pgGet, 1);
304
305 final Progress pgF = pgImprt;
306 final String[] luid = new String[1];
307
308 try {
309 new ConnectActionClientObject(host, port, true) {
310 @Override
311 public void action(Version serverVersion) throws Exception {
312 Progress pg = pgF;
313
314 Object rep = send(new Object[] { md5, "IMPORT",
315 url.toString() });
316
317 while (true) {
318 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
319 break;
320 }
321
322 rep = send(null);
323 }
324
325 pg.done();
326 luid[0] = (String) rep;
327 }
328
329 @Override
330 protected void onError(Exception e) {
331 Instance.getTraceHandler().error(e);
332 }
333 }.connect();
334 } catch (IOException e) {
335 Instance.getTraceHandler().error(e);
336 }
337
338 if (luid[0] == null) {
339 throw new IOException("Remote failure");
340 }
341
342 Story story = getStory(luid[0], pgGet);
343 pgGet.done();
344
345 pg.done();
346 return story;
347 }
348
349 @Override
350 // Could work (more slowly) without it
351 public synchronized void changeSource(final String luid,
352 final String newSource, Progress pg) throws IOException {
353 final Progress pgF = pg == null ? new Progress() : pg;
354
355 try {
356 new ConnectActionClientObject(host, port, true) {
357 @Override
358 public void action(Version serverVersion) throws Exception {
359 Progress pg = pgF;
360
361 Object rep = send(new Object[] { md5, "CHANGE_SOURCE",
362 luid, newSource });
363 while (true) {
364 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
365 break;
366 }
367
368 rep = send(null);
369 }
370 }
371
372 @Override
373 protected void onError(Exception e) {
374 Instance.getTraceHandler().error(e);
375 }
376 }.connect();
377 } catch (IOException e) {
378 Instance.getTraceHandler().error(e);
379 }
380 }
381
382 @Override
383 public synchronized File getFile(final String luid, Progress pg) {
384 throw new java.lang.InternalError(
385 "Operation not supportorted on remote Libraries");
386 }
387
388 /**
389 * Stop the server.
390 */
391 public void exit() {
392 try {
393 new ConnectActionClientObject(host, port, true) {
394 @Override
395 public void action(Version serverVersion) throws Exception {
396 send(new Object[] { md5, "EXIT" });
397 }
398
399 @Override
400 protected void onError(Exception e) {
401 Instance.getTraceHandler().error(e);
402 }
403 }.connect();
404 } catch (IOException e) {
405 Instance.getTraceHandler().error(e);
406 }
407 }
408
409 @Override
410 public synchronized MetaData getInfo(String luid) {
411 List<MetaData> metas = getMetasList(luid, null);
412 if (!metas.isEmpty()) {
413 return metas.get(0);
414 }
415
416 return null;
417 }
418
419 @Override
420 protected List<MetaData> getMetas(Progress pg) {
421 return getMetasList("*", pg);
422 }
423
424 @Override
425 protected void updateInfo(MetaData meta) {
426 // Will be taken care of directly server side
427 }
428
429 @Override
430 protected void deleteInfo(String luid) {
431 // Will be taken care of directly server side
432 }
433
434 // The following methods are only used by Save and Delete in BasicLibrary:
435
436 @Override
437 protected int getNextId() {
438 throw new java.lang.InternalError("Should not have been called");
439 }
440
441 @Override
442 protected void doDelete(String luid) throws IOException {
443 throw new java.lang.InternalError("Should not have been called");
444 }
445
446 @Override
447 protected Story doSave(Story story, Progress pg) throws IOException {
448 throw new java.lang.InternalError("Should not have been called");
449 }
450
451 //
452
453 /**
454 * Return the meta of the given story or a list of all known metas if the
455 * luid is "*".
456 * <p>
457 * Will not get the covers.
458 *
459 * @param luid
460 * the luid of the story or *
461 * @param pg
462 * the optional progress
463 *
464 *
465 * @return the metas
466 */
467 private List<MetaData> getMetasList(final String luid, Progress pg) {
468 final Progress pgF = pg;
469 final List<MetaData> metas = new ArrayList<MetaData>();
470
471 try {
472 new ConnectActionClientObject(host, port, true) {
473 @Override
474 public void action(Version serverVersion) throws Exception {
475 Progress pg = pgF;
476 if (pg == null) {
477 pg = new Progress();
478 }
479
480 Object rep = send(new Object[] { md5, "GET_METADATA", luid });
481
482 while (true) {
483 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
484 break;
485 }
486
487 rep = send(null);
488 }
489
490 if (rep instanceof MetaData[]) {
491 for (MetaData meta : (MetaData[]) rep) {
492 metas.add(meta);
493 }
494 } else if (rep != null) {
495 metas.add((MetaData) rep);
496 }
497 }
498
499 @Override
500 protected void onError(Exception e) {
501 Instance.getTraceHandler().error(e);
502 }
503 }.connect();
504 } catch (Exception e) {
505 Instance.getTraceHandler().error(e);
506 }
507
508 return metas;
509 }
510}