2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.nettyutil.handler.ssh.client;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.apache.sshd.common.future.SshFutureListener;
14 import org.apache.sshd.common.io.IoInputStream;
15 import org.apache.sshd.common.io.IoReadFuture;
16 import org.apache.sshd.common.util.Buffer;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * Listener on async input stream from SSH session.
22 * This listeners schedules reads in a loop until the session is closed or read fails.
24 public final class AsyncSshHandlerReader implements SshFutureListener<IoReadFuture>, AutoCloseable {
26 private static final Logger LOG = LoggerFactory.getLogger(AsyncSshHandlerReader.class);
28 private static final int BUFFER_SIZE = 2048;
30 private final AutoCloseable connectionClosedCallback;
31 private final ReadMsgHandler readHandler;
33 private final String channelId;
34 private IoInputStream asyncOut;
36 private IoReadFuture currentReadFuture;
38 public AsyncSshHandlerReader(final AutoCloseable connectionClosedCallback, final ReadMsgHandler readHandler,
39 final String channelId, final IoInputStream asyncOut) {
40 this.connectionClosedCallback = connectionClosedCallback;
41 this.readHandler = readHandler;
42 this.channelId = channelId;
43 this.asyncOut = asyncOut;
44 buf = new Buffer(BUFFER_SIZE);
45 asyncOut.read(buf).addListener(this);
49 public synchronized void operationComplete(final IoReadFuture future) {
50 if (future.getException() != null) {
52 //if asyncout is already set to null by close method, do nothing
53 if (asyncOut == null) {
57 if (asyncOut.isClosed() || asyncOut.isClosing()) {
59 LOG.debug("Ssh session dropped on channel: {}", channelId, future.getException());
61 LOG.warn("Exception while reading from SSH remote on channel {}", channelId, future.getException());
67 if (future.getRead() > 0) {
68 final ByteBuf msg = Unpooled.wrappedBuffer(buf.array(), 0, future.getRead());
69 if (LOG.isTraceEnabled()) {
70 LOG.trace("Reading message on channel: {}, message: {}",
71 channelId, AsyncSshHandlerWriter.byteBufToString(msg));
73 readHandler.onMessageRead(msg);
76 buf = new Buffer(BUFFER_SIZE);
77 currentReadFuture = asyncOut.read(buf);
78 currentReadFuture.addListener(this);
82 @SuppressWarnings("checkstyle:IllegalCatch")
83 private void invokeDisconnect() {
85 connectionClosedCallback.close();
86 } catch (final Exception e) {
87 // This should not happen
88 throw new IllegalStateException(e);
93 public synchronized void close() {
94 // Remove self as listener on close to prevent reading from closed input
95 if (currentReadFuture != null) {
96 currentReadFuture.removeListener(this);
97 currentReadFuture = null;
103 public interface ReadMsgHandler {
105 void onMessageRead(ByteBuf msg);