+
+ @Override
+ protected void onError(Exception e) {
+ if (e instanceof SSLException) {
+ long now = System.currentTimeMillis();
+ System.out.println(StringUtils.fromTime(now) + ": "
+ + "[Client connection refused (bad key)]");
+ } else {
+ getTraceHandler().error(e);
+ }
+ }
+
+ /**
+ * Break a story in multiple {@link Object}s for easier serialisation.
+ *
+ * @param story
+ * the {@link Story} to break
+ *
+ * @return the list of {@link Object}s
+ */
+ static List<Object> breakStory(Story story) {
+ List<Object> list = new ArrayList<Object>();
+
+ story = story.clone();
+ list.add(story);
+
+ if (story.getMeta().isImageDocument()) {
+ for (Chapter chap : story) {
+ list.add(chap);
+ list.addAll(chap.getParagraphs());
+ chap.setParagraphs(new ArrayList<Paragraph>());
+ }
+ story.setChapters(new ArrayList<Chapter>());
+ }
+
+ return list;
+ }
+
+ /**
+ * Rebuild a story from a list of broke up {@link Story} parts.
+ *
+ * @param list
+ * the list of {@link Story} parts
+ *
+ * @return the reconstructed {@link Story}
+ */
+ static Story rebuildStory(List<Object> list) {
+ Story story = null;
+ Chapter chap = null;
+
+ for (Object obj : list) {
+ if (obj instanceof Story) {
+ story = (Story) obj;
+ } else if (obj instanceof Chapter) {
+ chap = (Chapter) obj;
+ story.getChapters().add(chap);
+ } else if (obj instanceof Paragraph) {
+ chap.getParagraphs().add((Paragraph) obj);
+ }
+ }
+
+ return story;
+ }
+
+ /**
+ * Update the {@link Progress} with the adequate {@link Object} received
+ * from the network via {@link RemoteLibraryServer}.
+ *
+ * @param pg
+ * the {@link Progress} to update
+ * @param rep
+ * the object received from the network
+ *
+ * @return TRUE if it was a progress event, FALSE if not
+ */
+ static boolean updateProgress(Progress pg, Object rep) {
+ if (rep instanceof Integer[]) {
+ Integer[] a = (Integer[]) rep;
+ if (a.length == 3) {
+ int min = a[0];
+ int max = a[1];
+ int progress = a[2];
+
+ if (min >= 0 && min <= max) {
+ pg.setMinMax(min, max);
+ pg.setProgress(progress);
+
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Create a {@link Progress} that will forward its progress over the
+ * network.
+ *
+ * @param action
+ * the {@link ConnectActionServerObject} to use to forward it
+ *
+ * @return the {@link Progress}
+ */
+ private Progress createPgForwarder(final ConnectActionServerObject action) {
+ final Boolean[] isDoneForwarded = new Boolean[] { false };
+ final Progress pg = new Progress() {
+ @Override
+ public boolean isDone() {
+ return isDoneForwarded[0];
+ }
+ };
+
+ final Integer[] p = new Integer[] { -1, -1, -1 };
+ final Long[] lastTime = new Long[] { new Date().getTime() };
+ pg.addProgressListener(new ProgressListener() {
+ @Override
+ public void progress(Progress progress, String name) {
+ int min = pg.getMin();
+ int max = pg.getMax();
+ int relativeProgress = min
+ + (int) Math.round(pg.getRelativeProgress()
+ * (max - min));
+
+ // Do not re-send the same value twice over the wire,
+ // unless more than 2 seconds have elapsed (to maintain the
+ // connection)
+ if ((p[0] != min || p[1] != max || p[2] != relativeProgress)
+ || (new Date().getTime() - lastTime[0] > 2000)) {
+ p[0] = min;
+ p[1] = max;
+ p[2] = relativeProgress;
+
+ try {
+ action.send(new Integer[] { min, max, relativeProgress });
+ action.rec();
+ } catch (Exception e) {
+ getTraceHandler().error(e);
+ }
+
+ lastTime[0] = new Date().getTime();
+ }
+
+ isDoneForwarded[0] = (pg.getProgress() >= pg.getMax());
+ }
+ });
+
+ return pg;
+ }
+
+ // with 30 seconds timeout
+ private void forcePgDoneSent(Progress pg) {
+ long start = new Date().getTime();
+ pg.done();
+ while (!pg.isDone() && new Date().getTime() - start < 30000) {
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ getTraceHandler().error(e);
+ }
+ }
+ }