Class AbstractClientChannel
- java.lang.Object
-
- org.apache.sshd.common.util.logging.AbstractLoggingBean
-
- org.apache.sshd.common.util.closeable.IoBaseCloseable
-
- org.apache.sshd.common.util.closeable.AbstractCloseable
-
- org.apache.sshd.common.util.closeable.AbstractInnerCloseable
-
- org.apache.sshd.common.channel.AbstractChannel
-
- org.apache.sshd.client.channel.AbstractClientChannel
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.nio.channels.Channel
,ClientChannel
,ClientSessionHolder
,AttributeRepository
,AttributeStore
,Channel
,ChannelIdentifier
,ChannelListenerManager
,StreamingChannel
,ChannelStreamWriterResolver
,ChannelStreamWriterResolverManager
,Closeable
,PropertyResolver
,SessionContextHolder
,SessionHolder<Session>
,ExecutorServiceCarrier
- Direct Known Subclasses:
AgentForwardedChannel
,ChannelDirectTcpip
,ChannelForwardedX11
,ChannelSession
,TcpipClientChannel
public abstract class AbstractClientChannel extends AbstractChannel implements ClientChannel
TODO Add javadoc
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
AbstractClientChannel.NullIoInputStream
-
Nested classes/interfaces inherited from class org.apache.sshd.common.channel.AbstractChannel
AbstractChannel.GracefulChannelCloseable, AbstractChannel.GracefulState
-
Nested classes/interfaces inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
AbstractCloseable.State
-
Nested classes/interfaces inherited from interface org.apache.sshd.common.AttributeRepository
AttributeRepository.AttributeKey<T>
-
Nested classes/interfaces inherited from interface org.apache.sshd.common.channel.StreamingChannel
StreamingChannel.Streaming
-
-
Field Summary
Fields Modifier and Type Field Description protected ChannelAsyncInputStream
asyncErr
protected ChannelAsyncOutputStream
asyncIn
protected ChannelAsyncInputStream
asyncOut
private java.lang.String
channelType
protected java.io.OutputStream
err
protected java.util.concurrent.atomic.AtomicReference<java.lang.String>
exitSignalHolder
protected java.util.concurrent.atomic.AtomicReference<java.lang.Integer>
exitStatusHolder
protected java.io.InputStream
in
protected java.io.InputStream
invertedErr
protected java.io.OutputStream
invertedIn
protected java.io.InputStream
invertedOut
private static java.io.InputStream
NULL_INPUT_STREAM
protected java.util.concurrent.atomic.AtomicBoolean
opened
protected java.lang.String
openFailureLang
protected java.lang.String
openFailureMsg
protected int
openFailureReason
protected OpenFuture
openFuture
protected java.io.OutputStream
out
protected boolean
redirectErrorStream
protected StreamingChannel.Streaming
streaming
-
Fields inherited from class org.apache.sshd.common.channel.AbstractChannel
channelListenerProxy, channelListeners, closeSignaled, eofReceived, eofSent, gracefulFuture, gracefulState, initialized, RESPONSE_BUFFER_GROWTH_FACTOR, service, unregisterSignaled
-
Fields inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
closeFuture, futureLock, state
-
Fields inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
log
-
Fields inherited from interface org.apache.sshd.common.channel.Channel
CHANNEL_EXEC, CHANNEL_SHELL, CHANNEL_SUBSYSTEM
-
Fields inherited from interface org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolver
NONE
-
Fields inherited from interface org.apache.sshd.common.PropertyResolver
EMPTY
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractClientChannel(java.lang.String type)
protected
AbstractClientChannel(java.lang.String type, java.util.Collection<? extends RequestHandler<Channel>> handlers)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
addChannelSignalRequestHandlers(EventNotifier<java.lang.String> notifier)
protected abstract void
doOpen()
protected void
doWriteData(byte[] data, int off, long len)
protected void
doWriteExtendedData(byte[] data, int off, long len)
IoInputStream
getAsyncErr()
IoOutputStream
getAsyncIn()
IoInputStream
getAsyncOut()
java.util.Set<ClientChannelEvent>
getChannelState()
java.lang.String
getChannelType()
java.io.OutputStream
getErr()
java.lang.String
getExitSignal()
java.lang.Integer
getExitStatus()
java.io.InputStream
getIn()
protected Closeable
getInnerCloseable()
java.io.InputStream
getInvertedErr()
Obtains anInputStream
to read receivedSshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA
data directly from the channel.java.io.OutputStream
getInvertedIn()
Obtains anOutputStream
to send data directly to the remote end of the channel.java.io.InputStream
getInvertedOut()
Obtains anInputStream
to read receivedSshConstants.SSH_MSG_CHANNEL_DATA
data directly from the channel.java.io.OutputStream
getOut()
StreamingChannel.Streaming
getStreaming()
void
handleOpenFailure(Buffer buffer)
For a client channel, this method will be called internally by the session when the server has rejected this channel opening.void
handleOpenSuccess(long recipient, long rwSize, long packetSize, Buffer buffer)
For a client channel, this method will be called internally by the session when the confirmation has been received.void
handleWindowAdjust(Buffer buffer)
Invoked whenSSH_MSG_CHANNEL_WINDOW_ADJUST
receivedboolean
isRedirectErrorStream()
protected boolean
mayWrite()
OpenFuture
open()
OpenFuture
open(long recipient, long rwSize, long packetSize, Buffer buffer)
For a server channel, this method will actually open the channelvoid
setErr(java.io.OutputStream err)
Sets an output stream for the channel to write receivedSshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA
data to.void
setIn(java.io.InputStream in)
Sets anInputStream
that will be read by this channel and forwarded to the remote channel.void
setOut(java.io.OutputStream out)
Sets an output stream for the channel to write receivedSshConstants.SSH_MSG_CHANNEL_DATA
data to.void
setRedirectErrorStream(boolean redirectErrorStream)
Defines whether to redirect the error stream into the output stream; has no effect ifClientChannel.setErr(OutputStream)
has also been called by the time the channel is opened.void
setStreaming(StreamingChannel.Streaming streaming)
protected <C extends java.util.Collection<ClientChannelEvent>>
CupdateCurrentChannelState(C state)
java.util.Set<ClientChannelEvent>
waitFor(java.util.Collection<ClientChannelEvent> mask, long timeout)
Waits until any of the specified events in the mask is signaled-
Methods inherited from class org.apache.sshd.common.channel.AbstractChannel
addChannelListener, addPendingRequest, addRequestHandler, attributeKeys, clearAttributes, computeAttributeIfAbsent, configureWindow, getAttribute, getAttributesCount, getChannelId, getChannelListenerProxy, getChannelStreamWriterResolver, getExecutorService, getLocalWindow, getParentPropertyResolver, getProperties, getRecipient, getRemoteWindow, getRequestHandlers, getSession, handleChannelRegistrationResult, handleChannelRequest, handleChannelUnregistration, handleClose, handleData, handleEof, handleExtendedData, handleFailure, handleInternalRequest, handleRequest, handleSuccess, handleUnknownChannelRequest, init, invokeChannelSignaller, isEofSent, isEofSignalled, isInitialized, notifyStateChanged, notifyStateChanged, preClose, removeAttribute, removeChannelListener, removePendingRequest, removeRequestHandler, resolveChannelStreamWriterResolver, sendEof, sendResponse, sendWindowAdjust, setAttribute, setChannelStreamWriterResolver, setRecipient, signalChannelClosed, signalChannelClosed, signalChannelInitialized, signalChannelInitialized, signalChannelOpenFailure, signalChannelOpenFailure, signalChannelOpenSuccess, signalChannelOpenSuccess, toString, validateIncomingDataSize, writePacket
-
Methods inherited from class org.apache.sshd.common.util.closeable.AbstractInnerCloseable
doCloseGracefully, doCloseImmediately
-
Methods inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
addCloseFutureListener, builder, close, getFutureLock, isClosed, isClosing, removeCloseFutureListener
-
Methods inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
debug, debug, debug, debug, debug, error, error, error, error, error, getSimplifiedLogger, info, info, warn, warn, warn, warn, warn, warn, warn, warn
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.sshd.common.AttributeRepository
attributeKeys, getAttribute, getAttributesCount
-
Methods inherited from interface org.apache.sshd.common.AttributeStore
clearAttributes, computeAttributeIfAbsent, removeAttribute, setAttribute
-
Methods inherited from interface org.apache.sshd.common.channel.Channel
addRequestHandler, addRequestHandlers, getLocalWindow, getRecipient, getRemoteWindow, getRequestHandlers, handleChannelRegistrationResult, handleChannelUnregistration, handleClose, handleData, handleEof, handleExtendedData, handleFailure, handleRequest, handleSuccess, init, isEofSignalled, isInitialized, removeRequestHandler, removeRequestHandlers, resolveAttribute, writePacket
-
Methods inherited from interface org.apache.sshd.common.channel.ChannelIdentifier
getChannelId
-
Methods inherited from interface org.apache.sshd.common.channel.ChannelListenerManager
addChannelListener, getChannelListenerProxy, removeChannelListener
-
Methods inherited from interface org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolverManager
getChannelStreamWriterResolver, resolveChannelStreamWriter, resolveChannelStreamWriterResolver, setChannelStreamWriterResolver
-
Methods inherited from interface org.apache.sshd.client.channel.ClientChannel
getClientSession, waitFor
-
Methods inherited from interface org.apache.sshd.common.Closeable
addCloseFutureListener, close, close, isClosed, isClosing, isOpen, removeCloseFutureListener
-
Methods inherited from interface org.apache.sshd.common.PropertyResolver
getBoolean, getBooleanProperty, getCharset, getInteger, getIntProperty, getLong, getLongProperty, getObject, getParentPropertyResolver, getProperties, getString, getStringProperty, isEmpty
-
Methods inherited from interface org.apache.sshd.common.session.SessionHolder
getSession, getSessionContext
-
-
-
-
Field Detail
-
NULL_INPUT_STREAM
private static final java.io.InputStream NULL_INPUT_STREAM
-
opened
protected final java.util.concurrent.atomic.AtomicBoolean opened
-
streaming
protected StreamingChannel.Streaming streaming
-
asyncIn
protected ChannelAsyncOutputStream asyncIn
-
asyncOut
protected ChannelAsyncInputStream asyncOut
-
asyncErr
protected ChannelAsyncInputStream asyncErr
-
in
protected java.io.InputStream in
-
invertedIn
protected java.io.OutputStream invertedIn
-
out
protected java.io.OutputStream out
-
invertedOut
protected java.io.InputStream invertedOut
-
err
protected java.io.OutputStream err
-
invertedErr
protected java.io.InputStream invertedErr
-
redirectErrorStream
protected boolean redirectErrorStream
-
exitStatusHolder
protected final java.util.concurrent.atomic.AtomicReference<java.lang.Integer> exitStatusHolder
-
exitSignalHolder
protected final java.util.concurrent.atomic.AtomicReference<java.lang.String> exitSignalHolder
-
openFailureReason
protected int openFailureReason
-
openFailureMsg
protected java.lang.String openFailureMsg
-
openFailureLang
protected java.lang.String openFailureLang
-
openFuture
protected OpenFuture openFuture
-
channelType
private final java.lang.String channelType
-
-
Constructor Detail
-
AbstractClientChannel
protected AbstractClientChannel(java.lang.String type)
-
AbstractClientChannel
protected AbstractClientChannel(java.lang.String type, java.util.Collection<? extends RequestHandler<Channel>> handlers)
-
-
Method Detail
-
addChannelSignalRequestHandlers
protected void addChannelSignalRequestHandlers(EventNotifier<java.lang.String> notifier)
-
getChannelType
public java.lang.String getChannelType()
- Specified by:
getChannelType
in interfaceClientChannel
- Returns:
- The type of channel reported when it was created
-
getStreaming
public StreamingChannel.Streaming getStreaming()
- Specified by:
getStreaming
in interfaceStreamingChannel
-
setStreaming
public void setStreaming(StreamingChannel.Streaming streaming)
- Specified by:
setStreaming
in interfaceStreamingChannel
-
getAsyncIn
public IoOutputStream getAsyncIn()
- Specified by:
getAsyncIn
in interfaceClientChannel
-
getAsyncOut
public IoInputStream getAsyncOut()
- Specified by:
getAsyncOut
in interfaceClientChannel
-
getAsyncErr
public IoInputStream getAsyncErr()
- Specified by:
getAsyncErr
in interfaceClientChannel
-
getInvertedIn
public java.io.OutputStream getInvertedIn()
Description copied from interface:ClientChannel
Obtains anOutputStream
to send data directly to the remote end of the channel. This can be used instead of usingClientChannel.setIn(InputStream)
method and having the channel polling for data in that stream.When the channel closes, it will
close
the returned stream.This method should be called only after the channel has been opened.
- Specified by:
getInvertedIn
in interfaceClientChannel
- Returns:
- an
OutputStream
for sending data, ornull
if an input stream was set viaClientChannel.setIn(InputStream)
- See Also:
ClientChannel.setIn(InputStream)
-
getIn
public java.io.InputStream getIn()
-
setIn
public void setIn(java.io.InputStream in)
Description copied from interface:ClientChannel
Sets anInputStream
that will be read by this channel and forwarded to the remote channel. Note that using such a stream will create an additional thread for pumping the stream which will only be able to end when that stream is actually closed or EOF on the stream is reached. It is recommended to use theClientChannel.getInvertedIn()
method instead and write data directly.The stream must be set before the channel is opened. When the channel closes, it will
close
the given stream.- Specified by:
setIn
in interfaceClientChannel
- Parameters:
in
- anInputStream
to be polled and forwarded- See Also:
ClientChannel.getInvertedIn()
-
getInvertedOut
public java.io.InputStream getInvertedOut()
Description copied from interface:ClientChannel
Obtains anInputStream
to read receivedSshConstants.SSH_MSG_CHANNEL_DATA
data directly from the channel. This is an alternative toClientChannel.setOut(OutputStream)
. If the error stream is redirected to the output stream viasetRedirectErrorStream(true)
, this stream will also receiveSshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA
data.When the channel closes, it will not close the returned stream. It is the caller's responsibility to close the returned stream if needed. Closing the stream while the channel is open may cause the channel to be closed forcibly if more data arrives. The stream remains open after the channel has closed, so that the caller can read the last arrived data even afterwards.
As with all external processes, the application should read this stream to avoid that the channel blocks when the stream's buffer is full. The buffer size for the returned stream is bounded by the channel's local window size. If the caller does not read this stream, the channel will block once the local window is exhausted.
This method should be called only after the channel has been opened.
- Specified by:
getInvertedOut
in interfaceClientChannel
- Returns:
- an
InputStream
for reading received data, ornull
if an output stream was set viaClientChannel.setOut(OutputStream)
- See Also:
ClientChannel.setOut(OutputStream)
,ClientChannel.setRedirectErrorStream(boolean)
-
getOut
public java.io.OutputStream getOut()
-
setOut
public void setOut(java.io.OutputStream out)
Description copied from interface:ClientChannel
Sets an output stream for the channel to write receivedSshConstants.SSH_MSG_CHANNEL_DATA
data to. For remote command execution, this is typically the remote command'sstdout
. If the error stream is redirected to the output stream viasetRedirectErrorStream(true)
, this stream will also receiveSshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA
data.The stream must be set before the channel is opened. When the channel closes, it will
close
the given stream.If no stream is set by the time the channel is opened, the channel will internally forward data to a stream that can be read via the
InputStream
obtained viaClientChannel.getInvertedOut()
.- Specified by:
setOut
in interfaceClientChannel
- Parameters:
out
- theOutputStream
- See Also:
ClientChannel.getInvertedOut()
,ClientChannel.setRedirectErrorStream(boolean)
-
getInvertedErr
public java.io.InputStream getInvertedErr()
Description copied from interface:ClientChannel
Obtains anInputStream
to read receivedSshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA
data directly from the channel. This is an alternative toClientChannel.setErr(OutputStream)
. If the error stream is redirected to the output stream viasetRedirectErrorStream(true)
, the returned stream will not receive any data and be always at EOF.When the channel closes, it will not close the returned stream. It is the caller's responsibility to close the returned stream if needed. Closing the stream while the channel is open may cause the channel to be closed forcibly if more data arrives. The stream remains open after the channel has closed, so that the caller can read the last arrived data even afterwards.
As with all external processes, the application should read this stream (unless it was redirected) to avoid that the channel blocks when the stream's buffer is full. The buffer size for the returned stream is bounded by the channel's local window size. If the caller does not read this stream, the channel will block once the local window is exhausted.
This method should be called only after the channel has been opened.
- Specified by:
getInvertedErr
in interfaceClientChannel
- Returns:
- an
InputStream
for reading received data, ornull
if an output stream was set viaClientChannel.setErr(OutputStream)
- See Also:
ClientChannel.setErr(OutputStream)
,ClientChannel.setRedirectErrorStream(boolean)
-
getErr
public java.io.OutputStream getErr()
-
setErr
public void setErr(java.io.OutputStream err)
Description copied from interface:ClientChannel
Sets an output stream for the channel to write receivedSshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA
data to. For remote command execution, this is typically the remote command'sstderr
.The stream must be set before the channel is opened. When the channel closes, it will
close
the given stream.If no stream is set by the time the channel is opened, the channel will internally forward data to a stream that can be read via the
InputStream
obtained viaClientChannel.getInvertedErr()
. (Or it might forward the data to the output stream ifsetRedirectErrorStream(true)
is set.)- Specified by:
setErr
in interfaceClientChannel
- Parameters:
err
- theOutputStream
- See Also:
ClientChannel.getInvertedErr()
,ClientChannel.setRedirectErrorStream(boolean)
-
isRedirectErrorStream
public boolean isRedirectErrorStream()
-
setRedirectErrorStream
public void setRedirectErrorStream(boolean redirectErrorStream)
Description copied from interface:ClientChannel
Defines whether to redirect the error stream into the output stream; has no effect ifClientChannel.setErr(OutputStream)
has also been called by the time the channel is opened.- Specified by:
setRedirectErrorStream
in interfaceClientChannel
- Parameters:
redirectErrorStream
- whether to redirect the error stream to the output stream.
-
getInnerCloseable
protected Closeable getInnerCloseable()
- Overrides:
getInnerCloseable
in classAbstractChannel
-
waitFor
public java.util.Set<ClientChannelEvent> waitFor(java.util.Collection<ClientChannelEvent> mask, long timeout)
Description copied from interface:ClientChannel
Waits until any of the specified events in the mask is signaled- Specified by:
waitFor
in interfaceClientChannel
- Parameters:
mask
- TheClientChannelEvent
s masktimeout
- The timeout to wait (msec.) - if non-positive then forever- Returns:
- The actual signaled event - includes
ClientChannelEvent.TIMEOUT
if timeout expired before the expected event was signaled
-
getChannelState
public java.util.Set<ClientChannelEvent> getChannelState()
- Specified by:
getChannelState
in interfaceClientChannel
- Returns:
- A snapshot of the current channel state
- See Also:
ClientChannel.waitFor(Collection, long)
-
updateCurrentChannelState
protected <C extends java.util.Collection<ClientChannelEvent>> C updateCurrentChannelState(C state)
-
open
public OpenFuture open() throws java.io.IOException
- Specified by:
open
in interfaceClientChannel
- Throws:
java.io.IOException
-
open
public OpenFuture open(long recipient, long rwSize, long packetSize, Buffer buffer)
Description copied from interface:Channel
For a server channel, this method will actually open the channel- Specified by:
open
in interfaceChannel
- Parameters:
recipient
- Recipient identifier (UINT32 represented as along
)rwSize
- Read/Write window size (uint32
)packetSize
- Preferred maximum packet size (uint32
)buffer
- IncomingBuffer
that triggered the call. Note: the buffer's read position is exactly after the information that read to this call was decoded- Returns:
- An
OpenFuture
for the channel open request
-
handleOpenSuccess
public void handleOpenSuccess(long recipient, long rwSize, long packetSize, Buffer buffer)
Description copied from interface:Channel
For a client channel, this method will be called internally by the session when the confirmation has been received.- Specified by:
handleOpenSuccess
in interfaceChannel
- Parameters:
recipient
- Recipient identifier (UINT32 represented as along
)rwSize
- Read/Write window size (uint32
)packetSize
- Preferred maximum packet size (uint32
)buffer
- IncomingBuffer
that triggered the call. Note: the buffer's read position is exactly after the information that read to this call was decoded
-
doOpen
protected abstract void doOpen() throws java.io.IOException
- Throws:
java.io.IOException
-
handleOpenFailure
public void handleOpenFailure(Buffer buffer)
Description copied from interface:Channel
For a client channel, this method will be called internally by the session when the server has rejected this channel opening.- Specified by:
handleOpenFailure
in interfaceChannel
- Parameters:
buffer
- IncomingBuffer
that triggered the call. Note: the buffer's read position is exactly after the information that read to this call was decoded
-
doWriteData
protected void doWriteData(byte[] data, int off, long len) throws java.io.IOException
- Specified by:
doWriteData
in classAbstractChannel
- Throws:
java.io.IOException
-
doWriteExtendedData
protected void doWriteExtendedData(byte[] data, int off, long len) throws java.io.IOException
- Specified by:
doWriteExtendedData
in classAbstractChannel
- Throws:
java.io.IOException
-
handleWindowAdjust
public void handleWindowAdjust(Buffer buffer) throws java.io.IOException
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_WINDOW_ADJUST
received- Specified by:
handleWindowAdjust
in interfaceChannel
- Overrides:
handleWindowAdjust
in classAbstractChannel
- Parameters:
buffer
- The rest of the message dataBuffer
after decoding the channel identifiers- Throws:
java.io.IOException
- If failed to handle the message
-
mayWrite
protected boolean mayWrite()
- Overrides:
mayWrite
in classAbstractChannel
-
getExitStatus
public java.lang.Integer getExitStatus()
- Specified by:
getExitStatus
in interfaceClientChannel
- Returns:
- The signaled exit status via "exit-status" request -
null
if not signaled
-
getExitSignal
public java.lang.String getExitSignal()
- Specified by:
getExitSignal
in interfaceClientChannel
- Returns:
- The signaled exit signal via "exit-signal" -
null
if not signaled
-
-