1 package be
.nikiroo
.fanfix
.library
;
3 import java
.awt
.image
.BufferedImage
;
5 import java
.io
.IOException
;
6 import java
.util
.ArrayList
;
9 import be
.nikiroo
.fanfix
.Instance
;
10 import be
.nikiroo
.fanfix
.data
.MetaData
;
11 import be
.nikiroo
.fanfix
.data
.Story
;
12 import be
.nikiroo
.utils
.Progress
;
13 import be
.nikiroo
.utils
.Version
;
14 import be
.nikiroo
.utils
.serial
.ConnectActionClient
;
17 * This {@link BasicLibrary} will access a remote server to list the available
18 * stories, and download the ones you try to load to the local directory
19 * specified in the configuration.
23 public class RemoteLibrary
extends BasicLibrary
{
28 * Create a {@link RemoteLibrary} linked to the given server.
31 * the host to contact or NULL for localhost
33 * the port to contact it on
35 public RemoteLibrary(String host
, int port
) {
41 public String
getLibraryName() {
42 return host
+ ":" + port
;
46 protected List
<MetaData
> getMetas(Progress pg
) {
48 final List
<MetaData
> metas
= new ArrayList
<MetaData
>();
49 MetaData
[] fromNetwork
= this.<MetaData
[]> getRemoteObject( //
50 new Object
[] { "GET_METADATA", "*" });
52 if (fromNetwork
!= null) {
53 for (MetaData meta
: fromNetwork
) {
62 public BufferedImage
getCover(final String luid
) {
63 return this.<BufferedImage
> getRemoteObject( //
64 new Object
[] { "GET_COVER", luid
});
68 public BufferedImage
getSourceCover(final String source
) {
69 return this.<BufferedImage
> getRemoteObject( //
70 new Object
[] { "GET_SOURCE_COVER", source
});
74 public synchronized Story
getStory(final String luid
, Progress pg
) {
75 return this.<Story
> getRemoteObject(new Object
[] { "GET_STORY", luid
});
79 protected void clearCache() {
83 public synchronized Story
save(Story story
, String luid
, Progress pg
)
85 getRemoteObject(new Object
[] { "SAVE_STORY", story
, luid
});
87 // because the meta changed:
89 story
.setMeta(getInfo(luid
));
95 public synchronized void delete(String luid
) throws IOException
{
96 getRemoteObject(new Object
[] { "DELETE_STORY", luid
});
100 public void setSourceCover(String source
, String luid
) {
101 this.<BufferedImage
> getRemoteObject( //
102 new Object
[] { "SET_SOURCE_COVER", source
, luid
});
106 public synchronized File
getFile(final String luid
, Progress pg
) {
107 throw new java
.lang
.InternalError(
108 "Operation not supportorted on remote Libraries");
111 // The following methods are only used by Save and Delete in BasicLibrary:
114 protected int getNextId() {
115 throw new java
.lang
.InternalError("Should not have been called");
119 protected void doDelete(String luid
) throws IOException
{
120 throw new java
.lang
.InternalError("Should not have been called");
124 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
125 throw new java
.lang
.InternalError("Should not have been called");
129 * Return an object from the server.
132 * the expected type of object
134 * the command to send
136 * @return the object or NULL
138 @SuppressWarnings("unchecked")
139 private <T
> T
getRemoteObject(final Object
[] command
) {
140 final Object
[] result
= new Object
[1];
142 new ConnectActionClient(host
, port
, true) {
144 public void action(Version serverVersion
) throws Exception
{
146 Object rep
= send(command
);
148 } catch (Exception e
) {
153 } catch (IOException e
) {
158 return (T
) result
[0];
159 } catch (Exception e
) {