f4075dd7bf5df4e9fb1d734bc8d47fdb95852b5e
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibraryServer.java
1 package be.nikiroo.fanfix.library;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.net.ssl.SSLException;
12
13 import be.nikiroo.fanfix.Instance;
14 import be.nikiroo.fanfix.bundles.Config;
15 import be.nikiroo.fanfix.data.Chapter;
16 import be.nikiroo.fanfix.data.MetaData;
17 import be.nikiroo.fanfix.data.Paragraph;
18 import be.nikiroo.fanfix.data.Story;
19 import be.nikiroo.utils.Progress;
20 import be.nikiroo.utils.Progress.ProgressListener;
21 import be.nikiroo.utils.StringUtils;
22 import be.nikiroo.utils.Version;
23 import be.nikiroo.utils.serial.server.ConnectActionServerObject;
24 import be.nikiroo.utils.serial.server.ServerObject;
25
26 /**
27 * Create a new remote server that will listen for orders on the given port.
28 * <p>
29 * The available commands are given as arrays of objects (first item is the
30 * command, the rest are the arguments).
31 * <p>
32 * All the commands are always prefixed by the subkey (which can be EMPTY if
33 * none).
34 * <p>
35 * <ul>
36 * <li>PING: will return the mode if the key is accepted (mode can be: "r/o" or
37 * "r/w")</li>
38 * <li>GET_METADATA *: will return the metadata of all the stories in the
39 * library (array)</li> *
40 * <li>GET_METADATA [luid]: will return the metadata of the story of LUID luid</li>
41 * <li>GET_STORY [luid]: will return the given story if it exists (or NULL if
42 * not)</li>
43 * <li>SAVE_STORY [luid]: save the story (that must be sent just after the
44 * command) with the given LUID, then return the LUID</li>
45 * <li>IMPORT [url]: save the story found at the given URL, then return the LUID
46 * </li>
47 * <li>DELETE_STORY [luid]: delete the story of LUID luid</li>
48 * <li>GET_COVER [luid]: return the cover of the story</li>
49 * <li>GET_CUSTOM_COVER ["SOURCE"|"AUTHOR"] [source]: return the cover for this
50 * source/author</li>
51 * <li>SET_COVER ["SOURCE"|"AUTHOR"] [value] [luid]: set the default cover for
52 * the given source/author to the cover of the story denoted by luid</li>
53 * <li>CHANGE_SOURCE [luid] [new source]: change the source of the story of LUID
54 * luid</li>
55 * <li>EXIT: stop the server</li>
56 * </ul>
57 *
58 * @author niki
59 */
60 public class RemoteLibraryServer extends ServerObject {
61 private Map<Long, String> commands = new HashMap<Long, String>();
62 private Map<Long, Long> times = new HashMap<Long, Long>();
63 private Map<Long, Boolean> wls = new HashMap<Long, Boolean>();
64 private Map<Long, Boolean> rws = new HashMap<Long, Boolean>();
65
66 /**
67 * Create a new remote server (will not be active until
68 * {@link RemoteLibraryServer#start()} is called).
69 * <p>
70 * Note: the key we use here is the encryption key (it must not contain a
71 * subkey).
72 *
73 * @param key
74 * the key that will restrict access to this server
75 * @param port
76 * the port to listen on
77 *
78 * @throws IOException
79 * in case of I/O error
80 */
81 public RemoteLibraryServer(String key, int port) throws IOException {
82 super("Fanfix remote library", port, key);
83 setTraceHandler(Instance.getTraceHandler());
84 }
85
86 @Override
87 protected Object onRequest(ConnectActionServerObject action,
88 Version clientVersion, Object data, long id) throws Exception {
89 long start = new Date().getTime();
90
91 // defaults are positive (as previous versions without the feature)
92 boolean rw = true;
93 boolean wl = true;
94
95 String subkey = "";
96 String command = "";
97 Object[] args = new Object[0];
98 if (data instanceof Object[]) {
99 Object[] dataArray = (Object[]) data;
100 if (dataArray.length > 0) {
101 subkey = "" + dataArray[0];
102 }
103 if (dataArray.length > 1) {
104 command = "" + dataArray[1];
105
106 args = new Object[dataArray.length - 2];
107 for (int i = 2; i < dataArray.length; i++) {
108 args[i - 2] = dataArray[i];
109 }
110 }
111 }
112
113 List<String> whitelist = Instance.getConfig().getList(
114 Config.SERVER_WHITELIST);
115 if (whitelist == null) {
116 whitelist = new ArrayList<String>();
117 }
118
119 if (whitelist.isEmpty()) {
120 wl = false;
121 }
122
123 rw = Instance.getConfig().getBoolean(Config.SERVER_RW, rw);
124 if (!subkey.isEmpty()) {
125 List<String> allowed = Instance.getConfig().getList(
126 Config.SERVER_ALLOWED_SUBKEYS);
127 if (allowed.contains(subkey)) {
128 if ((subkey + "|").contains("|rw|")) {
129 rw = true;
130 }
131 if ((subkey + "|").contains("|wl|")) {
132 wl = false; // |wl| = bypass whitelist
133 }
134 }
135 }
136
137 String mode = display(wl, rw);
138
139 String trace = mode + "[ " + command + "] ";
140 for (Object arg : args) {
141 trace += arg + " ";
142 }
143 System.out.println(trace);
144
145 Object rep = doRequest(action, command, args, rw, whitelist);
146
147 commands.put(id, command);
148 wls.put(id, wl);
149 rws.put(id, rw);
150 times.put(id, (new Date().getTime() - start));
151
152 return rep;
153 }
154
155 private String display(boolean whitelist, boolean rw) {
156 String mode = "";
157 if (!rw) {
158 mode += "RO: ";
159 }
160 if (whitelist) {
161 mode += "WL: ";
162 }
163
164 return mode;
165 }
166
167 @Override
168 protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
169 boolean whitelist = wls.get(id);
170 boolean rw = rws.get(id);
171 wls.remove(id);
172 rws.remove(id);
173
174 String rec = StringUtils.formatNumber(bytesReceived) + "b";
175 String sent = StringUtils.formatNumber(bytesSent) + "b";
176 System.out.println(String.format("%s[>%s]: (%s sent, %s rec) in %d ms",
177 display(whitelist, rw), commands.get(id), sent, rec,
178 times.get(id)));
179
180 commands.remove(id);
181 times.remove(id);
182 }
183
184 private Object doRequest(ConnectActionServerObject action, String command,
185 Object[] args, boolean rw, List<String> whitelist)
186 throws NoSuchFieldException, NoSuchMethodException,
187 ClassNotFoundException, IOException {
188 if ("PING".equals(command)) {
189 return rw ? "r/w" : "r/o";
190 } else if ("GET_METADATA".equals(command)) {
191 if ("*".equals(args[0])) {
192 Progress pg = createPgForwarder(action);
193
194 List<MetaData> metas = new ArrayList<MetaData>();
195
196 for (MetaData meta : Instance.getLibrary().getMetas(pg)) {
197 MetaData light;
198 if (meta.getCover() == null) {
199 light = meta;
200 } else {
201 light = meta.clone();
202 light.setCover(null);
203 }
204
205 metas.add(light);
206 }
207
208 forcePgDoneSent(pg);
209 return metas.toArray(new MetaData[] {});
210 }
211
212 return new MetaData[] { Instance.getLibrary().getInfo(
213 (String) args[0]) };
214 } else if ("GET_STORY".equals(command)) {
215 MetaData meta = Instance.getLibrary().getInfo((String) args[0]);
216 meta = meta.clone();
217 meta.setCover(null);
218
219 action.send(meta);
220 action.rec();
221
222 Story story = Instance.getLibrary()
223 .getStory((String) args[0], null);
224 for (Object obj : breakStory(story)) {
225 action.send(obj);
226 action.rec();
227 }
228 } else if ("SAVE_STORY".equals(command)) {
229 List<Object> list = new ArrayList<Object>();
230
231 action.send(null);
232 Object obj = action.rec();
233 while (obj != null) {
234 list.add(obj);
235 action.send(null);
236 obj = action.rec();
237 }
238
239 Story story = rebuildStory(list);
240 Instance.getLibrary().save(story, (String) args[0], null);
241 return story.getMeta().getLuid();
242 } else if ("IMPORT".equals(command)) {
243 Progress pg = createPgForwarder(action);
244 Story story = Instance.getLibrary().imprt(
245 new URL((String) args[0]), pg);
246 forcePgDoneSent(pg);
247 return story.getMeta().getLuid();
248 } else if ("DELETE_STORY".equals(command)) {
249 Instance.getLibrary().delete((String) args[0]);
250 } else if ("GET_COVER".equals(command)) {
251 return Instance.getLibrary().getCover((String) args[0]);
252 } else if ("GET_CUSTOM_COVER".equals(command)) {
253 if ("SOURCE".equals(args[0])) {
254 return Instance.getLibrary().getCustomSourceCover(
255 (String) args[1]);
256 } else if ("AUTHOR".equals(args[0])) {
257 return Instance.getLibrary().getCustomAuthorCover(
258 (String) args[1]);
259 } else {
260 return null;
261 }
262 } else if ("SET_COVER".equals(command)) {
263 if ("SOURCE".equals(args[0])) {
264 Instance.getLibrary().setSourceCover((String) args[1],
265 (String) args[2]);
266 } else if ("AUTHOR".equals(args[0])) {
267 Instance.getLibrary().setAuthorCover((String) args[1],
268 (String) args[2]);
269 }
270 } else if ("CHANGE_STA".equals(command)) {
271 Progress pg = createPgForwarder(action);
272 Instance.getLibrary().changeSTA((String) args[0], (String) args[1],
273 (String) args[2], (String) args[3], pg);
274 forcePgDoneSent(pg);
275 } else if ("EXIT".equals(command)) {
276 stop(0, false);
277 }
278
279 return null;
280 }
281
282 @Override
283 protected void onError(Exception e) {
284 if (e instanceof SSLException) {
285 System.out.println("[Client connection refused (bad key)]");
286 } else {
287 getTraceHandler().error(e);
288 }
289 }
290
291 /**
292 * Break a story in multiple {@link Object}s for easier serialisation.
293 *
294 * @param story
295 * the {@link Story} to break
296 *
297 * @return the list of {@link Object}s
298 */
299 static List<Object> breakStory(Story story) {
300 List<Object> list = new ArrayList<Object>();
301
302 story = story.clone();
303 list.add(story);
304
305 if (story.getMeta().isImageDocument()) {
306 for (Chapter chap : story) {
307 list.add(chap);
308 list.addAll(chap.getParagraphs());
309 chap.setParagraphs(new ArrayList<Paragraph>());
310 }
311 story.setChapters(new ArrayList<Chapter>());
312 }
313
314 return list;
315 }
316
317 /**
318 * Rebuild a story from a list of broke up {@link Story} parts.
319 *
320 * @param list
321 * the list of {@link Story} parts
322 *
323 * @return the reconstructed {@link Story}
324 */
325 static Story rebuildStory(List<Object> list) {
326 Story story = null;
327 Chapter chap = null;
328
329 for (Object obj : list) {
330 if (obj instanceof Story) {
331 story = (Story) obj;
332 } else if (obj instanceof Chapter) {
333 chap = (Chapter) obj;
334 story.getChapters().add(chap);
335 } else if (obj instanceof Paragraph) {
336 chap.getParagraphs().add((Paragraph) obj);
337 }
338 }
339
340 return story;
341 }
342
343 /**
344 * Update the {@link Progress} with the adequate {@link Object} received
345 * from the network via {@link RemoteLibraryServer}.
346 *
347 * @param pg
348 * the {@link Progress} to update
349 * @param rep
350 * the object received from the network
351 *
352 * @return TRUE if it was a progress event, FALSE if not
353 */
354 static boolean updateProgress(Progress pg, Object rep) {
355 if (rep instanceof Integer[]) {
356 Integer[] a = (Integer[]) rep;
357 if (a.length == 3) {
358 int min = a[0];
359 int max = a[1];
360 int progress = a[2];
361
362 if (min >= 0 && min <= max) {
363 pg.setMinMax(min, max);
364 pg.setProgress(progress);
365
366 return true;
367 }
368 }
369 }
370
371 return false;
372 }
373
374 /**
375 * Create a {@link Progress} that will forward its progress over the
376 * network.
377 *
378 * @param action
379 * the {@link ConnectActionServerObject} to use to forward it
380 *
381 * @return the {@link Progress}
382 */
383 private Progress createPgForwarder(final ConnectActionServerObject action) {
384 final Boolean[] isDoneForwarded = new Boolean[] { false };
385 final Progress pg = new Progress() {
386 @Override
387 public boolean isDone() {
388 return isDoneForwarded[0];
389 }
390 };
391
392 final Integer[] p = new Integer[] { -1, -1, -1 };
393 final Long[] lastTime = new Long[] { new Date().getTime() };
394 pg.addProgressListener(new ProgressListener() {
395 @Override
396 public void progress(Progress progress, String name) {
397 int min = pg.getMin();
398 int max = pg.getMax();
399 int relativeProgress = min
400 + (int) Math.round(pg.getRelativeProgress()
401 * (max - min));
402
403 // Do not re-send the same value twice over the wire,
404 // unless more than 2 seconds have elapsed (to maintain the
405 // connection)
406 if ((p[0] != min || p[1] != max || p[2] != relativeProgress)
407 || (new Date().getTime() - lastTime[0] > 2000)) {
408 p[0] = min;
409 p[1] = max;
410 p[2] = relativeProgress;
411
412 try {
413 action.send(new Integer[] { min, max, relativeProgress });
414 action.rec();
415 } catch (Exception e) {
416 getTraceHandler().error(e);
417 }
418
419 lastTime[0] = new Date().getTime();
420 }
421
422 isDoneForwarded[0] = (pg.getProgress() >= pg.getMax());
423 }
424 });
425
426 return pg;
427 }
428
429 // with 30 seconds timeout
430 private void forcePgDoneSent(Progress pg) {
431 long start = new Date().getTime();
432 pg.done();
433 while (!pg.isDone() && new Date().getTime() - start < 30000) {
434 try {
435 Thread.sleep(100);
436 } catch (InterruptedException e) {
437 getTraceHandler().error(e);
438 }
439 }
440 }
441 }