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
.server
.ConnectActionClientObject
;
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 public BufferedImage
getCover(final String luid
) {
52 final BufferedImage
[] result
= new BufferedImage
[1];
55 new ConnectActionClientObject(host
, port
, true) {
57 public void action(Version serverVersion
) throws Exception
{
58 Object rep
= send(new Object
[] { key
, "GET_COVER", luid
});
59 result
[0] = (BufferedImage
) rep
;
63 protected void onError(Exception e
) {
64 Instance
.getTraceHandler().error(e
);
67 } catch (Exception e
) {
68 Instance
.getTraceHandler().error(e
);
75 public BufferedImage
getSourceCover(final String source
) {
76 final BufferedImage
[] result
= new BufferedImage
[1];
79 new ConnectActionClientObject(host
, port
, true) {
81 public void action(Version serverVersion
) throws Exception
{
82 Object rep
= send(new Object
[] { key
, "GET_SOURCE_COVER",
84 result
[0] = (BufferedImage
) rep
;
88 protected void onError(Exception e
) {
89 Instance
.getTraceHandler().error(e
);
92 } catch (Exception e
) {
93 Instance
.getTraceHandler().error(e
);
100 public synchronized Story
getStory(final String luid
, Progress pg
) {
101 final Progress pgF
= pg
;
102 final Story
[] result
= new Story
[1];
105 new ConnectActionClientObject(host
, port
, true) {
107 public void action(Version serverVersion
) throws Exception
{
113 Object rep
= send(new Object
[] { key
, "GET_STORY", luid
});
115 MetaData meta
= null;
116 if (rep
instanceof MetaData
) {
117 meta
= (MetaData
) rep
;
118 if (meta
.getWords() <= Integer
.MAX_VALUE
) {
119 pg
.setMinMax(0, (int) meta
.getWords());
123 List
<Object
> list
= new ArrayList
<Object
>();
124 for (Object obj
= send(null); obj
!= null; obj
= send(null)) {
129 result
[0] = RemoteLibraryServer
.rebuildStory(list
);
134 protected void onError(Exception e
) {
135 Instance
.getTraceHandler().error(e
);
138 } catch (Exception e
) {
139 Instance
.getTraceHandler().error(e
);
146 public synchronized Story
save(final Story story
, final String luid
,
147 Progress pg
) throws IOException
{
148 final Progress pgF
= pg
;
150 new ConnectActionClientObject(host
, port
, true) {
152 public void action(Version serverVersion
) throws Exception
{
158 if (story
.getMeta().getWords() <= Integer
.MAX_VALUE
) {
159 pg
.setMinMax(0, (int) story
.getMeta().getWords());
162 send(new Object
[] { key
, "SAVE_STORY", luid
});
164 List
<Object
> list
= RemoteLibraryServer
.breakStory(story
);
165 for (Object obj
: list
) {
175 protected void onError(Exception e
) {
176 Instance
.getTraceHandler().error(e
);
180 // because the meta changed:
182 story
.setMeta(getInfo(luid
));
188 public synchronized void delete(final String luid
) throws IOException
{
189 new ConnectActionClientObject(host
, port
, true) {
191 public void action(Version serverVersion
) throws Exception
{
192 send(new Object
[] { key
, "DELETE_STORY", luid
});
196 protected void onError(Exception e
) {
197 Instance
.getTraceHandler().error(e
);
203 public void setSourceCover(final String source
, final String luid
) {
205 new ConnectActionClientObject(host
, port
, true) {
207 public void action(Version serverVersion
) throws Exception
{
208 send(new Object
[] { key
, "SET_SOURCE_COVER", source
, luid
});
212 protected void onError(Exception e
) {
213 Instance
.getTraceHandler().error(e
);
216 } catch (IOException e
) {
217 Instance
.getTraceHandler().error(e
);
222 public synchronized File
getFile(final String luid
, Progress pg
) {
223 throw new java
.lang
.InternalError(
224 "Operation not supportorted on remote Libraries");
228 * Check if this {@link RemoteLibraryServer} is able to connect and identify
229 * to the remote server.
231 * @return TRUE if it is online
233 public boolean isOnline() {
234 final Boolean
[] result
= new Boolean
[1];
238 new ConnectActionClientObject(host
, port
, true) {
240 public void action(Version serverVersion
) throws Exception
{
241 Object rep
= send(new Object
[] { key
, "PING" });
242 result
[0] = "PONG".equals(rep
);
246 protected void onError(Exception e
) {
247 Instance
.getTraceHandler().error(e
);
250 } catch (Exception e
) {
251 Instance
.getTraceHandler().error(e
);
258 protected List
<MetaData
> getMetas(Progress pg
) {
259 final Progress pgF
= pg
;
260 final List
<MetaData
> metas
= new ArrayList
<MetaData
>();
263 new ConnectActionClientObject(host
, port
, true) {
265 public void action(Version serverVersion
) throws Exception
{
271 Object rep
= send(new Object
[] { key
, "GET_METADATA", "*" });
274 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
281 for (MetaData meta
: (MetaData
[]) rep
) {
287 protected void onError(Exception e
) {
288 Instance
.getTraceHandler().error(e
);
291 } catch (Exception e
) {
292 Instance
.getTraceHandler().error(e
);
299 protected void clearCache() {
302 // The following methods are only used by Save and Delete in BasicLibrary:
305 protected int getNextId() {
306 throw new java
.lang
.InternalError("Should not have been called");
310 protected void doDelete(String luid
) throws IOException
{
311 throw new java
.lang
.InternalError("Should not have been called");
315 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
316 throw new java
.lang
.InternalError("Should not have been called");