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;
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;
* 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;
this.invoker = invoker;
}
+ // TODO: refactor
public void run() {
- SshSession session;
- try {
- session = sshClient.openSession();
- } catch (IOException e) {
- logger.error("Cannot establish session", e);
- sshClient.close();
- return;
- }
try {
+ SshSession session = sshClient.openSession();
invoker.invoke(session);
InputStream stdOut = session.getStdout();
session.getStderr();
byteBuf.writeBytes(tranBuff);
ctx.fireChannelRead(byteBuf);
}
-
- } catch (VirtualSocketException e) {
- // Netty closed connection prematurely.
- // Just pass and move on.
} catch (Exception e) {
logger.error("Unexpected exception", e);
} finally {
private void writeImpl(ByteBuf message) throws IOException {
message.getBytes(0, stdIn, message.readableBytes());
+ message.release();
stdIn.flush();
}
}
}
- 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 +
+ '}';
}
}