Fix proxy command execution.

Change-Id: I757bba86589f5d17889fdec0e9a643549960e258
Signed-off-by: Greg Watson <g.watson@computer.org>
diff --git a/bundles/org.eclipse.remote.proxy.core/src/org/eclipse/remote/internal/proxy/core/ProxyConnection.java b/bundles/org.eclipse.remote.proxy.core/src/org/eclipse/remote/internal/proxy/core/ProxyConnection.java
index 358ab98..3e28abd 100644
--- a/bundles/org.eclipse.remote.proxy.core/src/org/eclipse/remote/internal/proxy/core/ProxyConnection.java
+++ b/bundles/org.eclipse.remote.proxy.core/src/org/eclipse/remote/internal/proxy/core/ProxyConnection.java
@@ -42,10 +42,6 @@
 public class ProxyConnection implements IRemoteConnectionControlService,
 		IRemoteConnectionChangeListener, IRemoteProxyService, IRemoteProcessService {
 
-	private final boolean logging = false;
-
-	public static final String EMPTY_STRING = ""; //$NON-NLS-1$
-
 	private String fWorkingDir;
 	private ChannelMultiplexer channelMux;
 	private MultiplexedChannel commandChannel;
@@ -121,7 +117,7 @@
 
 	@Override
 	public void setStreams(InputStream in, OutputStream out) {
-		channelMux = new ChannelMultiplexer("", this, in, out);
+		channelMux = new ChannelMultiplexer("C", this, in, out);
 	}
 
 	/*
@@ -228,6 +224,7 @@
 	private StringBuffer stdout = new StringBuffer();
 	private StringBuffer stderr = new StringBuffer();
 	
+	@SuppressWarnings("unused")
 	private String executeCommand(List<String> command, IProgressMonitor monitor) throws ProxyException {
 		try {
 			final MultiplexedChannel chanA = channelMux.openChannel();
diff --git a/bundles/org.eclipse.remote.proxy.core/src/org/eclipse/remote/internal/proxy/core/ProxyProcess.java b/bundles/org.eclipse.remote.proxy.core/src/org/eclipse/remote/internal/proxy/core/ProxyProcess.java
index fe0b338..45e4dd6 100644
--- a/bundles/org.eclipse.remote.proxy.core/src/org/eclipse/remote/internal/proxy/core/ProxyProcess.java
+++ b/bundles/org.eclipse.remote.proxy.core/src/org/eclipse/remote/internal/proxy/core/ProxyProcess.java
@@ -18,11 +18,10 @@
 import org.eclipse.remote.core.IRemoteProcessBuilder;
 import org.eclipse.remote.core.IRemoteProcessControlService;
 import org.eclipse.remote.core.IRemoteProcessTerminalService;
-import org.eclipse.remote.internal.core.RemoteProcess;
+import org.eclipse.remote.core.RemoteProcess;
 
 public class ProxyProcess extends RemoteProcess implements IRemoteProcessControlService, IRemoteProcessTerminalService {
 	private IRemoteProcess remoteProcess;
-	private int width, height;
 	private final InputStream procStdout;
 	private final InputStream procStderr;
 	private final OutputStream procStdin;
@@ -94,7 +93,7 @@
 	@Override
 	public int exitValue() {
 		if (!isCompleted) {
-			throw new IllegalStateException();
+			throw new IllegalThreadStateException();
 		}
 		return exitValue;
 	}
diff --git a/bundles/org.eclipse.remote.proxy.server.core/src/org/eclipse/remote/internal/proxy/server/commands/ServerExecCommand.java b/bundles/org.eclipse.remote.proxy.server.core/src/org/eclipse/remote/internal/proxy/server/commands/ServerExecCommand.java
index 5ccd52e..b52aa0a 100644
--- a/bundles/org.eclipse.remote.proxy.server.core/src/org/eclipse/remote/internal/proxy/server/commands/ServerExecCommand.java
+++ b/bundles/org.eclipse.remote.proxy.server.core/src/org/eclipse/remote/internal/proxy/server/commands/ServerExecCommand.java
@@ -36,6 +36,8 @@
 	private final DataOutputStream result;
 	private final DataInputStream cmd;
 	
+	private Process proc;
+	
 	private class CommandRunner implements Runnable {
 		@Override
 		public void run() {
@@ -61,28 +63,36 @@
 			}
 			builder.redirectErrorStream(redirect);
 			try {
-				Process proc = builder.start();
+				proc = builder.start();
 				startForwarder("stdout", proc.getInputStream(), stdout);
 				startForwarder("stderr", proc.getErrorStream(), stderr);
 				startForwarder("stdin", stdin, proc.getOutputStream());
 				System.err.println("waiting for proc");
-				while (cmd.available() == 0 && proc.isAlive()) {
-					Thread.sleep(500);
-				}
-				System.err.println("done waiting for proc");
-				if (cmd.available() > 0) {
-					cmd.readByte();
-					proc.destroyForcibly();
-				}
+				new Thread(new ProcMonitor(), "process monitor").start();
 				int exit = proc.waitFor();
 				System.err.println("exit status="+exit);
 				result.writeInt(exit);
+				result.flush();
 			} catch (IOException | InterruptedException e) {
 				// Ignore?
 			}
 		}
 	}
 	
+	private class ProcMonitor implements Runnable {
+		@Override
+		public void run() {
+			try {
+				cmd.readByte();
+				if (proc.isAlive()) {
+					proc.destroyForcibly();
+				}
+			} catch (IOException e) {
+				// Finish
+			}
+		}
+	}
+	
 	private class Forwarder implements Runnable {
 		private final InputStream in;
 		private final OutputStream out;
diff --git a/tests/org.eclipse.remote.proxy.tests/src/org/eclipse/remote/proxy/tests/ProcessTests.java b/tests/org.eclipse.remote.proxy.tests/src/org/eclipse/remote/proxy/tests/ProcessTests.java
index 6452df4..b149746 100644
--- a/tests/org.eclipse.remote.proxy.tests/src/org/eclipse/remote/proxy/tests/ProcessTests.java
+++ b/tests/org.eclipse.remote.proxy.tests/src/org/eclipse/remote/proxy/tests/ProcessTests.java
@@ -114,9 +114,15 @@
 				public void run() {
 					try {
 						BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
-						String line;
-						while ((line = stdout.readLine()) != null) {
+						String line = stdout.readLine();
+						int count = Integer.parseInt(line);
+						for (int i = 0 ; i < count; i++) {
+							line = stdout.readLine();
+							if (line == null) {
+								break;
+							}
 							result.append(line);
+							System.out.println(line);
 						}
 						try {
 							proc.destroy();
@@ -136,12 +142,15 @@
 				public void run() {
 					try {
 						BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
+						int count = 10;
+						String line = count + "\n";
+						stdin.write(line);
+						stdin.flush();
 						for (int i = 0; i < 10; i++) {
-							String line = i + "\n";
-							stdin.append(line);
+							line = i + "\n";
+							stdin.write(line);
 							stdin.flush();
 						}
-						proc.getOutputStream().close();
 						try {
 							proc.waitFor();
 						} catch (InterruptedException e) {
@@ -170,7 +179,7 @@
 	}
 
 	public void testExitValue() {
-		IRemoteProcessBuilder builder = processService.getProcessBuilder(new String[]{"sleep","60"}); //$NON-NLS-1$
+		IRemoteProcessBuilder builder = processService.getProcessBuilder(new String[]{"sleep","50"}); //$NON-NLS-1$
 		assertNotNull(builder);
 		IRemoteProcess rp = null;
 		try {
@@ -187,6 +196,13 @@
 		} catch(IllegalThreadStateException e) {
 			e.printStackTrace();
 		}
+		try {
+			p.destroyForcibly();
+			p.waitFor();
+		} catch (InterruptedException e) {
+			fail(e.getMessage());
+		}
+		assertFalse(p.isAlive());
 	}
 	
 	/*