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
50 .<MetaData
[]> getRemoteObject("GET_METADATA *");
52 if (fromNetwork
!= null) {
53 for (MetaData meta
: fromNetwork
) {
62 public BufferedImage
getCover(final String luid
) {
63 return this.<BufferedImage
> getRemoteObject("GET_COVER " + luid
);
67 public synchronized Story
getStory(final String luid
, Progress pg
) {
68 return this.<Story
> getRemoteObject("GET_STORY " + luid
);
72 protected void clearCache() {
76 public synchronized Story
save(Story story
, String luid
, Progress pg
)
78 throw new java
.lang
.InternalError(
79 "No write support allowed on remote Libraries");
83 public synchronized void delete(String luid
) throws IOException
{
84 throw new java
.lang
.InternalError(
85 "No write support allowed on remote Libraries");
89 public void setSourceCover(String source
, String luid
) {
90 throw new java
.lang
.InternalError(
91 "No write support allowed on remote Libraries");
95 public synchronized File
getFile(final String luid
) {
96 throw new java
.lang
.InternalError(
97 "Operation not supportorted on remote Libraries");
100 // The following methods are only used by Save and Delete in BasicLibrary:
103 protected int getNextId() {
104 throw new java
.lang
.InternalError("Should not have been called");
108 protected void doDelete(String luid
) throws IOException
{
109 throw new java
.lang
.InternalError("Should not have been called");
113 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
114 throw new java
.lang
.InternalError("Should not have been called");
118 * Return an object from the server.
121 * the expected type of object
123 * the command to send
125 * @return the object or NULL
127 @SuppressWarnings("unchecked")
128 private <T
> T
getRemoteObject(final String command
) {
129 final Object
[] result
= new Object
[1];
131 new ConnectActionClient(host
, port
, true) {
133 public void action(Version serverVersion
) throws Exception
{
135 Object rep
= send(command
);
137 } catch (Exception e
) {
142 } catch (IOException e
) {
147 return (T
) result
[0];
148 } catch (Exception e
) {