gui: fix ChangeSTA, fix [unknown] author
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
CommitLineData
e42573a0 1package be.nikiroo.fanfix.library;
b0e88ebd
NR
2
3import java.io.File;
4import java.io.IOException;
edf79e5e 5import java.net.URL;
e6249b0f 6import java.net.UnknownHostException;
68e2c6d2
NR
7import java.util.ArrayList;
8import java.util.List;
b0e88ebd 9
e42573a0 10import be.nikiroo.fanfix.Instance;
b0e88ebd
NR
11import be.nikiroo.fanfix.data.MetaData;
12import be.nikiroo.fanfix.data.Story;
16a81ef7 13import be.nikiroo.utils.Image;
b0e88ebd 14import be.nikiroo.utils.Progress;
416c54f8 15import be.nikiroo.utils.StringUtils;
b0e88ebd 16import be.nikiroo.utils.Version;
62c63b07 17import be.nikiroo.utils.serial.server.ConnectActionClientObject;
b0e88ebd 18
68e2c6d2
NR
19/**
20 * This {@link BasicLibrary} will access a remote server to list the available
a85e8077 21 * stories, and download the ones you try to load to the local directory
68e2c6d2
NR
22 * specified in the configuration.
23 *
24 * @author niki
25 */
26public class RemoteLibrary extends BasicLibrary {
b0e88ebd
NR
27 private String host;
28 private int port;
416c54f8 29 private final String md5;
68e2c6d2
NR
30
31 /**
32 * Create a {@link RemoteLibrary} linked to the given server.
33 *
2070ced5
NR
34 * @param key
35 * the key that will allow us to exchange information with the
36 * server
68e2c6d2
NR
37 * @param host
38 * the host to contact or NULL for localhost
39 * @param port
40 * the port to contact it on
41 */
2070ced5 42 public RemoteLibrary(String key, String host, int port) {
416c54f8 43 this.md5 = StringUtils.getMd5Hash(key);
b0e88ebd
NR
44 this.host = host;
45 this.port = port;
b0e88ebd
NR
46 }
47
99ccbdf6
NR
48 @Override
49 public String getLibraryName() {
50 return host + ":" + port;
51 }
52
e6249b0f
NR
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 {
416c54f8 64 Object rep = send(new Object[] { md5, "PING" });
e6249b0f
NR
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
b0e88ebd 97 @Override
16a81ef7
NR
98 public Image getCover(final String luid) {
99 final Image[] result = new Image[1];
b0e88ebd 100
b9ce9cad
NR
101 try {
102 new ConnectActionClientObject(host, port, true) {
103 @Override
104 public void action(Version serverVersion) throws Exception {
416c54f8 105 Object rep = send(new Object[] { md5, "GET_COVER", luid });
16a81ef7 106 result[0] = (Image) rep;
b9ce9cad 107 }
b0e88ebd 108
b9ce9cad
NR
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 }
b0e88ebd 117
b9ce9cad 118 return result[0];
085a2f9a
NR
119 }
120
121 @Override
e1de8087 122 public Image getCustomSourceCover(final String source) {
16a81ef7 123 final Image[] result = new Image[1];
b9ce9cad
NR
124
125 try {
126 new ConnectActionClientObject(host, port, true) {
127 @Override
128 public void action(Version serverVersion) throws Exception {
e1de8087
NR
129 Object rep = send(new Object[] { md5,
130 "GET_CUSTOM_SOURCE_COVER", source });
16a81ef7 131 result[0] = (Image) rep;
b9ce9cad
NR
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];
b0e88ebd 144 }
68e2c6d2
NR
145
146 @Override
ff05b828 147 public synchronized Story getStory(final String luid, Progress pg) {
b9ce9cad
NR
148 final Progress pgF = pg;
149 final Story[] result = new Story[1];
68e2c6d2 150
b9ce9cad
NR
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
416c54f8 160 Object rep = send(new Object[] { md5, "GET_STORY", luid });
b9ce9cad
NR
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];
68e2c6d2
NR
190 }
191
192 @Override
b9ce9cad
NR
193 public synchronized Story save(final Story story, final String luid,
194 Progress pg) throws IOException {
0fa0fe95
NR
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;
b9ce9cad
NR
207
208 new ConnectActionClientObject(host, port, true) {
209 @Override
210 public void action(Version serverVersion) throws Exception {
211 Progress pg = pgF;
b9ce9cad
NR
212 if (story.getMeta().getWords() <= Integer.MAX_VALUE) {
213 pg.setMinMax(0, (int) story.getMeta().getWords());
214 }
215
416c54f8 216 send(new Object[] { md5, "SAVE_STORY", luid });
b9ce9cad
NR
217
218 List<Object> list = RemoteLibraryServer.breakStory(story);
219 for (Object obj : list) {
220 send(obj);
221 pg.add(1);
222 }
223
edf79e5e 224 luidSaved[0] = (String) send(null);
0fa0fe95 225
b9ce9cad
NR
226 pg.done();
227 }
228
229 @Override
230 protected void onError(Exception e) {
231 Instance.getTraceHandler().error(e);
232 }
233 }.connect();
085a2f9a
NR
234
235 // because the meta changed:
edf79e5e 236 MetaData meta = getInfo(luidSaved[0]);
efa3c511
NR
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 }
edf79e5e 244 story.setMeta(meta);
0fa0fe95
NR
245
246 pg.done();
085a2f9a
NR
247
248 return story;
68e2c6d2
NR
249 }
250
251 @Override
b9ce9cad
NR
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 {
416c54f8 256 send(new Object[] { md5, "DELETE_STORY", luid });
b9ce9cad
NR
257 }
258
259 @Override
260 protected void onError(Exception e) {
261 Instance.getTraceHandler().error(e);
262 }
263 }.connect();
68e2c6d2
NR
264 }
265
266 @Override
b9ce9cad
NR
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 {
416c54f8 272 send(new Object[] { md5, "SET_SOURCE_COVER", source, luid });
b9ce9cad
NR
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);
edf79e5e
NR
282 }
283 }
284
285 @Override
286 // Could work (more slowly) without it
287 public Story imprt(final URL url, Progress pg) throws IOException {
00f6344a
NR
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
edf79e5e
NR
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
c8d48938
NR
351 protected synchronized void changeSTA(final String luid,
352 final String newSource, final String newTitle,
353 final String newAuthor, Progress pg) throws IOException {
edf79e5e
NR
354 final Progress pgF = pg == null ? new Progress() : pg;
355
356 try {
357 new ConnectActionClientObject(host, port, true) {
358 @Override
359 public void action(Version serverVersion) throws Exception {
360 Progress pg = pgF;
361
c8d48938
NR
362 Object rep = send(new Object[] { md5, "CHANGE_STA", luid,
363 newSource, newTitle, newAuthor });
edf79e5e
NR
364 while (true) {
365 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
366 break;
367 }
368
369 rep = send(null);
370 }
371 }
372
373 @Override
374 protected void onError(Exception e) {
375 Instance.getTraceHandler().error(e);
376 }
377 }.connect();
378 } catch (IOException e) {
379 Instance.getTraceHandler().error(e);
b9ce9cad 380 }
68e2c6d2
NR
381 }
382
ff05b828 383 @Override
2249988a 384 public synchronized File getFile(final String luid, Progress pg) {
ff05b828
NR
385 throw new java.lang.InternalError(
386 "Operation not supportorted on remote Libraries");
387 }
388
468b960b
NR
389 /**
390 * Stop the server.
391 */
392 public void exit() {
393 try {
394 new ConnectActionClientObject(host, port, true) {
395 @Override
396 public void action(Version serverVersion) throws Exception {
397 send(new Object[] { md5, "EXIT" });
398 }
399
400 @Override
401 protected void onError(Exception e) {
402 Instance.getTraceHandler().error(e);
403 }
404 }.connect();
405 } catch (IOException e) {
406 Instance.getTraceHandler().error(e);
407 }
408 }
409
e272f05f
NR
410 @Override
411 public synchronized MetaData getInfo(String luid) {
412 List<MetaData> metas = getMetasList(luid, null);
413 if (!metas.isEmpty()) {
414 return metas.get(0);
415 }
416
417 return null;
418 }
419
14b57448 420 @Override
b9ce9cad 421 protected List<MetaData> getMetas(Progress pg) {
e272f05f
NR
422 return getMetasList("*", pg);
423 }
424
425 @Override
efa3c511
NR
426 protected void updateInfo(MetaData meta) {
427 // Will be taken care of directly server side
428 }
429
430 @Override
c8d48938 431 protected void invalidateInfo(String luid) {
efa3c511 432 // Will be taken care of directly server side
e272f05f
NR
433 }
434
435 // The following methods are only used by Save and Delete in BasicLibrary:
436
437 @Override
438 protected int getNextId() {
439 throw new java.lang.InternalError("Should not have been called");
440 }
441
442 @Override
443 protected void doDelete(String luid) throws IOException {
444 throw new java.lang.InternalError("Should not have been called");
445 }
446
447 @Override
448 protected Story doSave(Story story, Progress pg) throws IOException {
449 throw new java.lang.InternalError("Should not have been called");
450 }
451
452 //
453
454 /**
455 * Return the meta of the given story or a list of all known metas if the
456 * luid is "*".
9f51d8ab
NR
457 * <p>
458 * Will not get the covers.
e272f05f
NR
459 *
460 * @param luid
461 * the luid of the story or *
462 * @param pg
463 * the optional progress
464 *
465 *
466 * @return the metas
467 */
468 private List<MetaData> getMetasList(final String luid, Progress pg) {
b9ce9cad
NR
469 final Progress pgF = pg;
470 final List<MetaData> metas = new ArrayList<MetaData>();
74a40dfb 471
ff05b828 472 try {
62c63b07 473 new ConnectActionClientObject(host, port, true) {
ff05b828
NR
474 @Override
475 public void action(Version serverVersion) throws Exception {
b9ce9cad
NR
476 Progress pg = pgF;
477 if (pg == null) {
478 pg = new Progress();
851dd538 479 }
74a40dfb 480
e272f05f 481 Object rep = send(new Object[] { md5, "GET_METADATA", luid });
74a40dfb 482
b9ce9cad
NR
483 while (true) {
484 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
485 break;
486 }
74a40dfb 487
b9ce9cad 488 rep = send(null);
ff05b828 489 }
851dd538 490
e272f05f
NR
491 if (rep instanceof MetaData[]) {
492 for (MetaData meta : (MetaData[]) rep) {
493 metas.add(meta);
494 }
495 } else if (rep != null) {
496 metas.add((MetaData) rep);
b9ce9cad 497 }
851dd538
NR
498 }
499
500 @Override
501 protected void onError(Exception e) {
502 Instance.getTraceHandler().error(e);
ff05b828
NR
503 }
504 }.connect();
ff05b828 505 } catch (Exception e) {
62c63b07 506 Instance.getTraceHandler().error(e);
ff05b828 507 }
b9ce9cad
NR
508
509 return metas;
510 }
b0e88ebd 511}