b123e0b631cd248f3cbaecbd85fe084aab755219
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / ssh / client / AsyncSshHandlerReader.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.nettyutil.handler.ssh.client;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.netconf.shaded.sshd.common.future.SshFutureListener;
14 import org.opendaylight.netconf.shaded.sshd.common.io.IoInputStream;
15 import org.opendaylight.netconf.shaded.sshd.common.io.IoReadFuture;
16 import org.opendaylight.netconf.shaded.sshd.common.util.buffer.Buffer;
17 import org.opendaylight.netconf.shaded.sshd.common.util.buffer.ByteArrayBuffer;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Listener on async input stream from SSH session.
23  * This listeners schedules reads in a loop until the session is closed or read fails.
24  */
25 @Deprecated(since = "7.0.0", forRemoval = true)
26 public final class AsyncSshHandlerReader implements SshFutureListener<IoReadFuture>, AutoCloseable {
27     private static final Logger LOG = LoggerFactory.getLogger(AsyncSshHandlerReader.class);
28     private static final int BUFFER_SIZE = 2048;
29
30     private final AutoCloseable connectionClosedCallback;
31     private final ReadMsgHandler readHandler;
32
33     private final String channelId;
34     private IoInputStream asyncOut;
35     private Buffer buf;
36     private IoReadFuture currentReadFuture;
37
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 ByteArrayBuffer(BUFFER_SIZE);
45         asyncOut.read(buf).addListener(this);
46     }
47
48     @Override
49     public void operationComplete(final IoReadFuture future) {
50         if (checkDisconnect(future)) {
51             invokeDisconnect();
52         }
53     }
54
55     private synchronized boolean checkDisconnect(final IoReadFuture future) {
56         if (future.getException() != null) {
57             //if asyncout is already set to null by close method, do nothing
58             if (asyncOut == null) {
59                 return false;
60             }
61
62             if (asyncOut.isClosed() || asyncOut.isClosing()) {
63                 // Ssh dropped
64                 LOG.debug("Ssh session dropped on channel: {}", channelId, future.getException());
65             } else {
66                 LOG.warn("Exception while reading from SSH remote on channel {}", channelId, future.getException());
67             }
68             return true;
69         } else if (future.getRead() > 0) {
70             final ByteBuf msg = Unpooled.wrappedBuffer(buf.array(), 0, future.getRead());
71             if (LOG.isTraceEnabled()) {
72                 LOG.trace("Reading message on channel: {}, message: {}",
73                         channelId, AsyncSshHandlerWriter.byteBufToString(msg));
74             }
75             readHandler.onMessageRead(msg);
76
77             // Schedule next read
78             buf = new ByteArrayBuffer(BUFFER_SIZE);
79             currentReadFuture = asyncOut.read(buf);
80             currentReadFuture.addListener(this);
81         }
82         return false;
83     }
84
85     /**
86      * Closing of the {@link AsyncSshHandlerReader}. This method should never be called with any locks held since
87      * call to {@link AutoCloseable#close()} can be a source of ABBA deadlock.
88      */
89     @SuppressWarnings("checkstyle:IllegalCatch")
90     private void invokeDisconnect() {
91         try {
92             connectionClosedCallback.close();
93         } catch (final Exception e) {
94             // This should not happen
95             throw new IllegalStateException(e);
96         }
97     }
98
99     @Override
100     public synchronized void close() {
101         // Remove self as listener on close to prevent reading from closed input
102         if (currentReadFuture != null) {
103             currentReadFuture.removeListener(this);
104             currentReadFuture = null;
105         }
106
107         asyncOut = null;
108     }
109
110     public interface ReadMsgHandler {
111
112         void onMessageRead(ByteBuf msg);
113     }
114 }