Remote, JPG/PNG:
[fanfix.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
14b57448 227 @Override
b9ce9cad
NR
228 protected List<MetaData> getMetas(Progress pg) {
229 final Progress pgF = pg;
230 final List<MetaData> metas = new ArrayList<MetaData>();
74a40dfb 231
ff05b828 232 try {
62c63b07 233 new ConnectActionClientObject(host, port, true) {
ff05b828
NR
234 @Override
235 public void action(Version serverVersion) throws Exception {
b9ce9cad
NR
236 Progress pg = pgF;
237 if (pg == null) {
238 pg = new Progress();
851dd538 239 }
74a40dfb 240
b9ce9cad 241 Object rep = send(new Object[] { key, "GET_METADATA", "*" });
74a40dfb 242
b9ce9cad
NR
243 while (true) {
244 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
245 break;
246 }
74a40dfb 247
b9ce9cad 248 rep = send(null);
ff05b828 249 }
851dd538 250
b9ce9cad
NR
251 for (MetaData meta : (MetaData[]) rep) {
252 metas.add(meta);
253 }
851dd538
NR
254 }
255
256 @Override
257 protected void onError(Exception e) {
258 Instance.getTraceHandler().error(e);
ff05b828
NR
259 }
260 }.connect();
ff05b828 261 } catch (Exception e) {
62c63b07 262 Instance.getTraceHandler().error(e);
ff05b828 263 }
b9ce9cad
NR
264
265 return metas;
266 }
267
268 @Override
269 protected void clearCache() {
270 }
271
272 // The following methods are only used by Save and Delete in BasicLibrary:
273
274 @Override
275 protected int getNextId() {
276 throw new java.lang.InternalError("Should not have been called");
277 }
278
279 @Override
280 protected void doDelete(String luid) throws IOException {
281 throw new java.lang.InternalError("Should not have been called");
282 }
283
284 @Override
285 protected Story doSave(Story story, Progress pg) throws IOException {
286 throw new java.lang.InternalError("Should not have been called");
e604986c 287 }
b0e88ebd 288}