X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fhandler%2Fssh%2Fclient%2FSshClientAdapter.java;h=244bcc0041c963988aa8fc78c712767fc7c643c2;hb=027bc8f87341f432654c3aaa7771658c25d2ca7d;hp=4213fe3e0642db6257b3e655c407a4394785fca3;hpb=110f0a37cb14b8fbf05b87dcb851bd3f026c6d8d;p=controller.git diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/client/SshClientAdapter.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/client/SshClientAdapter.java index 4213fe3e06..244bcc0041 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/client/SshClientAdapter.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/client/SshClientAdapter.java @@ -27,16 +27,14 @@ import org.opendaylight.controller.netconf.util.handler.ssh.virtualsocket.Virtua * pipeline. */ public class SshClientAdapter implements Runnable { + private static final int BUFFER_SIZE = 1024; + private final SshClient sshClient; private final Invoker invoker; - private SshSession session; - private InputStream stdOut; - private InputStream stdErr; private OutputStream stdIn; - private Queue postponned = new LinkedList<>(); - + private Queue postponed = new LinkedList<>(); private ChannelHandlerContext ctx; private ChannelPromise disconnectPromise; @@ -52,22 +50,22 @@ public class SshClientAdapter implements Runnable { public void run() { try { - session = sshClient.openSession(); + SshSession session = sshClient.openSession(); invoker.invoke(session); - stdOut = session.getStdout(); - stdErr = session.getStderr(); + InputStream stdOut = session.getStdout(); + session.getStderr(); synchronized (lock) { stdIn = session.getStdin(); - ByteBuf message = null; - while ((message = postponned.poll()) != null) { + ByteBuf message; + while ((message = postponed.poll()) != null) { writeImpl(message); } } - while (stopRequested.get() == false) { - byte[] readBuff = new byte[1024]; + while (!stopRequested.get()) { + byte[] readBuff = new byte[BUFFER_SIZE]; int c = stdOut.read(readBuff); if (c == -1) { continue; @@ -84,13 +82,14 @@ public class SshClientAdapter implements Runnable { // Netty closed connection prematurely. // Just pass and move on. } catch (Exception e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } finally { sshClient.close(); synchronized (lock) { - if (disconnectPromise != null) + if (disconnectPromise != null) { ctx.disconnect(disconnectPromise); + } } } } @@ -99,7 +98,7 @@ public class SshClientAdapter implements Runnable { public void write(ByteBuf message) throws IOException { synchronized (lock) { if (stdIn == null) { - postponned.add(message); + postponed.add(message); return; } writeImpl(message); @@ -119,8 +118,10 @@ public class SshClientAdapter implements Runnable { } public void start(ChannelHandlerContext ctx) { - if (this.ctx != null) - return; // context is already associated. + if (this.ctx != null) { + // context is already associated. + return; + } this.ctx = ctx; new Thread(this).start(); }