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
{
26 private final String key
;
29 * Create a {@link RemoteLibrary} linked to the given server.
32 * the key that will allow us to exchange information with the
35 * the host to contact or NULL for localhost
37 * the port to contact it on
39 public RemoteLibrary(String key
, String host
, int port
) {
46 public String
getLibraryName() {
47 return host
+ ":" + port
;
51 protected List
<MetaData
> getMetas(Progress pg
) {
53 final List
<MetaData
> metas
= new ArrayList
<MetaData
>();
54 MetaData
[] fromNetwork
= this.<MetaData
[]> getRemoteObject( //
55 new Object
[] { key
, "GET_METADATA", "*" });
57 if (fromNetwork
!= null) {
58 for (MetaData meta
: fromNetwork
) {
67 public BufferedImage
getCover(final String luid
) {
68 return this.<BufferedImage
> getRemoteObject( //
69 new Object
[] { key
, "GET_COVER", luid
});
73 public BufferedImage
getSourceCover(final String source
) {
74 return this.<BufferedImage
> getRemoteObject( //
75 new Object
[] { key
, "GET_SOURCE_COVER", source
});
79 public synchronized Story
getStory(final String luid
, Progress pg
) {
80 return this.<Story
> getRemoteObject( //
81 new Object
[] { key
, "GET_STORY", luid
});
85 protected void clearCache() {
89 public synchronized Story
save(Story story
, String luid
, Progress pg
)
91 getRemoteObject(new Object
[] { key
, "SAVE_STORY", story
, luid
});
93 // because the meta changed:
95 story
.setMeta(getInfo(luid
));
101 public synchronized void delete(String luid
) throws IOException
{
102 getRemoteObject(new Object
[] { key
, "DELETE_STORY", luid
});
106 public void setSourceCover(String source
, String luid
) {
107 this.<BufferedImage
> getRemoteObject( //
108 new Object
[] { key
, "SET_SOURCE_COVER", source
, luid
});
112 public synchronized File
getFile(final String luid
, Progress pg
) {
113 throw new java
.lang
.InternalError(
114 "Operation not supportorted on remote Libraries");
117 // The following methods are only used by Save and Delete in BasicLibrary:
120 protected int getNextId() {
121 throw new java
.lang
.InternalError("Should not have been called");
125 protected void doDelete(String luid
) throws IOException
{
126 throw new java
.lang
.InternalError("Should not have been called");
130 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
131 throw new java
.lang
.InternalError("Should not have been called");
135 * Return an object from the server.
138 * the expected type of object
140 * the command to send
142 * @return the object or NULL
144 @SuppressWarnings("unchecked")
145 private <T
> T
getRemoteObject(final Object
[] command
) {
146 final Object
[] result
= new Object
[1];
148 new ConnectActionClient(host
, port
, true) {
150 public void action(Version serverVersion
) throws Exception
{
152 Object rep
= send(command
);
154 } catch (Exception e
) {
159 } catch (IOException e
) {
164 return (T
) result
[0];
165 } catch (Exception e
) {