Repository: mina-sshd
Updated Branches:
  refs/heads/master c64e73acb -> 78d4d797c


[SSHD-692] Fixed ClientSession#executeRemoteCommand repeated call support


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/78d4d797
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/78d4d797
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/78d4d797

Branch: refs/heads/master
Commit: 78d4d797ca59c09bb7dd06b86655b16e94d2e408
Parents: c64e73a
Author: Lyor Goldstein <[email protected]>
Authored: Mon Aug 22 21:19:12 2016 +0300
Committer: Lyor Goldstein <[email protected]>
Committed: Mon Aug 22 21:19:12 2016 +0300

----------------------------------------------------------------------
 .../sshd/client/session/ClientSession.java      | 15 ---------
 .../sshd/client/channel/ChannelExecTest.java    |  2 +-
 .../sshd/client/session/ClientSessionTest.java  |  6 ++--
 .../sshd/util/test/CommandExecutionHelper.java  | 33 +++++++++++++-------
 4 files changed, 26 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/78d4d797/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSession.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSession.java 
b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSession.java
index dbd2b14..ab895e0 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSession.java
@@ -49,7 +49,6 @@ import 
org.apache.sshd.client.subsystem.sftp.SftpClientCreator;
 import org.apache.sshd.common.forward.PortForwardingManager;
 import org.apache.sshd.common.future.KeyExchangeFuture;
 import org.apache.sshd.common.session.Session;
-import org.apache.sshd.common.util.closeable.CloseableUtils;
 import org.apache.sshd.common.util.io.NoCloseOutputStream;
 import org.apache.sshd.common.util.io.NullOutputStream;
 import org.apache.sshd.common.util.net.SshdSocketAddress;
@@ -94,8 +93,6 @@ public interface ClientSession
     }
 
     Set<ClientChannelEvent> REMOTE_COMMAND_WAIT_EVENTS =
-            Collections.unmodifiableSet(EnumSet.of(ClientChannelEvent.CLOSED, 
ClientChannelEvent.EXIT_STATUS));
-    Set<ClientChannelEvent> REMOTE_COMMAND_END_EVENTS =
             Collections.unmodifiableSet(EnumSet.of(ClientChannelEvent.CLOSED));
 
     /**
@@ -213,10 +210,6 @@ public interface ClientSession
             channel.setErr(channelErr);
             channel.open().await(); // TODO use verify and a configurable 
timeout
 
-            OutputStream invertedStream = channel.getInvertedIn();
-            invertedStream.write(command.getBytes(charset));
-            invertedStream.flush();
-
             // TODO use a configurable timeout
             Collection<ClientChannelEvent> waitMask = 
channel.waitFor(REMOTE_COMMAND_WAIT_EVENTS, 0L);
             if (waitMask.contains(ClientChannelEvent.TIMEOUT)) {
@@ -228,14 +221,6 @@ public interface ClientSession
                 throw new RemoteException("Remote command failed (" + 
exitStatus + "): " + command, new ServerException(exitStatus.toString()));
             }
 
-            if (!waitMask.contains(ClientChannelEvent.CLOSED)) {
-                long maxWait = CloseableUtils.getMaxCloseWaitTime(this);
-                waitMask = channel.waitFor(REMOTE_COMMAND_END_EVENTS, maxWait);
-                if (waitMask.contains(ClientChannelEvent.TIMEOUT)) {
-                    throw new SocketTimeoutException("Failed to receive 
command channel close in time: " + command);
-                }
-            }
-
             byte[] response = channelOut.toByteArray();
             return new String(response, charset);
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/78d4d797/sshd-core/src/test/java/org/apache/sshd/client/channel/ChannelExecTest.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/client/channel/ChannelExecTest.java 
b/sshd-core/src/test/java/org/apache/sshd/client/channel/ChannelExecTest.java
index 5b227fc..2f4c10c 100644
--- 
a/sshd-core/src/test/java/org/apache/sshd/client/channel/ChannelExecTest.java
+++ 
b/sshd-core/src/test/java/org/apache/sshd/client/channel/ChannelExecTest.java
@@ -56,7 +56,7 @@ public class ChannelExecTest extends BaseTestSupport {
         sshd.setCommandFactory(new CommandFactory() {
             @Override
             public Command createCommand(String command) {
-                return new CommandExecutionHelper() {
+                return new CommandExecutionHelper(command) {
                     @Override
                     protected boolean handleCommandLine(String command) throws 
Exception {
                         OutputStream stdout = getOut();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/78d4d797/sshd-core/src/test/java/org/apache/sshd/client/session/ClientSessionTest.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/client/session/ClientSessionTest.java 
b/sshd-core/src/test/java/org/apache/sshd/client/session/ClientSessionTest.java
index 103a42b..9ab4ab3 100644
--- 
a/sshd-core/src/test/java/org/apache/sshd/client/session/ClientSessionTest.java
+++ 
b/sshd-core/src/test/java/org/apache/sshd/client/session/ClientSessionTest.java
@@ -87,7 +87,7 @@ public class ClientSessionTest extends BaseTestSupport {
         sshd.setCommandFactory(new CommandFactory() {
             @Override
             public Command createCommand(String command) {
-                return new CommandExecutionHelper() {
+                return new CommandExecutionHelper(command) {
                     private boolean cmdProcessed;
 
                     @Override
@@ -121,7 +121,7 @@ public class ClientSessionTest extends BaseTestSupport {
         sshd.setCommandFactory(new CommandFactory() {
             @Override
             public Command createCommand(String command) {
-                return new CommandExecutionHelper() {
+                return new CommandExecutionHelper(command) {
                     private boolean cmdProcessed;
 
                     @Override
@@ -169,7 +169,7 @@ public class ClientSessionTest extends BaseTestSupport {
         sshd.setCommandFactory(new CommandFactory() {
             @Override
             public Command createCommand(String command) {
-                return new CommandExecutionHelper() {
+                return new CommandExecutionHelper(command) {
                     private boolean cmdProcessed;
 
                     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/78d4d797/sshd-core/src/test/java/org/apache/sshd/util/test/CommandExecutionHelper.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/util/test/CommandExecutionHelper.java 
b/sshd-core/src/test/java/org/apache/sshd/util/test/CommandExecutionHelper.java
index a2006d5..f036288 100644
--- 
a/sshd-core/src/test/java/org/apache/sshd/util/test/CommandExecutionHelper.java
+++ 
b/sshd-core/src/test/java/org/apache/sshd/util/test/CommandExecutionHelper.java
@@ -42,9 +42,15 @@ public abstract class CommandExecutionHelper implements 
Command, Runnable, ExitC
     private Environment environment;
     private Thread thread;
     private boolean cbCalled;
+    // null/empty if shell
+    private String command;
 
     protected CommandExecutionHelper() {
-        super();
+        this(null);
+    }
+
+    protected CommandExecutionHelper(String command) {
+        this.command = command;
     }
 
     public InputStream getIn() {
@@ -102,17 +108,22 @@ public abstract class CommandExecutionHelper implements 
Command, Runnable, ExitC
 
     @Override
     public void run() {
-        String command = null;
-        try (BufferedReader r = new BufferedReader(new 
InputStreamReader(getIn(), StandardCharsets.UTF_8))) {
-            for (;;) {
-                command = r.readLine();
-                if (command == null) {
-                    return;
-                }
-
-                if (!handleCommandLine(command)) {
-                    return;
+        try {
+            if (command == null) {
+                try (BufferedReader r = new BufferedReader(new 
InputStreamReader(getIn(), StandardCharsets.UTF_8))) {
+                    for (;;) {
+                        command = r.readLine();
+                        if (command == null) {
+                            return;
+                        }
+
+                        if (!handleCommandLine(command)) {
+                            return;
+                        }
+                    }
                 }
+            } else {
+                handleCommandLine(command);
             }
         } catch (InterruptedIOException e) {
             // Ignore - signaled end

Reply via email to