- 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 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;