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
getSourceCover(final String source
) {
123 final Image
[] result
= new Image
[1];
126 new ConnectActionClientObject(host
, port
, true) {
128 public void action(Version serverVersion
) throws Exception
{
129 Object rep
= send(new Object
[] { md5
, "GET_SOURCE_COVER",
131 result
[0] = (Image
) rep
;
135 protected void onError(Exception e
) {
136 Instance
.getTraceHandler().error(e
);
139 } catch (Exception e
) {
140 Instance
.getTraceHandler().error(e
);
147 public synchronized Story
getStory(final String luid
, Progress pg
) {
148 final Progress pgF
= pg
;
149 final Story
[] result
= new Story
[1];
152 new ConnectActionClientObject(host
, port
, true) {
154 public void action(Version serverVersion
) throws Exception
{
160 Object rep
= send(new Object
[] { md5
, "GET_STORY", luid
});
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());
170 List
<Object
> list
= new ArrayList
<Object
>();
171 for (Object obj
= send(null); obj
!= null; obj
= send(null)) {
176 result
[0] = RemoteLibraryServer
.rebuildStory(list
);
181 protected void onError(Exception e
) {
182 Instance
.getTraceHandler().error(e
);
185 } catch (Exception e
) {
186 Instance
.getTraceHandler().error(e
);
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();
203 pg
.addProgress(pgSave
, 9);
204 pg
.addProgress(pgRefresh
, 1);
206 final Progress pgF
= pgSave
;
208 new ConnectActionClientObject(host
, port
, true) {
210 public void action(Version serverVersion
) throws Exception
{
212 if (story
.getMeta().getWords() <= Integer
.MAX_VALUE
) {
213 pg
.setMinMax(0, (int) story
.getMeta().getWords());
216 send(new Object
[] { md5
, "SAVE_STORY", luid
});
218 List
<Object
> list
= RemoteLibraryServer
.breakStory(story
);
219 for (Object obj
: list
) {
224 luidSaved
[0] = (String
) send(null);
230 protected void onError(Exception e
) {
231 Instance
.getTraceHandler().error(e
);
235 // because the meta changed:
236 invalidateInfo(luidSaved
[0]);
238 MetaData meta
= getInfo(luidSaved
[0]);
239 meta
.setCover(story
.getMeta().getCover());
248 public synchronized void delete(final String luid
) throws IOException
{
249 new ConnectActionClientObject(host
, port
, true) {
251 public void action(Version serverVersion
) throws Exception
{
252 send(new Object
[] { md5
, "DELETE_STORY", luid
});
256 protected void onError(Exception e
) {
257 Instance
.getTraceHandler().error(e
);
263 public void setSourceCover(final String source
, final String luid
) {
265 new ConnectActionClientObject(host
, port
, true) {
267 public void action(Version serverVersion
) throws Exception
{
268 send(new Object
[] { md5
, "SET_SOURCE_COVER", source
, luid
});
272 protected void onError(Exception e
) {
273 Instance
.getTraceHandler().error(e
);
276 } catch (IOException e
) {
277 Instance
.getTraceHandler().error(e
);
282 // Could work (more slowly) without it
283 public Story
imprt(final URL url
, Progress pg
) throws IOException
{
289 Progress pgImprt
= new Progress();
290 Progress pgGet
= new Progress();
291 pg
.addProgress(pgImprt
, 1);
292 pg
.addProgress(pgGet
, 1);
294 final Progress pgF
= pgImprt
;
295 final String
[] luid
= new String
[1];
298 new ConnectActionClientObject(host
, port
, true) {
300 public void action(Version serverVersion
) throws Exception
{
303 Object rep
= send(new Object
[] { md5
, "IMPORT",
307 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
315 luid
[0] = (String
) rep
;
319 protected void onError(Exception e
) {
320 Instance
.getTraceHandler().error(e
);
323 } catch (IOException e
) {
324 Instance
.getTraceHandler().error(e
);
327 if (luid
[0] == null) {
328 throw new IOException("Remote failure");
331 Story story
= getStory(luid
[0], pgGet
);
339 // Could work (more slowly) without it
340 public synchronized void changeSource(final String luid
,
341 final String newSource
, Progress pg
) throws IOException
{
342 final Progress pgF
= pg
== null ?
new Progress() : pg
;
345 new ConnectActionClientObject(host
, port
, true) {
347 public void action(Version serverVersion
) throws Exception
{
350 Object rep
= send(new Object
[] { md5
, "CHANGE_SOURCE",
353 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
362 protected void onError(Exception e
) {
363 Instance
.getTraceHandler().error(e
);
366 } catch (IOException e
) {
367 Instance
.getTraceHandler().error(e
);
372 public synchronized File
getFile(final String luid
, Progress pg
) {
373 throw new java
.lang
.InternalError(
374 "Operation not supportorted on remote Libraries");
382 new ConnectActionClientObject(host
, port
, true) {
384 public void action(Version serverVersion
) throws Exception
{
385 send(new Object
[] { md5
, "EXIT" });
389 protected void onError(Exception e
) {
390 Instance
.getTraceHandler().error(e
);
393 } catch (IOException e
) {
394 Instance
.getTraceHandler().error(e
);
399 public synchronized MetaData
getInfo(String luid
) {
400 List
<MetaData
> metas
= getMetasList(luid
, null);
401 if (!metas
.isEmpty()) {
409 protected List
<MetaData
> getMetas(Progress pg
) {
410 return getMetasList("*", pg
);
414 protected void invalidateInfo(String luid
) {
417 // The following methods are only used by Save and Delete in BasicLibrary:
420 protected int getNextId() {
421 throw new java
.lang
.InternalError("Should not have been called");
425 protected void doDelete(String luid
) throws IOException
{
426 throw new java
.lang
.InternalError("Should not have been called");
430 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
431 throw new java
.lang
.InternalError("Should not have been called");
437 * Return the meta of the given story or a list of all known metas if the
441 * the luid of the story or *
443 * the optional progress
448 private List
<MetaData
> getMetasList(final String luid
, Progress pg
) {
449 final Progress pgF
= pg
;
450 final List
<MetaData
> metas
= new ArrayList
<MetaData
>();
453 new ConnectActionClientObject(host
, port
, true) {
455 public void action(Version serverVersion
) throws Exception
{
461 Object rep
= send(new Object
[] { md5
, "GET_METADATA", luid
});
464 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
471 if (rep
instanceof MetaData
[]) {
472 for (MetaData meta
: (MetaData
[]) rep
) {
475 } else if (rep
!= null) {
476 metas
.add((MetaData
) rep
);
481 protected void onError(Exception e
) {
482 Instance
.getTraceHandler().error(e
);
485 } catch (Exception e
) {
486 Instance
.getTraceHandler().error(e
);