Package org.apache.sshd.common.channel
Class AbstractChannel
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Channel
,AttributeRepository
,AttributeStore
,Channel
,ChannelIdentifier
,ChannelListenerManager
,ChannelStreamWriterResolver
,ChannelStreamWriterResolverManager
,Closeable
,PropertyResolver
,SessionContextHolder
,SessionHolder<Session>
,ExecutorServiceCarrier
- Direct Known Subclasses:
AbstractClientChannel
,AbstractServerChannel
public abstract class AbstractChannel
extends AbstractInnerCloseable
implements Channel, ExecutorServiceCarrier
Provides common client/server channel functionality
- Author:
- Apache MINA SSHD Project
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
protected static enum
static interface
AAbstractChannel.PacketValidator
can validate packet lengths.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 extends Object>
-
Field Summary
Modifier and TypeFieldDescriptionprotected final ChannelListener
protected final Collection<ChannelListener>
Channel events listenerprotected final AtomicBoolean
static final AbstractChannel.PacketValidator
A defaultAbstractChannel.PacketValidator
that enforces that the packet size is not greater than the maximum packet size of the channel's local window.protected final AtomicBoolean
protected final AtomicBoolean
Deprecated.since 2.10.1protected final DefaultCloseFuture
protected AtomicReference<AbstractChannel.GracefulState>
protected final AtomicBoolean
static final IntUnaryOperator
Default growth factor function used to resize response buffersprotected ConnectionService
protected final AtomicBoolean
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
ModifierConstructorDescriptionprotected
AbstractChannel
(boolean client) protected
AbstractChannel
(boolean client, Collection<? extends RequestHandler<Channel>> handlers) protected
AbstractChannel
(String discriminator, boolean client) protected
AbstractChannel
(String discriminator, boolean client, Collection<? extends RequestHandler<Channel>> handlers, CloseableExecutorService executorService) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addChannelListener
(ChannelListener listener) Add a channel listenerprotected Date
addPendingRequest
(String request, boolean wantReply) Add a channel request to the tracked pending ones if reply is expectedvoid
addRequestHandler
(RequestHandler<Channel> handler) void
<T> T
computeAttributeIfAbsent
(AttributeRepository.AttributeKey<T> key, Function<? super AttributeRepository.AttributeKey<T>, ? extends T> resolver) protected void
protected abstract void
doWriteData
(byte[] data, int off, long len) protected abstract void
doWriteExtendedData
(byte[] data, int off, long len) <T> T
int
long
protected Closeable
Retrieves the currently setAbstractChannel.PacketValidator
.long
void
handleChannelRegistrationResult
(ConnectionService service, Session session, long channelId, boolean registered) Invoked after being successfully registered by the connection service - should throw aRuntimeException
if not registeredprotected void
handleChannelRequest
(String req, boolean wantReply, Buffer buffer) void
Called by the connection service to inform the channel that it has bee unregistered.void
Invoked whenSSH_MSG_CHANNEL_CLOSE
receivedvoid
handleData
(Buffer buffer) Invoked whenSSH_MSG_CHANNEL_DATA
receivedvoid
Invoked whenSSH_MSG_CHANNEL_EOF
receivedvoid
handleExtendedData
(Buffer buffer) Invoked whenSSH_MSG_CHANNEL_EXTENDED_DATA
receivedvoid
Invoked whenSSH_MSG_CHANNEL_FAILURE
receivedprotected RequestHandler.Result
handleInternalRequest
(String req, boolean wantReply, Buffer buffer) Called byhandleUnknownChannelRequest(String, boolean, Buffer)
in order to allow channel request handling if none of the registered handlers processed the request - last chance.void
handleRequest
(Buffer buffer) Invoked whenSSH_MSG_CHANNEL_REQUEST
receivedvoid
Invoked whenSSH_MSG_CHANNEL_SUCCESS
receivedprotected void
handleUnknownChannelRequest
(String req, boolean wantReply, Buffer buffer) Called when none of the register request handlers reported handling the requestvoid
handleWindowAdjust
(Buffer buffer) Invoked whenSSH_MSG_CHANNEL_WINDOW_ADJUST
receivedvoid
init
(ConnectionService service, Session session, long id) Invoked when the local channel is initial createdprotected void
invokeChannelSignaller
(Invoker<ChannelListener, Void> invoker) boolean
boolean
boolean
protected boolean
mayWrite()
protected void
notifyStateChanged
(String hint) protected void
notifyStateChanged
(ChannelListener listener, String hint) protected void
preClose()
<T> T
void
removeChannelListener
(ChannelListener listener) Remove a channel listenerprotected Date
removePendingRequest
(String request) Removes a channel request from the tracked onesvoid
removeRequestHandler
(RequestHandler<Channel> handler) protected IoWriteFuture
sendEof()
SendsSSH_MSG_CHANNEL_EOF
provided not already sent and current channel state allows it.protected IoWriteFuture
sendResponse
(Buffer buffer, String req, RequestHandler.Result result, boolean wantReply) protected void
sendWindowAdjust
(long len) <T> T
setAttribute
(AttributeRepository.AttributeKey<T> key, T value) void
void
Sets aAbstractChannel.PacketValidator
.protected void
setRecipient
(long recipient) void
signalChannelClosed
(Throwable reason) protected void
signalChannelClosed
(ChannelListener listener, Throwable reason) protected void
protected void
signalChannelInitialized
(ChannelListener listener) protected void
signalChannelOpenFailure
(Throwable reason) protected void
signalChannelOpenFailure
(ChannelListener listener, Throwable reason) protected void
protected void
signalChannelOpenSuccess
(ChannelListener listener) toString()
protected long
validateIncomingDataSize
(int cmd, long len) writePacket
(Buffer buffer) Encode and send the given buffer.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.channel.Channel
addRequestHandlers, handleOpenFailure, handleOpenSuccess, open, removeRequestHandlers, resolveAttribute
Methods inherited from interface org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolverManager
resolveChannelStreamWriter
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, getString, getStringProperty, isEmpty
Methods inherited from interface org.apache.sshd.common.session.SessionHolder
getSessionContext
-
Field Details
-
RESPONSE_BUFFER_GROWTH_FACTOR
Default growth factor function used to resize response buffers -
DEFAULT_PACKET_VALIDATOR
A defaultAbstractChannel.PacketValidator
that enforces that the packet size is not greater than the maximum packet size of the channel's local window. -
service
-
initialized
-
eofReceived
-
eofSent
Deprecated.since 2.10.1Obsolete and unused; present only for API backwards compatibility. UseisEofSent()
to determine whether EOF is already sent on this channel, andsendEof()
to send EOF. The latter will returnnull
if EOF already was sent. -
unregisterSignaled
-
closeSignaled
-
gracefulState
-
gracefulFuture
-
channelListeners
Channel events listener -
channelListenerProxy
-
-
Constructor Details
-
AbstractChannel
protected AbstractChannel(boolean client) -
AbstractChannel
-
AbstractChannel
-
AbstractChannel
protected AbstractChannel(String discriminator, boolean client, Collection<? extends RequestHandler<Channel>> handlers, CloseableExecutorService executorService)
-
-
Method Details
-
getRequestHandlers
- Specified by:
getRequestHandlers
in interfaceChannel
-
addRequestHandler
- Specified by:
addRequestHandler
in interfaceChannel
-
removeRequestHandler
- Specified by:
removeRequestHandler
in interfaceChannel
-
getChannelId
public long getChannelId()- Specified by:
getChannelId
in interfaceChannelIdentifier
- Returns:
- Local channel UINT32 identifier
-
getRecipient
public long getRecipient()- Specified by:
getRecipient
in interfaceChannel
- Returns:
- Remote channel UITN32 identifier
-
setRecipient
protected void setRecipient(long recipient) -
getLocalWindow
- Specified by:
getLocalWindow
in interfaceChannel
-
getRemoteWindow
- Specified by:
getRemoteWindow
in interfaceChannel
-
getSession
- Specified by:
getSession
in interfaceSessionHolder<Session>
-
getParentPropertyResolver
- Specified by:
getParentPropertyResolver
in interfacePropertyResolver
-
getExecutorService
- Specified by:
getExecutorService
in interfaceExecutorServiceCarrier
-
getChannelStreamWriterResolver
- Specified by:
getChannelStreamWriterResolver
in interfaceChannelStreamWriterResolverManager
-
setChannelStreamWriterResolver
- Specified by:
setChannelStreamWriterResolver
in interfaceChannelStreamWriterResolverManager
-
resolveChannelStreamWriterResolver
- Specified by:
resolveChannelStreamWriterResolver
in interfaceChannelStreamWriterResolverManager
-
addPendingRequest
Add a channel request to the tracked pending ones if reply is expected- Parameters:
request
- The request typewantReply
-true
if reply is expected- Returns:
- The allocated
Date
timestamp -null
if no reply is expected (in which case the request is not tracked) - Throws:
IllegalArgumentException
- If the request is already being tracked- See Also:
-
removePendingRequest
Removes a channel request from the tracked ones- Parameters:
request
- The request type- Returns:
- The allocated
Date
timestamp -null
if the specified request type is not being tracked or has not been added to the tracked ones to begin with - See Also:
-
handleRequest
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_REQUEST
received- Specified by:
handleRequest
in interfaceChannel
- Parameters:
buffer
- The rest of the message dataBuffer
after decoding the channel identifiers- Throws:
IOException
- If failed to handle the message
-
handleChannelRequest
protected void handleChannelRequest(String req, boolean wantReply, Buffer buffer) throws IOException - Throws:
IOException
-
handleUnknownChannelRequest
protected void handleUnknownChannelRequest(String req, boolean wantReply, Buffer buffer) throws IOException Called when none of the register request handlers reported handling the request- Parameters:
req
- The request typewantReply
- Whether reply is requestedbuffer
- TheBuffer
containing extra request-specific data- Throws:
IOException
- If failed to send the response (if needed)- See Also:
-
handleInternalRequest
protected RequestHandler.Result handleInternalRequest(String req, boolean wantReply, Buffer buffer) throws IOException Called byhandleUnknownChannelRequest(String, boolean, Buffer)
in order to allow channel request handling if none of the registered handlers processed the request - last chance.- Parameters:
req
- The request typewantReply
- Whether reply is requestedbuffer
- TheBuffer
containing extra request-specific data- Returns:
- The handling result - if
null
orUnsupported
and reply is required then a failure message will be sent - Throws:
IOException
- If failed to process the request internally
-
sendResponse
protected IoWriteFuture sendResponse(Buffer buffer, String req, RequestHandler.Result result, boolean wantReply) throws IOException - Throws:
IOException
-
init
Description copied from interface:Channel
Invoked when the local channel is initial created- Specified by:
init
in interfaceChannel
- Parameters:
service
- TheConnectionService
through which the channel is initializedsession
- TheSession
associated with the channelid
- The locally assigned channel identifier (UINT32 represented as along
)- Throws:
IOException
- If failed to process the initialization
-
signalChannelInitialized
- Throws:
IOException
-
signalChannelInitialized
-
signalChannelOpenSuccess
protected void signalChannelOpenSuccess() -
signalChannelOpenSuccess
-
isInitialized
public boolean isInitialized()- Specified by:
isInitialized
in interfaceChannel
- Returns:
true
if call toChannel.init(ConnectionService, Session, long)
was successfully completed
-
handleChannelRegistrationResult
public void handleChannelRegistrationResult(ConnectionService service, Session session, long channelId, boolean registered) Description copied from interface:Channel
Invoked after being successfully registered by the connection service - should throw aRuntimeException
if not registered- Specified by:
handleChannelRegistrationResult
in interfaceChannel
- Parameters:
service
- TheConnectionService
through which the channel is registeredsession
- TheSession
associated with the channelchannelId
- The locally assigned channel identifier (UINT32 represented as along
)registered
- Whether registration was successful or not
-
signalChannelOpenFailure
-
signalChannelOpenFailure
-
notifyStateChanged
-
notifyStateChanged
-
addChannelListener
Description copied from interface:ChannelListenerManager
Add a channel listener- Specified by:
addChannelListener
in interfaceChannelListenerManager
- Parameters:
listener
- TheChannelListener
to add - notnull
-
removeChannelListener
Description copied from interface:ChannelListenerManager
Remove a channel listener- Specified by:
removeChannelListener
in interfaceChannelListenerManager
- Parameters:
listener
- TheChannelListener
to remove
-
getChannelListenerProxy
- Specified by:
getChannelListenerProxy
in interfaceChannelListenerManager
- Returns:
- A (never
null
proxyChannelListener
that represents all the currently registered listeners. Any method invocation on the proxy is replicated to the currently registered listeners
-
handleClose
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_CLOSE
received- Specified by:
handleClose
in interfaceChannel
- Throws:
IOException
- If failed to handle the message
-
getInnerCloseable
- Specified by:
getInnerCloseable
in classAbstractInnerCloseable
-
preClose
protected void preClose()- Overrides:
preClose
in classAbstractCloseable
-
handleChannelUnregistration
Description copied from interface:Channel
Called by the connection service to inform the channel that it has bee unregistered.- Specified by:
handleChannelUnregistration
in interfaceChannel
- Parameters:
service
- TheConnectionService
through which the channel is unregistered
-
signalChannelClosed
-
signalChannelClosed
-
invokeChannelSignaller
- Throws:
Throwable
-
writePacket
Description copied from interface:Channel
Encode and send the given buffer. Note: for session packets the buffer has to have 5 bytes free at the beginning to allow the encoding to take place. Also, the write position of the buffer has to be set to the position of the last byte to write.- Specified by:
writePacket
in interfaceChannel
- Parameters:
buffer
- the buffer to encode and send. NOTE: the buffer must not be touched until the returned write future is completed.- Returns:
- An
IoWriteFuture
that can be used to check when the packet has actually been sent - Throws:
IOException
- if an error occurred when encoding or sending the packet
-
mayWrite
protected boolean mayWrite() -
handleData
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_DATA
received- Specified by:
handleData
in interfaceChannel
- Parameters:
buffer
- The rest of the message dataBuffer
after decoding the channel identifiers- Throws:
IOException
- If failed to handle the message
-
handleExtendedData
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_EXTENDED_DATA
received- Specified by:
handleExtendedData
in interfaceChannel
- Parameters:
buffer
- The rest of the message dataBuffer
after decoding the channel identifiers- Throws:
IOException
- If failed to handle the message
-
validateIncomingDataSize
- Throws:
IOException
-
getPacketValidator
Retrieves the currently setAbstractChannel.PacketValidator
.- Returns:
- the validator, never
null
-
setPacketValidator
Sets aAbstractChannel.PacketValidator
.- Parameters:
validator
- the validator to set, ifnull
theDEFAULT_PACKET_VALIDATOR
is set
-
handleEof
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_EOF
received- Specified by:
handleEof
in interfaceChannel
- Throws:
IOException
- If failed to handle the message
-
isEofSignalled
public boolean isEofSignalled()- Specified by:
isEofSignalled
in interfaceChannel
- Returns:
true
if the peer signaled that it will not send any more data- See Also:
-
handleWindowAdjust
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_WINDOW_ADJUST
received- Specified by:
handleWindowAdjust
in interfaceChannel
- Parameters:
buffer
- The rest of the message dataBuffer
after decoding the channel identifiers- Throws:
IOException
- If failed to handle the message
-
handleSuccess
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_SUCCESS
received- Specified by:
handleSuccess
in interfaceChannel
- Throws:
IOException
- If failed to handle the message
-
handleFailure
Description copied from interface:Channel
Invoked whenSSH_MSG_CHANNEL_FAILURE
received- Specified by:
handleFailure
in interfaceChannel
- Throws:
IOException
- If failed to handle the message
-
doWriteData
- Throws:
IOException
-
doWriteExtendedData
- Throws:
IOException
-
sendEof
SendsSSH_MSG_CHANNEL_EOF
provided not already sent and current channel state allows it.- Returns:
- The
IoWriteFuture
of the sent packet -null
if message not sent due to channel state (or already sent) - Throws:
IOException
- If failed to send the packet
-
isEofSent
public boolean isEofSent() -
getProperties
- Specified by:
getProperties
in interfacePropertyResolver
-
getAttributesCount
public int getAttributesCount()- Specified by:
getAttributesCount
in interfaceAttributeRepository
-
getAttribute
- Specified by:
getAttribute
in interfaceAttributeRepository
-
attributeKeys
- Specified by:
attributeKeys
in interfaceAttributeRepository
-
computeAttributeIfAbsent
public <T> T computeAttributeIfAbsent(AttributeRepository.AttributeKey<T> key, Function<? super AttributeRepository.AttributeKey<T>, ? extends T> resolver) - Specified by:
computeAttributeIfAbsent
in interfaceAttributeStore
-
setAttribute
- Specified by:
setAttribute
in interfaceAttributeStore
-
removeAttribute
- Specified by:
removeAttribute
in interfaceAttributeStore
-
clearAttributes
public void clearAttributes()- Specified by:
clearAttributes
in interfaceAttributeStore
-
configureWindow
protected void configureWindow() -
sendWindowAdjust
- Throws:
IOException
-
toString
-