1 package be
.nikiroo
.fanfix
.library
;
4 import java
.io
.IOException
;
6 import java
.net
.UnknownHostException
;
7 import java
.util
.ArrayList
;
10 import be
.nikiroo
.fanfix
.Instance
;
11 import be
.nikiroo
.fanfix
.data
.MetaData
;
12 import be
.nikiroo
.fanfix
.data
.Story
;
13 import be
.nikiroo
.utils
.Image
;
14 import be
.nikiroo
.utils
.Progress
;
15 import be
.nikiroo
.utils
.StringUtils
;
16 import be
.nikiroo
.utils
.Version
;
17 import be
.nikiroo
.utils
.serial
.server
.ConnectActionClientObject
;
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.
26 public class RemoteLibrary
extends BasicLibrary
{
29 private final String md5
;
32 * Create a {@link RemoteLibrary} linked to the given server.
35 * the key that will allow us to exchange information with the
38 * the host to contact or NULL for localhost
40 * the port to contact it on
42 public RemoteLibrary(String key
, String host
, int port
) {
43 this.md5
= StringUtils
.getMd5Hash(key
);
49 public String
getLibraryName() {
50 return host
+ ":" + port
;
54 public Status
getStatus() {
55 final Status
[] result
= new Status
[1];
57 result
[0] = Status
.INVALID
;
59 ConnectActionClientObject action
= null;
61 action
= new ConnectActionClientObject(host
, port
, true) {
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
;
68 result
[0] = Status
.UNAUTORIZED
;
73 protected void onError(Exception e
) {
74 result
[0] = Status
.UNAVAILABLE
;
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
;
89 } catch (Exception e
) {
90 result
[0] = Status
.UNAVAILABLE
;
98 public Image
getCover(final String luid
) {
99 final Image
[] result
= new Image
[1];
102 new ConnectActionClientObject(host
, port
, true) {
104 public void action(Version serverVersion
) throws Exception
{
105 Object rep
= send(new Object
[] { md5
, "GET_COVER", luid
});
106 result
[0] = (Image
) rep
;
110 protected void onError(Exception e
) {
111 Instance
.getTraceHandler().error(e
);
114 } catch (Exception e
) {
115 Instance
.getTraceHandler().error(e
);
122 public Image
getCustomSourceCover(final String source
) {
123 return getCustomCover(source
, "SOURCE");
127 public Image
getCustomAuthorCover(final String author
) {
128 return getCustomCover(author
, "AUTHOR");
131 // type: "SOURCE" or "AUTHOR"
132 private Image
getCustomCover(final String source
, final String type
) {
133 final Image
[] result
= new Image
[1];
136 new ConnectActionClientObject(host
, port
, true) {
138 public void action(Version serverVersion
) throws Exception
{
139 Object rep
= send(new Object
[] { md5
, "GET_CUSTOM_COVER",
141 result
[0] = (Image
) rep
;
145 protected void onError(Exception e
) {
146 Instance
.getTraceHandler().error(e
);
149 } catch (Exception e
) {
150 Instance
.getTraceHandler().error(e
);
157 public synchronized Story
getStory(final String luid
, Progress pg
) {
158 final Progress pgF
= pg
;
159 final Story
[] result
= new Story
[1];
162 new ConnectActionClientObject(host
, port
, true) {
164 public void action(Version serverVersion
) throws Exception
{
170 Object rep
= send(new Object
[] { md5
, "GET_STORY", luid
});
172 MetaData meta
= null;
173 if (rep
instanceof MetaData
) {
174 meta
= (MetaData
) rep
;
175 if (meta
.getWords() <= Integer
.MAX_VALUE
) {
176 pg
.setMinMax(0, (int) meta
.getWords());
180 List
<Object
> list
= new ArrayList
<Object
>();
181 for (Object obj
= send(null); obj
!= null; obj
= send(null)) {
186 result
[0] = RemoteLibraryServer
.rebuildStory(list
);
191 protected void onError(Exception e
) {
192 Instance
.getTraceHandler().error(e
);
195 } catch (Exception e
) {
196 Instance
.getTraceHandler().error(e
);
203 public synchronized Story
save(final Story story
, final String luid
,
204 Progress pg
) throws IOException
{
205 final String
[] luidSaved
= new String
[1];
206 Progress pgSave
= new Progress();
207 Progress pgRefresh
= new Progress();
213 pg
.addProgress(pgSave
, 9);
214 pg
.addProgress(pgRefresh
, 1);
216 final Progress pgF
= pgSave
;
218 new ConnectActionClientObject(host
, port
, true) {
220 public void action(Version serverVersion
) throws Exception
{
222 if (story
.getMeta().getWords() <= Integer
.MAX_VALUE
) {
223 pg
.setMinMax(0, (int) story
.getMeta().getWords());
226 send(new Object
[] { md5
, "SAVE_STORY", luid
});
228 List
<Object
> list
= RemoteLibraryServer
.breakStory(story
);
229 for (Object obj
: list
) {
234 luidSaved
[0] = (String
) send(null);
240 protected void onError(Exception e
) {
241 Instance
.getTraceHandler().error(e
);
245 // because the meta changed:
246 MetaData meta
= getInfo(luidSaved
[0]);
247 if (story
.getMeta().getClass() != null) {
248 // If already available locally:
249 meta
.setCover(story
.getMeta().getCover());
252 meta
.setCover(getCover(meta
.getLuid()));
262 public synchronized void delete(final String luid
) throws IOException
{
263 new ConnectActionClientObject(host
, port
, true) {
265 public void action(Version serverVersion
) throws Exception
{
266 send(new Object
[] { md5
, "DELETE_STORY", luid
});
270 protected void onError(Exception e
) {
271 Instance
.getTraceHandler().error(e
);
277 public void setSourceCover(final String source
, final String luid
) {
278 setCover(source
, luid
, "SOURCE");
282 public void setAuthorCover(final String author
, final String luid
) {
283 setCover(author
, luid
, "AUTHOR");
286 // type = "SOURCE" | "AUTHOR"
287 private void setCover(final String value
, final String luid
,
290 new ConnectActionClientObject(host
, port
, true) {
292 public void action(Version serverVersion
) throws Exception
{
293 send(new Object
[] { md5
, "SET_COVER", type
, value
, luid
});
297 protected void onError(Exception e
) {
298 Instance
.getTraceHandler().error(e
);
301 } catch (IOException e
) {
302 Instance
.getTraceHandler().error(e
);
307 // Could work (more slowly) without it
308 public Story
imprt(final URL url
, Progress pg
) throws IOException
{
309 // Import the file locally if it is actually a file
310 if (url
== null || url
.getProtocol().equalsIgnoreCase("file")) {
311 return super.imprt(url
, pg
);
314 // Import it remotely if it is an URL
321 Progress pgImprt
= new Progress();
322 Progress pgGet
= new Progress();
323 pg
.addProgress(pgImprt
, 1);
324 pg
.addProgress(pgGet
, 1);
326 final Progress pgF
= pgImprt
;
327 final String
[] luid
= new String
[1];
330 new ConnectActionClientObject(host
, port
, true) {
332 public void action(Version serverVersion
) throws Exception
{
335 Object rep
= send(new Object
[] { md5
, "IMPORT",
339 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
347 luid
[0] = (String
) rep
;
351 protected void onError(Exception e
) {
352 Instance
.getTraceHandler().error(e
);
355 } catch (IOException e
) {
356 Instance
.getTraceHandler().error(e
);
359 if (luid
[0] == null) {
360 throw new IOException("Remote failure");
363 Story story
= getStory(luid
[0], pgGet
);
371 // Could work (more slowly) without it
372 protected synchronized void changeSTA(final String luid
,
373 final String newSource
, final String newTitle
,
374 final String newAuthor
, Progress pg
) throws IOException
{
375 final Progress pgF
= pg
== null ?
new Progress() : pg
;
378 new ConnectActionClientObject(host
, port
, true) {
380 public void action(Version serverVersion
) throws Exception
{
383 Object rep
= send(new Object
[] { md5
, "CHANGE_STA", luid
,
384 newSource
, newTitle
, newAuthor
});
386 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
395 protected void onError(Exception e
) {
396 Instance
.getTraceHandler().error(e
);
399 } catch (IOException e
) {
400 Instance
.getTraceHandler().error(e
);
405 public synchronized File
getFile(final String luid
, Progress pg
) {
406 throw new java
.lang
.InternalError(
407 "Operation not supportorted on remote Libraries");
415 new ConnectActionClientObject(host
, port
, true) {
417 public void action(Version serverVersion
) throws Exception
{
418 send(new Object
[] { md5
, "EXIT" });
422 protected void onError(Exception e
) {
423 Instance
.getTraceHandler().error(e
);
426 } catch (IOException e
) {
427 Instance
.getTraceHandler().error(e
);
432 public synchronized MetaData
getInfo(String luid
) {
433 List
<MetaData
> metas
= getMetasList(luid
, null);
434 if (!metas
.isEmpty()) {
442 protected List
<MetaData
> getMetas(Progress pg
) {
443 return getMetasList("*", pg
);
447 protected void updateInfo(MetaData meta
) {
448 // Will be taken care of directly server side
452 protected void invalidateInfo(String luid
) {
453 // Will be taken care of directly server side
456 // The following methods are only used by Save and Delete in BasicLibrary:
459 protected int getNextId() {
460 throw new java
.lang
.InternalError("Should not have been called");
464 protected void doDelete(String luid
) throws IOException
{
465 throw new java
.lang
.InternalError("Should not have been called");
469 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
470 throw new java
.lang
.InternalError("Should not have been called");
476 * Return the meta of the given story or a list of all known metas if the
479 * Will not get the covers.
482 * the luid of the story or *
484 * the optional progress
489 private List
<MetaData
> getMetasList(final String luid
, Progress pg
) {
490 final Progress pgF
= pg
;
491 final List
<MetaData
> metas
= new ArrayList
<MetaData
>();
494 new ConnectActionClientObject(host
, port
, true) {
496 public void action(Version serverVersion
) throws Exception
{
502 Object rep
= send(new Object
[] { md5
, "GET_METADATA", luid
});
505 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
512 if (rep
instanceof MetaData
[]) {
513 for (MetaData meta
: (MetaData
[]) rep
) {
516 } else if (rep
!= null) {
517 metas
.add((MetaData
) rep
);
522 protected void onError(Exception e
) {
523 Instance
.getTraceHandler().error(e
);
526 } catch (Exception e
) {
527 Instance
.getTraceHandler().error(e
);