- private static Object getNextObject(Object clientOrServer)
- throws NoSuchFieldException, NoSuchMethodException,
- ClassNotFoundException, IOException {
- if (clientOrServer instanceof ConnectActionClientObject) {
- ConnectActionClientObject client = (ConnectActionClientObject) clientOrServer;
- return client.send(null);
- } else if (clientOrServer instanceof ConnectActionServerObject) {
- ConnectActionServerObject server = (ConnectActionServerObject) clientOrServer;
- Object obj = server.rec();
- server.send(null);
- return obj;
- } else {
- throw new ClassNotFoundException();
- }
+ /**
+ * 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 Object[] pMeta = new MetaData[1];
+ final String[] pName = new String[1];
+ final Long[] lastTime = new Long[] { new Date().getTime() };
+ pg.addProgressListener(new ProgressListener() {
+ @Override
+ public void progress(Progress progress, String name) {
+ Object meta = pg.get("meta");
+ if (meta instanceof MetaData) {
+ meta = removeCover((MetaData)meta);
+ }
+
+ int min = pg.getMin();
+ int max = pg.getMax();
+ int rel = min
+ + (int) Math.round(pg.getRelativeProgress()
+ * (max - min));
+
+ boolean samePg = p[0] == min && p[1] == max && p[2] == rel;
+
+ // Do not re-send the same value twice over the wire,
+ // unless more than 2 seconds have elapsed (to maintain the
+ // connection)
+ if (!samePg || !same(pMeta[0], meta)
+ || !same(pName[0], name) //
+ || (new Date().getTime() - lastTime[0] > 2000)) {
+ p[0] = min;
+ p[1] = max;
+ p[2] = rel;
+ pMeta[0] = meta;
+ pName[0] = name;
+
+ try {
+ action.send(new Object[] { "UPDATE", min, max, rel,
+ meta, name });
+ action.rec();
+ } catch (Exception e) {
+ getTraceHandler().error(e);
+ }
+
+ lastTime[0] = new Date().getTime();
+ }
+
+ isDoneForwarded[0] = (pg.getProgress() >= pg.getMax());
+ }
+ });
+
+ return pg;