From 28beec0648b5a149ff9c3e2f39e621470522cdb3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Thu, 8 Mar 2018 14:03:39 +0100
Subject: [PATCH] iss1026: fix scp auth error

---
 .../SPIMPipelineProgressViewController.java   |  5 +-
 .../ui/TaskComputationAdapter.java            | 53 ++++++++++++++-----
 .../ui/TaskComputationControl.java            | 17 +++---
 .../ui/TaskComputationWindow.java             |  5 +-
 4 files changed, 56 insertions(+), 24 deletions(-)

diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/SPIMPipelineProgressViewController.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/SPIMPipelineProgressViewController.java
index 7e984be5..2786964c 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/SPIMPipelineProgressViewController.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/SPIMPipelineProgressViewController.java
@@ -78,11 +78,13 @@ public class SPIMPipelineProgressViewController extends BorderPane implements Cl
 	private Timer timer;
 	private ObservableTaskRegistry registry;
 	private ExecutorService executorServiceWS;
+	private ExecutorService executorServiceScp;
 	private Executor executorFx = new FXFrameExecutorService();
 	private Window root;
 
 	public SPIMPipelineProgressViewController() {
 		executorServiceWS = Executors.newSingleThreadExecutor();
+		executorServiceScp = Executors.newSingleThreadExecutor();
 		init();
 
 	}
@@ -106,6 +108,7 @@ public class SPIMPipelineProgressViewController extends BorderPane implements Cl
 	public void close() {
 		timer.cancel();
 		executorServiceWS.shutdown();
+		executorServiceScp.shutdown();
 	}
 
 	@Override
@@ -129,7 +132,7 @@ public class SPIMPipelineProgressViewController extends BorderPane implements Cl
 	}
 
 	private void proof(ObservableValue<Task> task, int columnIndex) {
-		ModalDialogs.doModal(new TaskComputationWindow(root, task.getValue().getComputations().get(columnIndex - 1)),
+		ModalDialogs.doModal(new TaskComputationWindow(root, task.getValue().getComputations().get(columnIndex - 1), executorServiceScp),
 				WindowConstants.DISPOSE_ON_CLOSE);
 	}
 
diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationAdapter.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationAdapter.java
index db7c53dd..323f34a1 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationAdapter.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationAdapter.java
@@ -6,6 +6,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.Timer;
 import java.util.TimerTask;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
 import java.util.stream.Collectors;
 
 import org.slf4j.Logger;
@@ -29,20 +32,39 @@ public class TaskComputationAdapter implements Closeable {
 
 	private final List<ObservableLog> logs = new LinkedList<>();
 
-	private final Timer timer;
+	private Timer timer;
 
-	public TaskComputationAdapter(TaskComputation computation) {
+	private ExecutorService scpExecutor;
+
+	public TaskComputationAdapter(TaskComputation computation, ExecutorService scpExecutor) {
 		this.computation = computation;
+		this.scpExecutor = scpExecutor;
 		timer = new Timer();
-		Map<String, Long> sizes = computation.getOutFileSizes();
-		computation.getOutputs().forEach(outputFile -> addOutputFile(outputFile, sizes.get(outputFile)));
-		computation.getLogs().forEach(log->logs.add(new ObservableLog(log)));
-		timer.scheduleAtFixedRate(new P_TimerTask(), Constants.HAAS_TIMEOUT, Constants.HAAS_TIMEOUT);
+	}
+
+	public void init() {
+		Future<?> future = scpExecutor.submit(() -> {
+			Map<String, Long> sizes = computation.getOutFileSizes();
+			computation.getOutputs().forEach(outputFile -> addOutputFile(outputFile, sizes.get(outputFile)));
+			computation.getLogs().forEach(log -> logs.add(new ObservableLog(log)));
+		});
+		try {
+			future.get();
+		} catch (InterruptedException | ExecutionException e) {
+			log.error(e.getMessage(), e);
+		}
+		synchronized (this) {
+			if(timer != null) {
+				timer.schedule(new P_TimerTask(), Constants.HAAS_TIMEOUT, Constants.HAAS_TIMEOUT);
+			}
+		}
+		
 	}
 
 	@Override
-	public void close() {
+	public synchronized void close() {
 		timer.cancel();
+		timer = null;
 	}
 
 	public List<ObservableValue<RemoteFileInfo>> getOutputs() {
@@ -150,11 +172,18 @@ public class TaskComputationAdapter implements Closeable {
 
 		@Override
 		public void run() {
-			Map<String, Long> sizes = computation.getOutFileSizes();
-			Map<String, Log> logs = computation.getLogs().stream()
-					.collect(Collectors.<Log, String, Log>toMap((Log log) -> log.getName(), (Log log) -> log));
-			TaskComputationAdapter.this.logs.forEach(log->((ObservableLog) log).setContentValue(logs.get(log.getName())));
-			outputs.forEach(value -> ((ObservableOutputFile) value).setSize(sizes.get(value.getValue().getName())));
+			try {
+				scpExecutor.submit(() -> {
+					Map<String, Long> sizes = computation.getOutFileSizes();
+					Map<String, Log> logs = computation.getLogs().stream()
+							.collect(Collectors.<Log, String, Log>toMap((Log log) -> log.getName(), (Log log) -> log));
+					TaskComputationAdapter.this.logs
+							.forEach(log -> ((ObservableLog) log).setContentValue(logs.get(log.getName())));
+					outputs.forEach(value -> ((ObservableOutputFile) value).setSize(sizes.get(value.getValue().getName())));
+				}).get();
+			} catch (InterruptedException | ExecutionException e) {
+				log.error(e.getMessage(), e);
+			}
 		}
 
 	}
diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationControl.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationControl.java
index df7be58c..7d0891e6 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationControl.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationControl.java
@@ -26,11 +26,8 @@ import javafx.scene.control.Tab;
 import javafx.scene.control.TabPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.Priority;
-//TASK: context menu udělat pro TaskComputation (buňku) nikoliv řádek - dodělat
-//TASK: vyřešit problém při konkurentním scp
 public class TaskComputationControl extends TabPane implements CloseableControl, InitiableControl {
-	@SuppressWarnings("unused")
-	private static Logger log = LoggerFactory.getLogger(cz.it4i.fiji.haas_spim_benchmark.ui.TaskComputationControl.class);
+	public final static Logger log = LoggerFactory.getLogger(cz.it4i.fiji.haas_spim_benchmark.ui.TaskComputationControl.class);
 
 	private TaskComputationAdapter adapter;
 	
@@ -42,9 +39,12 @@ public class TaskComputationControl extends TabPane implements CloseableControl,
 
 	private TaskComputation computation;
 	
-	public TaskComputationControl(TaskComputation computation) {
+	private ExecutorService scpExecutor;
+	
+	public TaskComputationControl(TaskComputation computation, ExecutorService scpExecutor) {
 		JavaFXRoutines.initRootAndController("TaskComputationView.fxml", this);
 		this.computation = computation;
+		this.scpExecutor = scpExecutor;
 	}
 	
 	@Override
@@ -53,7 +53,8 @@ public class TaskComputationControl extends TabPane implements CloseableControl,
 			ProgressDialog dialog = ModalDialogs.doModal(new ProgressDialog(parameter, "Updating infos..."),
 					WindowConstants.DO_NOTHING_ON_CLOSE);
 			try {
-				adapter = new TaskComputationAdapter(computation);
+				adapter = new TaskComputationAdapter(computation, scpExecutor);
+				adapter.init();
 			} finally {
 				dialog.done();
 			}
@@ -80,9 +81,7 @@ public class TaskComputationControl extends TabPane implements CloseableControl,
 	}
 	@Override
 	public void close() {
-		if(adapter != null) {
-			adapter.close();
-		}
+		adapter.close();
 		wsExecutorService.shutdown();
 	}
 
diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationWindow.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationWindow.java
index 98f8f2ae..e5226cfa 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationWindow.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/TaskComputationWindow.java
@@ -1,6 +1,7 @@
 package cz.it4i.fiji.haas_spim_benchmark.ui;
 
 import java.awt.Window;
+import java.util.concurrent.ExecutorService;
 
 import cz.it4i.fiji.haas.ui.FXFrame;
 import cz.it4i.fiji.haas_spim_benchmark.core.TaskComputation;
@@ -9,8 +10,8 @@ public class TaskComputationWindow extends FXFrame<TaskComputationControl> {
 
 	private static final long serialVersionUID = 1L;
 
-	public TaskComputationWindow(Window applicationFrame,TaskComputation computation) {
-		super(applicationFrame,()->new TaskComputationControl(computation));
+	public TaskComputationWindow(Window applicationFrame,TaskComputation computation, ExecutorService scpExecutor) {
+		super(applicationFrame,()->new TaskComputationControl(computation, scpExecutor));
 		
 	}
 
-- 
GitLab