1 package be
.nikiroo
.fanfix
.library
;
3 import java
.awt
.image
.BufferedImage
;
5 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
.Progress
;
14 import be
.nikiroo
.utils
.Version
;
15 import be
.nikiroo
.utils
.serial
.server
.ConnectActionClientObject
;
18 * This {@link BasicLibrary} will access a remote server to list the available
19 * stories, and download the ones you try to load to the local directory
20 * specified in the configuration.
24 public class RemoteLibrary
extends BasicLibrary
{
27 private final String key
;
30 * Create a {@link RemoteLibrary} linked to the given server.
33 * the key that will allow us to exchange information with the
36 * the host to contact or NULL for localhost
38 * the port to contact it on
40 public RemoteLibrary(String key
, String host
, int port
) {
47 public String
getLibraryName() {
48 return host
+ ":" + port
;
52 public Status
getStatus() {
53 final Status
[] result
= new Status
[1];
55 result
[0] = Status
.INVALID
;
57 ConnectActionClientObject action
= null;
59 action
= new ConnectActionClientObject(host
, port
, true) {
61 public void action(Version serverVersion
) throws Exception
{
62 Object rep
= send(new Object
[] { key
, "PING" });
63 if ("PONG".equals(rep
)) {
64 result
[0] = Status
.READY
;
66 result
[0] = Status
.UNAUTORIZED
;
71 protected void onError(Exception e
) {
72 result
[0] = Status
.UNAVAILABLE
;
76 } catch (UnknownHostException e
) {
77 result
[0] = Status
.INVALID
;
78 } catch (IllegalArgumentException e
) {
79 result
[0] = Status
.INVALID
;
80 } catch (Exception e
) {
81 result
[0] = Status
.UNAVAILABLE
;
87 } catch (Exception e
) {
88 result
[0] = Status
.UNAVAILABLE
;
96 public BufferedImage
getCover(final String luid
) {
97 final BufferedImage
[] result
= new BufferedImage
[1];
100 new ConnectActionClientObject(host
, port
, true) {
102 public void action(Version serverVersion
) throws Exception
{
103 Object rep
= send(new Object
[] { key
, "GET_COVER", luid
});
104 result
[0] = (BufferedImage
) rep
;
108 protected void onError(Exception e
) {
109 Instance
.getTraceHandler().error(e
);
112 } catch (Exception e
) {
113 Instance
.getTraceHandler().error(e
);
120 public BufferedImage
getSourceCover(final String source
) {
121 final BufferedImage
[] result
= new BufferedImage
[1];
124 new ConnectActionClientObject(host
, port
, true) {
126 public void action(Version serverVersion
) throws Exception
{
127 Object rep
= send(new Object
[] { key
, "GET_SOURCE_COVER",
129 result
[0] = (BufferedImage
) rep
;
133 protected void onError(Exception e
) {
134 Instance
.getTraceHandler().error(e
);
137 } catch (Exception e
) {
138 Instance
.getTraceHandler().error(e
);
145 public synchronized Story
getStory(final String luid
, Progress pg
) {
146 final Progress pgF
= pg
;
147 final Story
[] result
= new Story
[1];
150 new ConnectActionClientObject(host
, port
, true) {
152 public void action(Version serverVersion
) throws Exception
{
158 Object rep
= send(new Object
[] { key
, "GET_STORY", luid
});
160 MetaData meta
= null;
161 if (rep
instanceof MetaData
) {
162 meta
= (MetaData
) rep
;
163 if (meta
.getWords() <= Integer
.MAX_VALUE
) {
164 pg
.setMinMax(0, (int) meta
.getWords());
168 List
<Object
> list
= new ArrayList
<Object
>();
169 for (Object obj
= send(null); obj
!= null; obj
= send(null)) {
174 result
[0] = RemoteLibraryServer
.rebuildStory(list
);
179 protected void onError(Exception e
) {
180 Instance
.getTraceHandler().error(e
);
183 } catch (Exception e
) {
184 Instance
.getTraceHandler().error(e
);
191 public synchronized Story
save(final Story story
, final String luid
,
192 Progress pg
) throws IOException
{
193 final Progress pgF
= pg
;
195 new ConnectActionClientObject(host
, port
, true) {
197 public void action(Version serverVersion
) throws Exception
{
203 if (story
.getMeta().getWords() <= Integer
.MAX_VALUE
) {
204 pg
.setMinMax(0, (int) story
.getMeta().getWords());
207 send(new Object
[] { key
, "SAVE_STORY", luid
});
209 List
<Object
> list
= RemoteLibraryServer
.breakStory(story
);
210 for (Object obj
: list
) {
220 protected void onError(Exception e
) {
221 Instance
.getTraceHandler().error(e
);
225 // because the meta changed:
227 story
.setMeta(getInfo(luid
));
233 public synchronized void delete(final String luid
) throws IOException
{
234 new ConnectActionClientObject(host
, port
, true) {
236 public void action(Version serverVersion
) throws Exception
{
237 send(new Object
[] { key
, "DELETE_STORY", luid
});
241 protected void onError(Exception e
) {
242 Instance
.getTraceHandler().error(e
);
248 public void setSourceCover(final String source
, final String luid
) {
250 new ConnectActionClientObject(host
, port
, true) {
252 public void action(Version serverVersion
) throws Exception
{
253 send(new Object
[] { key
, "SET_SOURCE_COVER", source
, luid
});
257 protected void onError(Exception e
) {
258 Instance
.getTraceHandler().error(e
);
261 } catch (IOException e
) {
262 Instance
.getTraceHandler().error(e
);
267 public synchronized File
getFile(final String luid
, Progress pg
) {
268 throw new java
.lang
.InternalError(
269 "Operation not supportorted on remote Libraries");
273 protected List
<MetaData
> getMetas(Progress pg
) {
274 final Progress pgF
= pg
;
275 final List
<MetaData
> metas
= new ArrayList
<MetaData
>();
278 new ConnectActionClientObject(host
, port
, true) {
280 public void action(Version serverVersion
) throws Exception
{
286 Object rep
= send(new Object
[] { key
, "GET_METADATA", "*" });
289 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
296 for (MetaData meta
: (MetaData
[]) rep
) {
302 protected void onError(Exception e
) {
303 Instance
.getTraceHandler().error(e
);
306 } catch (Exception e
) {
307 Instance
.getTraceHandler().error(e
);
314 protected void clearCache() {
317 // The following methods are only used by Save and Delete in BasicLibrary:
320 protected int getNextId() {
321 throw new java
.lang
.InternalError("Should not have been called");
325 protected void doDelete(String luid
) throws IOException
{
326 throw new java
.lang
.InternalError("Should not have been called");
330 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
331 throw new java
.lang
.InternalError("Should not have been called");