ea155eb88d14515cf4334dfc38eb1843e0b9f005
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
1 package be.nikiroo.fanfix.library;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.List;
8
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;
15
16 /**
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.
20 *
21 * @author niki
22 */
23 public class RemoteLibrary extends BasicLibrary {
24 private String host;
25 private int port;
26 private final String key;
27
28 /**
29 * Create a {@link RemoteLibrary} linked to the given server.
30 *
31 * @param key
32 * the key that will allow us to exchange information with the
33 * server
34 * @param host
35 * the host to contact or NULL for localhost
36 * @param port
37 * the port to contact it on
38 */
39 public RemoteLibrary(String key, String host, int port) {
40 this.key = key;
41 this.host = host;
42 this.port = port;
43 }
44
45 @Override
46 public String getLibraryName() {
47 return host + ":" + port;
48 }
49
50 @Override
51 public BufferedImage getCover(final String luid) {
52 final BufferedImage[] result = new BufferedImage[1];
53
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 }
61
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 }
70
71 return result[0];
72 }
73
74 @Override
75 public BufferedImage getSourceCover(final String source) {
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];
97 }
98
99 @Override
100 public synchronized Story getStory(final String luid, Progress pg) {
101 final Progress pgF = pg;
102 final Story[] result = new Story[1];
103
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];
143 }
144
145 @Override
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();
179
180 // because the meta changed:
181 clearCache();
182 story.setMeta(getInfo(luid));
183
184 return story;
185 }
186
187 @Override
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();
200 }
201
202 @Override
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 }
219 }
220
221 @Override
222 public synchronized File getFile(final String luid, Progress pg) {
223 throw new java.lang.InternalError(
224 "Operation not supportorted on remote Libraries");
225 }
226
227 @Override
228 protected List<MetaData> getMetas(Progress pg) {
229 final Progress pgF = pg;
230 final List<MetaData> metas = new ArrayList<MetaData>();
231
232 try {
233 new ConnectActionClientObject(host, port, true) {
234 @Override
235 public void action(Version serverVersion) throws Exception {
236 Progress pg = pgF;
237 if (pg == null) {
238 pg = new Progress();
239 }
240
241 Object rep = send(new Object[] { key, "GET_METADATA", "*" });
242
243 while (true) {
244 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
245 break;
246 }
247
248 rep = send(null);
249 }
250
251 for (MetaData meta : (MetaData[]) rep) {
252 metas.add(meta);
253 }
254 }
255
256 @Override
257 protected void onError(Exception e) {
258 Instance.getTraceHandler().error(e);
259 }
260 }.connect();
261 } catch (Exception e) {
262 Instance.getTraceHandler().error(e);
263 }
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");
287 }
288 }