Fix cover file not sent over wire
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
CommitLineData
e42573a0 1package be.nikiroo.fanfix.library;
b0e88ebd 2
68e2c6d2 3import java.awt.image.BufferedImage;
b0e88ebd
NR
4import java.io.File;
5import java.io.IOException;
68e2c6d2
NR
6import java.util.ArrayList;
7import java.util.List;
b0e88ebd 8
e42573a0 9import be.nikiroo.fanfix.Instance;
b0e88ebd
NR
10import be.nikiroo.fanfix.data.MetaData;
11import be.nikiroo.fanfix.data.Story;
b0e88ebd
NR
12import be.nikiroo.utils.Progress;
13import be.nikiroo.utils.Version;
62c63b07 14import be.nikiroo.utils.serial.server.ConnectActionClientObject;
b0e88ebd 15
68e2c6d2
NR
16/**
17 * This {@link BasicLibrary} will access a remote server to list the available
a85e8077 18 * stories, and download the ones you try to load to the local directory
68e2c6d2
NR
19 * specified in the configuration.
20 *
21 * @author niki
22 */
23public class RemoteLibrary extends BasicLibrary {
b0e88ebd
NR
24 private String host;
25 private int port;
2070ced5 26 private final String key;
68e2c6d2
NR
27
28 /**
29 * Create a {@link RemoteLibrary} linked to the given server.
30 *
2070ced5
NR
31 * @param key
32 * the key that will allow us to exchange information with the
33 * server
68e2c6d2
NR
34 * @param host
35 * the host to contact or NULL for localhost
36 * @param port
37 * the port to contact it on
38 */
2070ced5
NR
39 public RemoteLibrary(String key, String host, int port) {
40 this.key = key;
b0e88ebd
NR
41 this.host = host;
42 this.port = port;
b0e88ebd
NR
43 }
44
99ccbdf6
NR
45 @Override
46 public String getLibraryName() {
47 return host + ":" + port;
48 }
49
b0e88ebd 50 @Override
b9ce9cad
NR
51 public BufferedImage getCover(final String luid) {
52 final BufferedImage[] result = new BufferedImage[1];
b0e88ebd 53
b9ce9cad
NR
54 try {
55 new ConnectActionClientObject(host, port, true) {
56 @Override
57 public void action(Version serverVersion) throws Exception {
58 Object rep = send(new Object[] { key, "GET_COVER", luid });
59 result[0] = (BufferedImage) rep;
60 }
b0e88ebd 61
b9ce9cad
NR
62 @Override
63 protected void onError(Exception e) {
64 Instance.getTraceHandler().error(e);
65 }
66 }.connect();
67 } catch (Exception e) {
68 Instance.getTraceHandler().error(e);
69 }
b0e88ebd 70
b9ce9cad 71 return result[0];
085a2f9a
NR
72 }
73
74 @Override
75 public BufferedImage getSourceCover(final String source) {
b9ce9cad
NR
76 final BufferedImage[] result = new BufferedImage[1];
77
78 try {
79 new ConnectActionClientObject(host, port, true) {
80 @Override
81 public void action(Version serverVersion) throws Exception {
82 Object rep = send(new Object[] { key, "GET_SOURCE_COVER",
83 source });
84 result[0] = (BufferedImage) rep;
85 }
86
87 @Override
88 protected void onError(Exception e) {
89 Instance.getTraceHandler().error(e);
90 }
91 }.connect();
92 } catch (Exception e) {
93 Instance.getTraceHandler().error(e);
94 }
95
96 return result[0];
b0e88ebd 97 }
68e2c6d2
NR
98
99 @Override
ff05b828 100 public synchronized Story getStory(final String luid, Progress pg) {
b9ce9cad
NR
101 final Progress pgF = pg;
102 final Story[] result = new Story[1];
68e2c6d2 103
b9ce9cad
NR
104 try {
105 new ConnectActionClientObject(host, port, true) {
106 @Override
107 public void action(Version serverVersion) throws Exception {
108 Progress pg = pgF;
109 if (pg == null) {
110 pg = new Progress();
111 }
112
113 Object rep = send(new Object[] { key, "GET_STORY", luid });
114
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());
120 }
121 }
122
123 List<Object> list = new ArrayList<Object>();
124 for (Object obj = send(null); obj != null; obj = send(null)) {
125 list.add(obj);
126 pg.add(1);
127 }
128
129 result[0] = RemoteLibraryServer.rebuildStory(list);
130 pg.done();
131 }
132
133 @Override
134 protected void onError(Exception e) {
135 Instance.getTraceHandler().error(e);
136 }
137 }.connect();
138 } catch (Exception e) {
139 Instance.getTraceHandler().error(e);
140 }
141
142 return result[0];
68e2c6d2
NR
143 }
144
145 @Override
b9ce9cad
NR
146 public synchronized Story save(final Story story, final String luid,
147 Progress pg) throws IOException {
148 final Progress pgF = pg;
149
150 new ConnectActionClientObject(host, port, true) {
151 @Override
152 public void action(Version serverVersion) throws Exception {
153 Progress pg = pgF;
154 if (pg == null) {
155 pg = new Progress();
156 }
157
158 if (story.getMeta().getWords() <= Integer.MAX_VALUE) {
159 pg.setMinMax(0, (int) story.getMeta().getWords());
160 }
161
162 send(new Object[] { key, "SAVE_STORY", luid });
163
164 List<Object> list = RemoteLibraryServer.breakStory(story);
165 for (Object obj : list) {
166 send(obj);
167 pg.add(1);
168 }
169
170 send(null);
171 pg.done();
172 }
173
174 @Override
175 protected void onError(Exception e) {
176 Instance.getTraceHandler().error(e);
177 }
178 }.connect();
085a2f9a
NR
179
180 // because the meta changed:
181 clearCache();
182 story.setMeta(getInfo(luid));
183
184 return story;
68e2c6d2
NR
185 }
186
187 @Override
b9ce9cad
NR
188 public synchronized void delete(final String luid) throws IOException {
189 new ConnectActionClientObject(host, port, true) {
190 @Override
191 public void action(Version serverVersion) throws Exception {
192 send(new Object[] { key, "DELETE_STORY", luid });
193 }
194
195 @Override
196 protected void onError(Exception e) {
197 Instance.getTraceHandler().error(e);
198 }
199 }.connect();
68e2c6d2
NR
200 }
201
202 @Override
b9ce9cad
NR
203 public void setSourceCover(final String source, final String luid) {
204 try {
205 new ConnectActionClientObject(host, port, true) {
206 @Override
207 public void action(Version serverVersion) throws Exception {
208 send(new Object[] { key, "SET_SOURCE_COVER", source, luid });
209 }
210
211 @Override
212 protected void onError(Exception e) {
213 Instance.getTraceHandler().error(e);
214 }
215 }.connect();
216 } catch (IOException e) {
217 Instance.getTraceHandler().error(e);
218 }
68e2c6d2
NR
219 }
220
ff05b828 221 @Override
2249988a 222 public synchronized File getFile(final String luid, Progress pg) {
ff05b828
NR
223 throw new java.lang.InternalError(
224 "Operation not supportorted on remote Libraries");
225 }
226
3bbc86a5
NR
227 /**
228 * Check if this {@link RemoteLibraryServer} is able to connect and identify
229 * to the remote server.
230 *
231 * @return TRUE if it is online
232 */
233 public boolean isOnline() {
234 final Boolean[] result = new Boolean[1];
235
236 result[0] = false;
237 try {
238 new ConnectActionClientObject(host, port, true) {
239 @Override
240 public void action(Version serverVersion) throws Exception {
241 Object rep = send(new Object[] { key, "PING" });
242 result[0] = "PONG".equals(rep);
243 }
244
245 @Override
246 protected void onError(Exception e) {
247 Instance.getTraceHandler().error(e);
248 }
249 }.connect();
250 } catch (Exception e) {
251 Instance.getTraceHandler().error(e);
252 }
253
254 return result[0];
255 }
256
14b57448 257 @Override
b9ce9cad
NR
258 protected List<MetaData> getMetas(Progress pg) {
259 final Progress pgF = pg;
260 final List<MetaData> metas = new ArrayList<MetaData>();
74a40dfb 261
ff05b828 262 try {
62c63b07 263 new ConnectActionClientObject(host, port, true) {
ff05b828
NR
264 @Override
265 public void action(Version serverVersion) throws Exception {
b9ce9cad
NR
266 Progress pg = pgF;
267 if (pg == null) {
268 pg = new Progress();
851dd538 269 }
74a40dfb 270
b9ce9cad 271 Object rep = send(new Object[] { key, "GET_METADATA", "*" });
74a40dfb 272
b9ce9cad
NR
273 while (true) {
274 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
275 break;
276 }
74a40dfb 277
b9ce9cad 278 rep = send(null);
ff05b828 279 }
851dd538 280
b9ce9cad
NR
281 for (MetaData meta : (MetaData[]) rep) {
282 metas.add(meta);
283 }
851dd538
NR
284 }
285
286 @Override
287 protected void onError(Exception e) {
288 Instance.getTraceHandler().error(e);
ff05b828
NR
289 }
290 }.connect();
ff05b828 291 } catch (Exception e) {
62c63b07 292 Instance.getTraceHandler().error(e);
ff05b828 293 }
b9ce9cad
NR
294
295 return metas;
296 }
297
298 @Override
299 protected void clearCache() {
300 }
301
302 // The following methods are only used by Save and Delete in BasicLibrary:
303
304 @Override
305 protected int getNextId() {
306 throw new java.lang.InternalError("Should not have been called");
307 }
308
309 @Override
310 protected void doDelete(String luid) throws IOException {
311 throw new java.lang.InternalError("Should not have been called");
312 }
313
314 @Override
315 protected Story doSave(Story story, Progress pg) throws IOException {
316 throw new java.lang.InternalError("Should not have been called");
e604986c 317 }
b0e88ebd 318}