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
.StringUtils
;
15 import be
.nikiroo
.utils
.Version
;
16 import be
.nikiroo
.utils
.serial
.server
.ConnectActionClientObject
;
19 * This {@link BasicLibrary} will access a remote server to list the available
20 * stories, and download the ones you try to load to the local directory
21 * specified in the configuration.
25 public class RemoteLibrary
extends BasicLibrary
{
28 private final String md5
;
31 * Create a {@link RemoteLibrary} linked to the given server.
34 * the key that will allow us to exchange information with the
37 * the host to contact or NULL for localhost
39 * the port to contact it on
41 public RemoteLibrary(String key
, String host
, int port
) {
42 this.md5
= StringUtils
.getMd5Hash(key
);
48 public String
getLibraryName() {
49 return host
+ ":" + port
;
53 public Status
getStatus() {
54 final Status
[] result
= new Status
[1];
56 result
[0] = Status
.INVALID
;
58 ConnectActionClientObject action
= null;
60 action
= new ConnectActionClientObject(host
, port
, true) {
62 public void action(Version serverVersion
) throws Exception
{
63 Object rep
= send(new Object
[] { md5
, "PING" });
64 if ("PONG".equals(rep
)) {
65 result
[0] = Status
.READY
;
67 result
[0] = Status
.UNAUTORIZED
;
72 protected void onError(Exception e
) {
73 result
[0] = Status
.UNAVAILABLE
;
77 } catch (UnknownHostException e
) {
78 result
[0] = Status
.INVALID
;
79 } catch (IllegalArgumentException e
) {
80 result
[0] = Status
.INVALID
;
81 } catch (Exception e
) {
82 result
[0] = Status
.UNAVAILABLE
;
88 } catch (Exception e
) {
89 result
[0] = Status
.UNAVAILABLE
;
97 public BufferedImage
getCover(final String luid
) {
98 final BufferedImage
[] result
= new BufferedImage
[1];
101 new ConnectActionClientObject(host
, port
, true) {
103 public void action(Version serverVersion
) throws Exception
{
104 Object rep
= send(new Object
[] { md5
, "GET_COVER", luid
});
105 result
[0] = (BufferedImage
) rep
;
109 protected void onError(Exception e
) {
110 Instance
.getTraceHandler().error(e
);
113 } catch (Exception e
) {
114 Instance
.getTraceHandler().error(e
);
121 public BufferedImage
getSourceCover(final String source
) {
122 final BufferedImage
[] result
= new BufferedImage
[1];
125 new ConnectActionClientObject(host
, port
, true) {
127 public void action(Version serverVersion
) throws Exception
{
128 Object rep
= send(new Object
[] { md5
, "GET_SOURCE_COVER",
130 result
[0] = (BufferedImage
) rep
;
134 protected void onError(Exception e
) {
135 Instance
.getTraceHandler().error(e
);
138 } catch (Exception e
) {
139 Instance
.getTraceHandler().error(e
);
146 public synchronized Story
getStory(final String luid
, Progress pg
) {
147 final Progress pgF
= pg
;
148 final Story
[] result
= new Story
[1];
151 new ConnectActionClientObject(host
, port
, true) {
153 public void action(Version serverVersion
) throws Exception
{
159 Object rep
= send(new Object
[] { md5
, "GET_STORY", luid
});
161 MetaData meta
= null;
162 if (rep
instanceof MetaData
) {
163 meta
= (MetaData
) rep
;
164 if (meta
.getWords() <= Integer
.MAX_VALUE
) {
165 pg
.setMinMax(0, (int) meta
.getWords());
169 List
<Object
> list
= new ArrayList
<Object
>();
170 for (Object obj
= send(null); obj
!= null; obj
= send(null)) {
175 result
[0] = RemoteLibraryServer
.rebuildStory(list
);
180 protected void onError(Exception e
) {
181 Instance
.getTraceHandler().error(e
);
184 } catch (Exception e
) {
185 Instance
.getTraceHandler().error(e
);
192 public synchronized Story
save(final Story story
, final String luid
,
193 Progress pg
) throws IOException
{
194 final String
[] luidSaved
= new String
[1];
195 Progress pgSave
= new Progress();
196 Progress pgRefresh
= new Progress();
202 pg
.addProgress(pgSave
, 9);
203 pg
.addProgress(pgRefresh
, 1);
205 final Progress pgF
= pgSave
;
207 new ConnectActionClientObject(host
, port
, true) {
209 public void action(Version serverVersion
) throws Exception
{
211 if (story
.getMeta().getWords() <= Integer
.MAX_VALUE
) {
212 pg
.setMinMax(0, (int) story
.getMeta().getWords());
215 send(new Object
[] { md5
, "SAVE_STORY", luid
});
217 List
<Object
> list
= RemoteLibraryServer
.breakStory(story
);
218 for (Object obj
: list
) {
223 send(null); // done sending the story
224 luidSaved
[0] = (String
) send(null); // get LUID
230 protected void onError(Exception e
) {
231 Instance
.getTraceHandler().error(e
);
235 // because the meta changed:
238 story
.setMeta(getInfo(luidSaved
[0]));
246 public synchronized void delete(final String luid
) throws IOException
{
247 new ConnectActionClientObject(host
, port
, true) {
249 public void action(Version serverVersion
) throws Exception
{
250 send(new Object
[] { md5
, "DELETE_STORY", luid
});
254 protected void onError(Exception e
) {
255 Instance
.getTraceHandler().error(e
);
261 public void setSourceCover(final String source
, final String luid
) {
263 new ConnectActionClientObject(host
, port
, true) {
265 public void action(Version serverVersion
) throws Exception
{
266 send(new Object
[] { md5
, "SET_SOURCE_COVER", source
, luid
});
270 protected void onError(Exception e
) {
271 Instance
.getTraceHandler().error(e
);
274 } catch (IOException e
) {
275 Instance
.getTraceHandler().error(e
);
280 public synchronized File
getFile(final String luid
, Progress pg
) {
281 throw new java
.lang
.InternalError(
282 "Operation not supportorted on remote Libraries");
290 new ConnectActionClientObject(host
, port
, true) {
292 public void action(Version serverVersion
) throws Exception
{
293 send(new Object
[] { md5
, "EXIT" });
297 protected void onError(Exception e
) {
298 Instance
.getTraceHandler().error(e
);
301 } catch (IOException e
) {
302 Instance
.getTraceHandler().error(e
);
307 protected List
<MetaData
> getMetas(Progress pg
) {
308 final Progress pgF
= pg
;
309 final List
<MetaData
> metas
= new ArrayList
<MetaData
>();
312 new ConnectActionClientObject(host
, port
, true) {
314 public void action(Version serverVersion
) throws Exception
{
320 Object rep
= send(new Object
[] { md5
, "GET_METADATA", "*" });
323 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
330 for (MetaData meta
: (MetaData
[]) rep
) {
336 protected void onError(Exception e
) {
337 Instance
.getTraceHandler().error(e
);
340 } catch (Exception e
) {
341 Instance
.getTraceHandler().error(e
);
348 protected void clearCache() {
351 // The following methods are only used by Save and Delete in BasicLibrary:
354 protected int getNextId() {
355 throw new java
.lang
.InternalError("Should not have been called");
359 protected void doDelete(String luid
) throws IOException
{
360 throw new java
.lang
.InternalError("Should not have been called");
364 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
365 throw new java
.lang
.InternalError("Should not have been called");