From 4b7b9420473c8e302faf6abe788426e6978bf01e Mon Sep 17 00:00:00 2001
From: Jan Kozusznik <jan@kozusznik.cz>
Date: Wed, 22 Nov 2017 16:43:56 +0100
Subject: [PATCH] better progress

---
 .../fiji/haas_java_client/HaaSClient.java     | 73 ++++++++++++++++---
 .../cz/it4i/fiji/scpclient/ScpClient.java     | 53 +++++---------
 .../fiji/scpclient/TransferFileProgress.java  |  2 +-
 3 files changed, 82 insertions(+), 46 deletions(-)

diff --git a/haas-java-client/src/main/java/cz/it4i/fiji/haas_java_client/HaaSClient.java b/haas-java-client/src/main/java/cz/it4i/fiji/haas_java_client/HaaSClient.java
index 9266e90f..6493fe2f 100644
--- a/haas-java-client/src/main/java/cz/it4i/fiji/haas_java_client/HaaSClient.java
+++ b/haas-java-client/src/main/java/cz/it4i/fiji/haas_java_client/HaaSClient.java
@@ -166,11 +166,6 @@ public class HaaSClient {
 					final long[] fileTransfered = { 0 };
 					TransferFileProgress progress = new TransferFileProgress() {
 
-						@Override
-						public long getMinimalDeltaForNotification() {
-							return step;
-						}
-
 						@Override
 						public void dataTransfered(long bytesTransfered) {
 							fileTransfered[0] += bytesTransfered;
@@ -255,7 +250,7 @@ public class HaaSClient {
 				String[] files = getFileTransfer().listChangedFilesForJob(jobId, getSessionID());
 				List<Long> fileSizes = getSizes(Arrays.asList(files).stream()
 						.map(filename -> "'" + ft.getSharedBasepath() + "/" + filename + "'").collect(Collectors.toList()),
-						scpClient, notifier);
+						scpClient, new P_ProgressNotifierDecorator4Size(notifier));
 				final long totalFileSize = fileSizes.stream().mapToLong(i -> i.longValue()).sum();
 				int[] idx = { 0 };
 				final int[] totalDownloaded = { 0 };
@@ -271,11 +266,6 @@ public class HaaSClient {
 					notifier.addItem(item = fileName);
 					scpClient.download(fileToDownload, rFile, new TransferFileProgress() {
 
-						@Override
-						public long getMinimalDeltaForNotification() {
-							return totalFileSize / 100;
-						}
-
 						@Override
 						public void dataTransfered(long bytesTransfered) {
 							totalDownloaded[0] += bytesTransfered;
@@ -405,4 +395,65 @@ public class HaaSClient {
 		return sessionID;
 	}
 
+	private class P_ProgressNotifierDecorator4Size extends P_ProgressNotifierDecorator{
+
+		private static final int SIZE_RATIO = 20;
+		public P_ProgressNotifierDecorator4Size(ProgressNotifier notifier) {
+			super(notifier);
+			
+		}
+		@Override
+		public void setItemCount(int count, int total) {
+			super.setItemCount(count, total);
+			setCount(count, total * SIZE_RATIO);
+		}
+	}
+	
+	private class P_ProgressNotifierDecorator implements ProgressNotifier{
+		private ProgressNotifier notifier;
+
+		
+		
+		public P_ProgressNotifierDecorator(ProgressNotifier notifier) {
+			super();
+			this.notifier = notifier;
+		}
+
+
+
+		public void setTitle(String title) {
+			notifier.setTitle(title);
+		}
+
+
+
+		public void setCount(int count, int total) {
+			notifier.setCount(count, total);
+		}
+
+
+
+		public void addItem(Object item) {
+			notifier.addItem(item);
+		}
+
+
+
+		public void setItemCount(int count, int total) {
+			notifier.setItemCount(count, total);
+		}
+
+
+
+		public void itemDone(Object item) {
+			notifier.itemDone(item);
+		}
+
+
+
+		public void done() {
+			notifier.done();
+		}
+
+	}
 }
diff --git a/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/ScpClient.java b/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/ScpClient.java
index 8a69e78d..88bcfdb4 100644
--- a/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/ScpClient.java
+++ b/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/ScpClient.java
@@ -27,15 +27,10 @@ public class ScpClient implements Closeable {
 	private JSch jsch = new JSch();
 	private Session session;
 	private TransferFileProgress dummyProgress = new TransferFileProgress() {
-		
-		@Override
-		public long getMinimalDeltaForNotification() {
-			return 0;
-		}
-		
+
 		@Override
 		public void dataTransfered(long bytesTransfered) {
-			
+
 		}
 	};
 
@@ -67,7 +62,7 @@ public class ScpClient implements Closeable {
 	}
 
 	public void download(String lfile, Path rFile) throws JSchException, IOException {
-		download(lfile, rFile, dummyProgress );
+		download(lfile, rFile, dummyProgress);
 	}
 
 	public boolean download(String lfile, Path rfile, TransferFileProgress progress) throws JSchException, IOException {
@@ -132,7 +127,6 @@ public class ScpClient implements Closeable {
 					// read a content of lfile
 					try (OutputStream fos = Files.newOutputStream(rfile)) {
 						int foo;
-						long totalTransfered = 0;
 						while (true) {
 							if (buf.length < filesize)
 								foo = buf.length;
@@ -144,11 +138,7 @@ public class ScpClient implements Closeable {
 								break;
 							}
 							fos.write(buf, 0, foo);
-							totalTransfered += foo;
-							if(totalTransfered >= progress.getMinimalDeltaForNotification()) {
-								progress.dataTransfered(totalTransfered);
-								totalTransfered = 0;
-							}
+							progress.dataTransfered(foo);
 							filesize -= foo;
 							if (filesize == 0L)
 								break;
@@ -173,9 +163,9 @@ public class ScpClient implements Closeable {
 	}
 
 	public boolean upload(Path file, String rfile) throws JSchException, IOException {
-		return upload(file, rfile,dummyProgress);
+		return upload(file, rfile, dummyProgress);
 	}
-	
+
 	public boolean upload(Path file, String rfile, TransferFileProgress progress) throws JSchException, IOException {
 
 		Session session = connectionSession();
@@ -217,18 +207,13 @@ public class ScpClient implements Closeable {
 			}
 			byte[] buf = new byte[getBufferSize()];
 			// send a content of lfile
-			long transfered = 0;
 			try (InputStream fis = Files.newInputStream(file)) {
 				while (true) {
 					int len = fis.read(buf, 0, buf.length);
 					if (len <= 0)
 						break;
 					out.write(buf, 0, len); // out.flush();
-					transfered += len;
-					if(transfered >= progress.getMinimalDeltaForNotification()) {
-						progress.dataTransfered(transfered);
-						transfered = 0;
-					}
+					progress.dataTransfered(len);
 				}
 			}
 			// send '\0'
@@ -255,7 +240,7 @@ public class ScpClient implements Closeable {
 
 		try {
 			channel.connect();
-			return  ((List<LsEntry>)((ChannelSftp) channel).ls(lfile)).stream().map(atr -> atr.getAttrs().getSize())
+			return ((List<LsEntry>) ((ChannelSftp) channel).ls(lfile)).stream().map(atr -> atr.getAttrs().getSize())
 					.collect(Collectors.toList());
 
 		} catch (SftpException e) {
@@ -355,35 +340,35 @@ public class ScpClient implements Closeable {
 
 	public long size(String lfile) throws JSchException, IOException {
 		Session session = connectionSession();
-	
+
 		// exec 'scp -f rfile' remotely
 		String command = "scp -f " + lfile;
 		Channel channel = session.openChannel("exec");
-	
+
 		try {
 			((ChannelExec) channel).setCommand(command);
-	
+
 			// get I/O streams for remote scp
 			try (OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream()) {
-	
+
 				channel.connect();
-	
+
 				byte[] buf = new byte[getBufferSize()];
-	
+
 				// send '\0'
 				buf[0] = 0;
 				out.write(buf, 0, 1);
 				out.flush();
-	
+
 				while (true) {
 					int c = checkAck(in);
 					if (c != 'C') {
 						break;
 					}
-	
+
 					// read '0644 '
 					in.read(buf, 0, 5);
-	
+
 					long filesize = 0L;
 					while (true) {
 						if (in.read(buf, 0, 1) < 0) {
@@ -395,10 +380,10 @@ public class ScpClient implements Closeable {
 						filesize = filesize * 10L + (long) (buf[0] - '0');
 					}
 					return filesize;
-	
+
 				}
 			}
-	
+
 		} finally {
 			channel.disconnect();
 		}
diff --git a/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/TransferFileProgress.java b/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/TransferFileProgress.java
index 1e0f9632..af9f1d45 100644
--- a/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/TransferFileProgress.java
+++ b/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/TransferFileProgress.java
@@ -1,6 +1,6 @@
 package cz.it4i.fiji.scpclient;
 
 public interface TransferFileProgress {
-	long getMinimalDeltaForNotification();
+	
 	void dataTransfered(long bytesTransfered);
 }
-- 
GitLab