public abstract class AbstractChannel extends AbstractInnerCloseable implements Channel, ExecutorServiceCarrier
Modifier and Type | Class and Description |
---|---|
class |
AbstractChannel.GracefulChannelCloseable |
protected static class |
AbstractChannel.GracefulState |
AbstractCloseable.State
AttributeRepository.AttributeKey<T>
Modifier and Type | Field and Description |
---|---|
protected ChannelListener |
channelListenerProxy |
protected Collection<ChannelListener> |
channelListeners
Channel events listener
|
protected AtomicBoolean |
closeSignaled |
protected AtomicBoolean |
eofReceived |
protected AtomicBoolean |
eofSent |
protected DefaultCloseFuture |
gracefulFuture |
protected AtomicReference<AbstractChannel.GracefulState> |
gracefulState |
protected AtomicBoolean |
initialized |
static IntUnaryOperator |
RESPONSE_BUFFER_GROWTH_FACTOR
Default growth factor function used to resize response buffers
|
protected ConnectionService |
service |
protected AtomicBoolean |
unregisterSignaled |
closeFuture, futureLock, state
log
CHANNEL_EXEC, CHANNEL_SHELL, CHANNEL_SUBSYSTEM
EMPTY
NONE
Modifier | Constructor and Description |
---|---|
protected |
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) |
Modifier and Type | Method and Description |
---|---|
void |
addChannelListener(ChannelListener listener)
Add a channel listener
|
protected Date |
addPendingRequest(String request,
boolean wantReply)
Add a channel request to the tracked pending ones if reply is expected
|
void |
addRequestHandler(RequestHandler<Channel> handler) |
Collection<AttributeRepository.AttributeKey<?>> |
attributeKeys() |
void |
clearAttributes()
Removes all currently stored user-defined attributes
|
<T> T |
computeAttributeIfAbsent(AttributeRepository.AttributeKey<T> key,
Function<? super AttributeRepository.AttributeKey<T>,? extends T> resolver)
If the specified key is not already associated with a value (or is mapped to
null ), attempts to compute
its value using the given mapping function and enters it into this map unless null . |
protected void |
configureWindow() |
protected abstract void |
doWriteData(byte[] data,
int off,
long len) |
protected abstract void |
doWriteExtendedData(byte[] data,
int off,
long len) |
<T> T |
getAttribute(AttributeRepository.AttributeKey<T> key)
Returns the value of the user-defined attribute.
|
int |
getAttributesCount() |
long |
getChannelId() |
ChannelListener |
getChannelListenerProxy() |
ChannelStreamWriterResolver |
getChannelStreamWriterResolver() |
CloseableExecutorService |
getExecutorService() |
protected Closeable |
getInnerCloseable() |
LocalWindow |
getLocalWindow() |
PropertyResolver |
getParentPropertyResolver() |
Map<String,Object> |
getProperties()
A map of properties that can be used to configure the SSH server or client.
|
long |
getRecipient() |
RemoteWindow |
getRemoteWindow() |
List<RequestHandler<Channel>> |
getRequestHandlers() |
Session |
getSession() |
void |
handleChannelRegistrationResult(ConnectionService service,
Session session,
long channelId,
boolean registered)
Invoked after being successfully registered by the connection service - should throw a
RuntimeException
if not registered |
protected void |
handleChannelRequest(String req,
boolean wantReply,
Buffer buffer) |
void |
handleChannelUnregistration(ConnectionService service)
Called by the connection service to inform the channel that it has bee unregistered.
|
void |
handleClose()
Invoked when
SSH_MSG_CHANNEL_CLOSE received |
void |
handleData(Buffer buffer)
Invoked when
SSH_MSG_CHANNEL_DATA received |
void |
handleEof()
Invoked when
SSH_MSG_CHANNEL_EOF received |
void |
handleExtendedData(Buffer buffer)
Invoked when
SSH_MSG_CHANNEL_EXTENDED_DATA received |
void |
handleFailure()
Invoked when
SSH_MSG_CHANNEL_FAILURE received |
protected RequestHandler.Result |
handleInternalRequest(String req,
boolean wantReply,
Buffer buffer)
Called by
handleUnknownChannelRequest(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 when
SSH_MSG_CHANNEL_REQUEST received |
void |
handleSuccess()
Invoked when
SSH_MSG_CHANNEL_SUCCESS received |
protected void |
handleUnknownChannelRequest(String req,
boolean wantReply,
Buffer buffer)
Called when none of the register request handlers reported handling the request
|
void |
handleWindowAdjust(Buffer buffer)
Invoked when
SSH_MSG_CHANNEL_WINDOW_ADJUST received |
void |
init(ConnectionService service,
Session session,
long id)
Invoked when the local channel is initial created
|
protected void |
invokeChannelSignaller(Invoker<ChannelListener,Void> invoker) |
boolean |
isEofSent() |
boolean |
isEofSignalled() |
boolean |
isInitialized() |
protected boolean |
mayWrite() |
protected void |
notifyStateChanged(ChannelListener listener,
String hint) |
protected void |
notifyStateChanged(String hint) |
protected void |
preClose()
preClose is guaranteed to be called before doCloseGracefully or doCloseImmediately.
|
<T> T |
removeAttribute(AttributeRepository.AttributeKey<T> key)
Removes the user-defined attribute
|
void |
removeChannelListener(ChannelListener listener)
Remove a channel listener
|
protected Date |
removePendingRequest(String request)
Removes a channel request from the tracked ones
|
void |
removeRequestHandler(RequestHandler<Channel> handler) |
ChannelStreamWriterResolver |
resolveChannelStreamWriterResolver() |
protected IoWriteFuture |
sendEof()
Sends
SSH_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)
Sets a user-defined attribute.
|
void |
setChannelStreamWriterResolver(ChannelStreamWriterResolver resolver) |
protected void |
setRecipient(long recipient) |
protected void |
signalChannelClosed(ChannelListener listener,
Throwable reason) |
void |
signalChannelClosed(Throwable reason) |
protected void |
signalChannelInitialized() |
protected void |
signalChannelInitialized(ChannelListener listener) |
protected void |
signalChannelOpenFailure(ChannelListener listener,
Throwable reason) |
protected void |
signalChannelOpenFailure(Throwable reason) |
protected void |
signalChannelOpenSuccess() |
protected void |
signalChannelOpenSuccess(ChannelListener listener) |
String |
toString() |
protected long |
validateIncomingDataSize(int cmd,
long len) |
IoWriteFuture |
writePacket(Buffer buffer)
Encode and send the given buffer.
|
doCloseGracefully, doCloseImmediately
addCloseFutureListener, builder, close, getFutureLock, isClosed, isClosing, removeCloseFutureListener
debug, debug, debug, debug, debug, error, error, error, error, error, getSimplifiedLogger, info, info, warn, warn, warn, warn, warn, warn, warn, warn
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addRequestHandlers, handleOpenFailure, handleOpenSuccess, open, removeRequestHandlers, resolveAttribute, resolveAttribute
getSessionContext
getBoolean, getBooleanProperty, getCharset, getInteger, getIntProperty, getLong, getLongProperty, getObject, getString, getStringProperty, isEmpty, isEmpty
ofAttributesMap, ofKeyValuePair
resolveChannelStreamWriter
addCloseFutureListener, close, close, close, getMaxCloseWaitTime, isClosed, isClosing, isOpen, removeCloseFutureListener
public static final IntUnaryOperator RESPONSE_BUFFER_GROWTH_FACTOR
protected ConnectionService service
protected final AtomicBoolean initialized
protected final AtomicBoolean eofReceived
protected final AtomicBoolean eofSent
protected final AtomicBoolean unregisterSignaled
protected final AtomicBoolean closeSignaled
protected AtomicReference<AbstractChannel.GracefulState> gracefulState
protected final DefaultCloseFuture gracefulFuture
protected final Collection<ChannelListener> channelListeners
protected final ChannelListener channelListenerProxy
protected 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)
public List<RequestHandler<Channel>> getRequestHandlers()
public void addRequestHandler(RequestHandler<Channel> handler)
public void removeRequestHandler(RequestHandler<Channel> handler)
public long getChannelId()
getChannelId
in interface ChannelIdentifier
public long getRecipient()
protected void setRecipient(long recipient)
public LocalWindow getLocalWindow()
public RemoteWindow getRemoteWindow()
public Session getSession()
getSession
in interface SessionHolder<Session>
public PropertyResolver getParentPropertyResolver()
getParentPropertyResolver
in interface PropertyResolver
null
if no parentpublic CloseableExecutorService getExecutorService()
getExecutorService
in interface ExecutorServiceCarrier
CloseableExecutorService
to usepublic ChannelStreamWriterResolver getChannelStreamWriterResolver()
getChannelStreamWriterResolver
in interface ChannelStreamWriterResolverManager
public void setChannelStreamWriterResolver(ChannelStreamWriterResolver resolver)
setChannelStreamWriterResolver
in interface ChannelStreamWriterResolverManager
public ChannelStreamWriterResolver resolveChannelStreamWriterResolver()
resolveChannelStreamWriterResolver
in interface ChannelStreamWriterResolverManager
protected Date addPendingRequest(String request, boolean wantReply)
request
- The request typewantReply
- true
if reply is expectedDate
timestamp - null
if no reply is expected (in
which case the request is not tracked)IllegalArgumentException
- If the request is already being trackedremovePendingRequest(String)
protected Date removePendingRequest(String request)
request
- The request typeDate
timestamp - null
if the specified request type is not being
tracked or has not been added to the tracked ones to begin withaddPendingRequest(String, boolean)
public void handleRequest(Buffer buffer) throws IOException
Channel
SSH_MSG_CHANNEL_REQUEST
receivedbuffer
- The rest of the message data Buffer
after decoding the channel identifiersIOException
- If failed to handle the messageprotected void handleChannelRequest(String req, boolean wantReply, Buffer buffer) throws IOException
IOException
protected void handleUnknownChannelRequest(String req, boolean wantReply, Buffer buffer) throws IOException
req
- The request typewantReply
- Whether reply is requestedbuffer
- The Buffer
containing extra request-specific dataIOException
- If failed to send the response (if needed)handleInternalRequest(String, boolean, Buffer)
protected RequestHandler.Result handleInternalRequest(String req, boolean wantReply, Buffer buffer) throws IOException
handleUnknownChannelRequest(String, boolean, Buffer)
in order to allow channel request
handling if none of the registered handlers processed the request - last chance.req
- The request typewantReply
- Whether reply is requestedbuffer
- The Buffer
containing extra request-specific datanull
or Unsupported
and reply is required then a
failure message will be sentIOException
- If failed to process the request internallyprotected IoWriteFuture sendResponse(Buffer buffer, String req, RequestHandler.Result result, boolean wantReply) throws IOException
IOException
public void init(ConnectionService service, Session session, long id) throws IOException
Channel
service
- The ConnectionService
through which the channel is initializedsession
- The Session
associated with the channelid
- The locally assigned channel identifier (UINT32 represented as a long
)IOException
- If failed to process the initializationprotected void signalChannelInitialized() throws IOException
IOException
protected void signalChannelInitialized(ChannelListener listener)
protected void signalChannelOpenSuccess()
protected void signalChannelOpenSuccess(ChannelListener listener)
public boolean isInitialized()
true
if call to Channel.init(ConnectionService, Session, long)
was successfully completedpublic void handleChannelRegistrationResult(ConnectionService service, Session session, long channelId, boolean registered)
Channel
RuntimeException
if not registeredservice
- The ConnectionService
through which the channel is registeredsession
- The Session
associated with the channelchannelId
- The locally assigned channel identifier (UINT32 represented as a long
)registered
- Whether registration was successful or notprotected void signalChannelOpenFailure(Throwable reason)
protected void signalChannelOpenFailure(ChannelListener listener, Throwable reason)
protected void notifyStateChanged(String hint)
protected void notifyStateChanged(ChannelListener listener, String hint)
public void addChannelListener(ChannelListener listener)
ChannelListenerManager
addChannelListener
in interface ChannelListenerManager
listener
- The ChannelListener
to add - not null
public void removeChannelListener(ChannelListener listener)
ChannelListenerManager
removeChannelListener
in interface ChannelListenerManager
listener
- The ChannelListener
to removepublic ChannelListener getChannelListenerProxy()
getChannelListenerProxy
in interface ChannelListenerManager
null
proxy ChannelListener
that represents all the currently registered
listeners. Any method invocation on the proxy is replicated to the currently registered listenerspublic void handleClose() throws IOException
Channel
SSH_MSG_CHANNEL_CLOSE
receivedIOException
- If failed to handle the messageprotected Closeable getInnerCloseable()
getInnerCloseable
in class AbstractInnerCloseable
protected void preClose()
AbstractCloseable
preClose
in class AbstractCloseable
public void handleChannelUnregistration(ConnectionService service)
Channel
service
- The ConnectionService
through which the channel is unregisteredpublic void signalChannelClosed(Throwable reason)
protected void signalChannelClosed(ChannelListener listener, Throwable reason)
protected void invokeChannelSignaller(Invoker<ChannelListener,Void> invoker) throws Throwable
Throwable
public IoWriteFuture writePacket(Buffer buffer) throws IOException
Channel
buffer
- the buffer to encode and send. NOTE: the buffer must not be touched until the returned
write future is completed.IoWriteFuture
that can be used to check when the packet has actually been sentIOException
- if an error occurred when encoding or sending the packetprotected boolean mayWrite()
public void handleData(Buffer buffer) throws IOException
Channel
SSH_MSG_CHANNEL_DATA
receivedbuffer
- The rest of the message data Buffer
after decoding the channel identifiersIOException
- If failed to handle the messagepublic void handleExtendedData(Buffer buffer) throws IOException
Channel
SSH_MSG_CHANNEL_EXTENDED_DATA
receivedbuffer
- The rest of the message data Buffer
after decoding the channel identifiersIOException
- If failed to handle the messageprotected long validateIncomingDataSize(int cmd, long len) throws IOException
IOException
public void handleEof() throws IOException
Channel
SSH_MSG_CHANNEL_EOF
receivedIOException
- If failed to handle the messagepublic boolean isEofSignalled()
true
if the peer signaled that it will not send any more datapublic void handleWindowAdjust(Buffer buffer) throws IOException
Channel
SSH_MSG_CHANNEL_WINDOW_ADJUST
receivedbuffer
- The rest of the message data Buffer
after decoding the channel identifiersIOException
- If failed to handle the messagepublic void handleSuccess() throws IOException
Channel
SSH_MSG_CHANNEL_SUCCESS
receivedIOException
- If failed to handle the messagepublic void handleFailure() throws IOException
Channel
SSH_MSG_CHANNEL_FAILURE
receivedIOException
- If failed to handle the messageprotected abstract void doWriteData(byte[] data, int off, long len) throws IOException
IOException
protected abstract void doWriteExtendedData(byte[] data, int off, long len) throws IOException
IOException
protected IoWriteFuture sendEof() throws IOException
SSH_MSG_CHANNEL_EOF
provided not already sent and current channel state allows it.IoWriteFuture
of the sent packet - null
if message not sent due to
channel state (or already sent)IOException
- If failed to send the packetpublic boolean isEofSent()
public Map<String,Object> getProperties()
PropertyResolver
A map of properties that can be used to configure the SSH server or client. This map will never be changed by either the server or client and is not supposed to be changed at runtime (changes are not bound to have any effect on a running client or server), though it may affect the creation of sessions later as these values are usually not cached.
Note: the type of the mapped property should match the expected configuration value type -
Long, Integer, Boolean,
String
, etc.... If it doesn't, the toString()
result of the mapped value is used to convert it to the
required type. E.g., if the mapped value is the string "1234" and the expected value is a
long
then it will be parsed into one. Also, if the mapped value is an Integer
but a long
is expected, then it will be converted into one.
getProperties
in interface PropertyResolver
Map
containing configuration values, never null
. Note: may be
immutable.public int getAttributesCount()
getAttributesCount
in interface AttributeRepository
public <T> T getAttribute(AttributeRepository.AttributeKey<T> key)
AttributeRepository
getAttribute
in interface AttributeRepository
T
- The generic attribute typekey
- The key of the attribute; must not be null
.null
if there is no value associated with the specified keypublic Collection<AttributeRepository.AttributeKey<?>> attributeKeys()
attributeKeys
in interface AttributeRepository
Collection
snapshot of all the currently registered attributes in the repositorypublic <T> T computeAttributeIfAbsent(AttributeRepository.AttributeKey<T> key, Function<? super AttributeRepository.AttributeKey<T>,? extends T> resolver)
AttributeStore
null
), attempts to compute
its value using the given mapping function and enters it into this map unless null
.computeAttributeIfAbsent
in interface AttributeStore
T
- The generic attribute typekey
- The key of the attribute; must not be null
.resolver
- The (never null
) mapping function to use if value not already mapped. If returns
null
then value is not mapped to the provided key.null
if value not mapped and resolver did not return a
non-null
value for itpublic <T> T setAttribute(AttributeRepository.AttributeKey<T> key, T value)
AttributeStore
setAttribute
in interface AttributeStore
T
- The generic attribute typekey
- The key of the attribute; must not be null
.value
- The value of the attribute; must not be null
.null
if it is new.public <T> T removeAttribute(AttributeRepository.AttributeKey<T> key)
AttributeStore
removeAttribute
in interface AttributeStore
T
- The generic attribute typekey
- The key of the attribute; must not be null
.null
if no previous valuepublic void clearAttributes()
AttributeStore
clearAttributes
in interface AttributeStore
protected void configureWindow()
protected void sendWindowAdjust(long len) throws IOException
IOException
Copyright © 2008–2024 The Apache Software Foundation. All rights reserved.