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 private LocalLibrary lib
;
29 private List
<MetaData
> metas
;
32 * Create a {@link RemoteLibrary} linked to the given server.
35 * the host to contact or NULL for localhost
37 * the port to contact it on
39 public RemoteLibrary(String host
, int port
) {
43 this.baseDir
= Instance
.getRemoteDir(host
);
44 this.baseDir
.mkdirs();
46 this.lib
= new LocalLibrary(baseDir
);
50 public String
getLibraryName() {
51 return host
+ ":" + port
;
55 protected List
<MetaData
> getMetas(Progress pg
) {
59 metas
= new ArrayList
<MetaData
>();
62 new ConnectActionClient(host
, port
, true) {
64 public void action(Version serverVersion
) throws Exception
{
66 Object rep
= send("GET_METADATA *");
67 for (MetaData meta
: (MetaData
[]) rep
) {
70 } catch (Exception e
) {
75 } catch (IOException e
) {
79 List
<String
> test
= new ArrayList
<String
>();
80 for (MetaData meta
: metas
) {
81 if (test
.contains(meta
.getLuid())) {
82 throw new RuntimeException("wwops");
84 test
.add(meta
.getLuid());
92 public synchronized File
getFile(final String luid
) {
93 File file
= lib
.getFile(luid
);
95 final File
[] tmp
= new File
[1];
97 new ConnectActionClient(host
, port
, true) {
99 public void action(Version serverVersion
) throws Exception
{
101 Object rep
= send("GET_STORY " + luid
);
102 Story story
= (Story
) rep
;
104 lib
.save(story
, luid
, null);
105 tmp
[0] = lib
.getFile(luid
);
107 } catch (Exception e
) {
112 } catch (IOException e
) {
120 MetaData meta
= getInfo(luid
);
128 public BufferedImage
getCover(final String luid
) {
129 // Retrieve it from the cache if possible:
130 if (lib
.getInfo(luid
) != null) {
131 return lib
.getCover(luid
);
134 final BufferedImage
[] result
= new BufferedImage
[1];
136 new ConnectActionClient(host
, port
, true) {
138 public void action(Version serverVersion
) throws Exception
{
140 Object rep
= send("GET_COVER " + luid
);
141 result
[0] = (BufferedImage
) rep
;
142 } catch (Exception e
) {
147 } catch (IOException e
) {
155 protected void clearCache() {
161 public synchronized Story
save(Story story
, String luid
, Progress pg
)
163 throw new java
.lang
.InternalError(
164 "No write support allowed on remote Libraries");
168 public synchronized void delete(String luid
) throws IOException
{
169 throw new java
.lang
.InternalError(
170 "No write support allowed on remote Libraries");
174 public void setSourceCover(String source
, String luid
) {
175 throw new java
.lang
.InternalError(
176 "No write support allowed on remote Libraries");
179 // All the following methods are only used by Save and Delete in
183 protected int getNextId() {
184 throw new java
.lang
.InternalError("Should not have been called");
188 protected void doDelete(String luid
) throws IOException
{
189 throw new java
.lang
.InternalError("Should not have been called");
193 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
194 throw new java
.lang
.InternalError("Should not have been called");
198 * Return the backing local library.
200 * @return the library
202 LocalLibrary
getLocalLibrary() {