X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnettyutil%2Fhandler%2Fssh%2Fclient%2FSshClientAdapter.java;h=1a2eb3f1ab43d188179351341264c0e46bc52350;hp=ad8b25ff2156d8e937d65d054b41b1e3f34c159e;hb=c0664e68c1408f269a5782f2dba4b1e9044164f6;hpb=ace21ede4027c481d820b40da25b74c73642e5da diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/SshClientAdapter.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/SshClientAdapter.java index ad8b25ff21..1a2eb3f1ab 100644 --- a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/SshClientAdapter.java +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/client/SshClientAdapter.java @@ -8,8 +8,13 @@ package org.opendaylight.controller.netconf.nettyutil.handler.ssh.client; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; + import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import java.io.IOException; @@ -18,7 +23,6 @@ import java.io.OutputStream; import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.atomic.AtomicBoolean; -import org.opendaylight.controller.netconf.nettyutil.handler.ssh.virtualsocket.VirtualSocketException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,7 +31,7 @@ import org.slf4j.LoggerFactory; * Worker thread class. Handles all downstream and upstream events in SSH Netty * pipeline. */ -public class SshClientAdapter implements Runnable { +class SshClientAdapter implements Runnable { private static final Logger logger = LoggerFactory.getLogger(SshClientAdapter.class); private static final int BUFFER_SIZE = 1024; @@ -51,6 +55,7 @@ public class SshClientAdapter implements Runnable { this.invoker = invoker; } + // TODO: refactor public void run() { try { SshSession session = sshClient.openSession(); @@ -80,12 +85,6 @@ public class SshClientAdapter implements Runnable { byteBuf.writeBytes(tranBuff); ctx.fireChannelRead(byteBuf); } - - } catch (VirtualSocketException e) { - // Netty closed connection prematurely. - // Or maybe tried to open ganymed connection without having initialized session - // (ctx.channel().remoteAddress() is null) - // Just pass and move on. } catch (Exception e) { logger.error("Unexpected exception", e); } finally { @@ -123,12 +122,23 @@ public class SshClientAdapter implements Runnable { } } - public void start(ChannelHandlerContext ctx) { - if (this.ctx != null) { - // context is already associated. - return; + public Thread start(ChannelHandlerContext ctx, ChannelFuture channelFuture) { + checkArgument(channelFuture.isSuccess()); + checkNotNull(ctx.channel().remoteAddress()); + synchronized (this) { + checkState(this.ctx == null); + this.ctx = ctx; } - this.ctx = ctx; - new Thread(this).start(); + String threadName = toString(); + Thread thread = new Thread(this, threadName); + thread.start(); + return thread; + } + + @Override + public String toString() { + return "SshClientAdapter{" + + "sshClient=" + sshClient + + '}'; } }