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 key
;
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
) {
49 public String
getLibraryName() {
50 return host
+ ":" + port
;
54 public Status
getStatus() {
55 final Status
[] result
= new Status
[1];
57 result
[0] = Status
.INVALID
;
60 Instance
.getTraceHandler().trace("Getting remote lib status...");
61 new ConnectActionClientObject(host
, port
, key
) {
63 public void action(Version serverVersion
) throws Exception
{
65 Object rep
= sendCmd(this, new Object
[] { "PING" });
67 if ("PONG".equals(rep
)) {
68 result
[0] = Status
.READY
;
70 result
[0] = Status
.UNAUTORIZED
;
72 } catch (IllegalArgumentException e
) {
73 result
[0] = Status
.UNAUTORIZED
;
78 protected void onError(Exception e
) {
79 result
[0] = Status
.UNAVAILABLE
;
82 } catch (UnknownHostException e
) {
83 result
[0] = Status
.INVALID
;
84 } catch (IllegalArgumentException e
) {
85 result
[0] = Status
.INVALID
;
86 } catch (Exception e
) {
87 result
[0] = Status
.UNAVAILABLE
;
90 Instance
.getTraceHandler().trace("Remote lib status: " + result
[0]);
95 public Image
getCover(final String luid
) {
96 final Image
[] result
= new Image
[1];
99 new ConnectActionClientObject(host
, port
, key
) {
101 public void action(Version serverVersion
) throws Exception
{
102 Object rep
= sendCmd(this,
103 new Object
[] { "GET_COVER", luid
});
104 result
[0] = (Image
) rep
;
108 protected void onError(Exception e
) {
109 Instance
.getTraceHandler().error(e
);
112 } catch (Exception e
) {
113 Instance
.getTraceHandler().error(e
);
120 public Image
getCustomSourceCover(final String source
) {
121 return getCustomCover(source
, "SOURCE");
125 public Image
getCustomAuthorCover(final String author
) {
126 return getCustomCover(author
, "AUTHOR");
129 // type: "SOURCE" or "AUTHOR"
130 private Image
getCustomCover(final String source
, final String type
) {
131 final Image
[] result
= new Image
[1];
134 new ConnectActionClientObject(host
, port
, key
) {
136 public void action(Version serverVersion
) throws Exception
{
137 Object rep
= sendCmd(this, new Object
[] {
138 "GET_CUSTOM_COVER", type
, source
});
139 result
[0] = (Image
) rep
;
143 protected void onError(Exception e
) {
144 Instance
.getTraceHandler().error(e
);
147 } catch (Exception e
) {
148 Instance
.getTraceHandler().error(e
);
155 public synchronized Story
getStory(final String luid
, Progress pg
) {
156 final Progress pgF
= pg
;
157 final Story
[] result
= new Story
[1];
160 new ConnectActionClientObject(host
, port
, key
) {
162 public void action(Version serverVersion
) throws Exception
{
168 Object rep
= sendCmd(this,
169 new Object
[] { "GET_STORY", luid
});
171 MetaData meta
= null;
172 if (rep
instanceof MetaData
) {
173 meta
= (MetaData
) rep
;
174 if (meta
.getWords() <= Integer
.MAX_VALUE
) {
175 pg
.setMinMax(0, (int) meta
.getWords());
179 List
<Object
> list
= new ArrayList
<Object
>();
180 for (Object obj
= send(null); obj
!= null; obj
= send(null)) {
185 result
[0] = RemoteLibraryServer
.rebuildStory(list
);
190 protected void onError(Exception e
) {
191 Instance
.getTraceHandler().error(e
);
194 } catch (Exception e
) {
195 Instance
.getTraceHandler().error(e
);
202 public synchronized Story
save(final Story story
, final String luid
,
203 Progress pg
) throws IOException
{
204 final String
[] luidSaved
= new String
[1];
205 Progress pgSave
= new Progress();
206 Progress pgRefresh
= new Progress();
212 pg
.addProgress(pgSave
, 9);
213 pg
.addProgress(pgRefresh
, 1);
215 final Progress pgF
= pgSave
;
217 new ConnectActionClientObject(host
, port
, key
) {
219 public void action(Version serverVersion
) throws Exception
{
221 if (story
.getMeta().getWords() <= Integer
.MAX_VALUE
) {
222 pg
.setMinMax(0, (int) story
.getMeta().getWords());
225 sendCmd(this, new Object
[] { "SAVE_STORY", luid
});
227 List
<Object
> list
= RemoteLibraryServer
.breakStory(story
);
228 for (Object obj
: list
) {
233 luidSaved
[0] = (String
) send(null);
239 protected void onError(Exception e
) {
240 Instance
.getTraceHandler().error(e
);
244 // because the meta changed:
245 MetaData meta
= getInfo(luidSaved
[0]);
246 if (story
.getMeta().getClass() != null) {
247 // If already available locally:
248 meta
.setCover(story
.getMeta().getCover());
251 meta
.setCover(getCover(meta
.getLuid()));
261 public synchronized void delete(final String luid
) throws IOException
{
262 new ConnectActionClientObject(host
, port
, key
) {
264 public void action(Version serverVersion
) throws Exception
{
265 sendCmd(this, new Object
[] { "DELETE_STORY", luid
});
269 protected void onError(Exception e
) {
270 Instance
.getTraceHandler().error(e
);
276 public void setSourceCover(final String source
, final String luid
) {
277 setCover(source
, luid
, "SOURCE");
281 public void setAuthorCover(final String author
, final String luid
) {
282 setCover(author
, luid
, "AUTHOR");
285 // type = "SOURCE" | "AUTHOR"
286 private void setCover(final String value
, final String luid
,
289 new ConnectActionClientObject(host
, port
, key
) {
291 public void action(Version serverVersion
) throws Exception
{
293 new Object
[] { "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
, key
) {
332 public void action(Version serverVersion
) throws Exception
{
335 Object rep
= sendCmd(this,
336 new Object
[] { "IMPORT", url
.toString() });
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
, key
) {
380 public void action(Version serverVersion
) throws Exception
{
383 Object rep
= sendCmd(this, new Object
[] { "CHANGE_STA",
384 luid
, 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
, key
) {
417 public void action(Version serverVersion
) throws Exception
{
418 sendCmd(this, new Object
[] { "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
, key
) {
496 public void action(Version serverVersion
) throws Exception
{
502 Object rep
= sendCmd(this, new Object
[] { "GET_METADATA",
506 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
513 if (rep
instanceof MetaData
[]) {
514 for (MetaData meta
: (MetaData
[]) rep
) {
517 } else if (rep
!= null) {
518 metas
.add((MetaData
) rep
);
523 protected void onError(Exception e
) {
524 Instance
.getTraceHandler().error(e
);
527 } catch (Exception e
) {
528 Instance
.getTraceHandler().error(e
);
534 // IllegalArgumentException if key is bad
535 private Object
sendCmd(ConnectActionClientObject action
, Object
[] params
)
536 throws IOException
, NoSuchFieldException
, NoSuchMethodException
,
537 ClassNotFoundException
{
538 Object rep
= action
.send(params
);
540 String hash
= hashKey(key
, "" + rep
);
541 return action
.send(hash
);
545 * Return a hash that corresponds to the given key and the given random
549 * the key (the secret)
554 * @return a hash that was computed using both
556 static String
hashKey(String key
, String random
) {
557 return StringUtils
.getMd5Hash(key
+ " <==> " + random
);