Class AbstractChannel

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
All Implemented Interfaces:
Closeable, AutoCloseable, Channel, org.apache.sshd.common.AttributeRepository, org.apache.sshd.common.AttributeStore, Channel, ChannelIdentifier, ChannelListenerManager, ChannelStreamWriterResolver, ChannelStreamWriterResolverManager, org.apache.sshd.common.Closeable, org.apache.sshd.common.PropertyResolver, org.apache.sshd.common.session.SessionContextHolder, SessionHolder<Session>, org.apache.sshd.common.util.threads.ExecutorServiceCarrier
Direct Known Subclasses:
AbstractClientChannel, AbstractServerChannel

public abstract class AbstractChannel extends org.apache.sshd.common.util.closeable.AbstractInnerCloseable implements Channel, org.apache.sshd.common.util.threads.ExecutorServiceCarrier
Provides common client/server channel functionality
Author:
Apache MINA SSHD Project
  • Field Details

  • Constructor Details

    • AbstractChannel

      protected AbstractChannel(boolean client)
    • AbstractChannel

      protected AbstractChannel(boolean client, Collection<? extends RequestHandler<Channel>> handlers)
    • AbstractChannel

      protected AbstractChannel(String discriminator, boolean client)
    • AbstractChannel

      protected AbstractChannel(String discriminator, boolean client, Collection<? extends RequestHandler<Channel>> handlers, org.apache.sshd.common.util.threads.CloseableExecutorService executorService)
  • Method Details

    • getRequestHandlers

      public List<RequestHandler<Channel>> getRequestHandlers()
      Specified by:
      getRequestHandlers in interface Channel
    • addRequestHandler

      public void addRequestHandler(RequestHandler<Channel> handler)
      Specified by:
      addRequestHandler in interface Channel
    • removeRequestHandler

      public void removeRequestHandler(RequestHandler<Channel> handler)
      Specified by:
      removeRequestHandler in interface Channel
    • getChannelId

      public long getChannelId()
      Specified by:
      getChannelId in interface ChannelIdentifier
      Returns:
      Local channel UINT32 identifier
    • getRecipient

      public long getRecipient()
      Specified by:
      getRecipient in interface Channel
      Returns:
      Remote channel UITN32 identifier
    • setRecipient

      protected void setRecipient(long recipient)
    • getLocalWindow

      public LocalWindow getLocalWindow()
      Specified by:
      getLocalWindow in interface Channel
    • getRemoteWindow

      public RemoteWindow getRemoteWindow()
      Specified by:
      getRemoteWindow in interface Channel
    • getSession

      public Session getSession()
      Specified by:
      getSession in interface SessionHolder<Session>
    • getParentPropertyResolver

      public org.apache.sshd.common.PropertyResolver getParentPropertyResolver()
      Specified by:
      getParentPropertyResolver in interface org.apache.sshd.common.PropertyResolver
    • getExecutorService

      public org.apache.sshd.common.util.threads.CloseableExecutorService getExecutorService()
      Specified by:
      getExecutorService in interface org.apache.sshd.common.util.threads.ExecutorServiceCarrier
    • getChannelStreamWriterResolver

      public ChannelStreamWriterResolver getChannelStreamWriterResolver()
      Specified by:
      getChannelStreamWriterResolver in interface ChannelStreamWriterResolverManager
    • setChannelStreamWriterResolver

      public void setChannelStreamWriterResolver(ChannelStreamWriterResolver resolver)
      Specified by:
      setChannelStreamWriterResolver in interface ChannelStreamWriterResolverManager
    • resolveChannelStreamWriterResolver

      public ChannelStreamWriterResolver resolveChannelStreamWriterResolver()
      Specified by:
      resolveChannelStreamWriterResolver in interface ChannelStreamWriterResolverManager
    • addPendingRequest

      protected Date addPendingRequest(String request, boolean wantReply)
      Add a channel request to the tracked pending ones if reply is expected
      Parameters:
      request - The request type
      wantReply - 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

      protected Date removePendingRequest(String request)
      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

      public void handleRequest(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Description copied from interface: Channel
      Invoked when SSH_MSG_CHANNEL_REQUEST received
      Specified by:
      handleRequest in interface Channel
      Parameters:
      buffer - The rest of the message data Buffer after decoding the channel identifiers
      Throws:
      IOException - If failed to handle the message
    • handleChannelRequest

      protected void handleChannelRequest(String req, boolean wantReply, org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Throws:
      IOException
    • handleUnknownChannelRequest

      protected void handleUnknownChannelRequest(String req, boolean wantReply, org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Called when none of the register request handlers reported handling the request
      Parameters:
      req - The request type
      wantReply - Whether reply is requested
      buffer - The Buffer 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, org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Called by handleUnknownChannelRequest(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 type
      wantReply - Whether reply is requested
      buffer - The Buffer containing extra request-specific data
      Returns:
      The handling result - if null or Unsupported and reply is required then a failure message will be sent
      Throws:
      IOException - If failed to process the request internally
    • sendResponse

      protected org.apache.sshd.common.io.IoWriteFuture sendResponse(org.apache.sshd.common.util.buffer.Buffer buffer, String req, RequestHandler.Result result, boolean wantReply) throws IOException
      Throws:
      IOException
    • init

      public void init(ConnectionService service, Session session, long id) throws IOException
      Description copied from interface: Channel
      Invoked when the local channel is initial created
      Specified by:
      init in interface Channel
      Parameters:
      service - The ConnectionService through which the channel is initialized
      session - The Session associated with the channel
      id - The locally assigned channel identifier (UINT32 represented as a long)
      Throws:
      IOException - If failed to process the initialization
    • signalChannelInitialized

      protected void signalChannelInitialized() throws IOException
      Throws:
      IOException
    • signalChannelOpenSuccess

      protected void signalChannelOpenSuccess()
    • isInitialized

      public boolean isInitialized()
      Specified by:
      isInitialized in interface Channel
      Returns:
      true if call to Channel.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 a RuntimeException if not registered
      Specified by:
      handleChannelRegistrationResult in interface Channel
      Parameters:
      service - The ConnectionService through which the channel is registered
      session - The Session associated with the channel
      channelId - The locally assigned channel identifier (UINT32 represented as a long)
      registered - Whether registration was successful or not
    • signalChannelOpenFailure

      protected void signalChannelOpenFailure(Throwable reason)
    • notifyStateChanged

      protected void notifyStateChanged(String hint)
    • addChannelListener

      public void addChannelListener(ChannelListener listener)
      Description copied from interface: ChannelListenerManager
      Add a channel listener
      Specified by:
      addChannelListener in interface ChannelListenerManager
      Parameters:
      listener - The ChannelListener to add - not null
    • removeChannelListener

      public void removeChannelListener(ChannelListener listener)
      Description copied from interface: ChannelListenerManager
      Remove a channel listener
      Specified by:
      removeChannelListener in interface ChannelListenerManager
      Parameters:
      listener - The ChannelListener to remove
    • getChannelListenerProxy

      public ChannelListener getChannelListenerProxy()
      Specified by:
      getChannelListenerProxy in interface ChannelListenerManager
      Returns:
      A (never null proxy ChannelListener that represents all the currently registered listeners. Any method invocation on the proxy is replicated to the currently registered listeners
    • handleClose

      public void handleClose() throws IOException
      Description copied from interface: Channel
      Invoked when SSH_MSG_CHANNEL_CLOSE received
      Specified by:
      handleClose in interface Channel
      Throws:
      IOException - If failed to handle the message
    • getInnerCloseable

      protected org.apache.sshd.common.Closeable getInnerCloseable()
      Specified by:
      getInnerCloseable in class org.apache.sshd.common.util.closeable.AbstractInnerCloseable
    • preClose

      protected void preClose()
      Overrides:
      preClose in class org.apache.sshd.common.util.closeable.AbstractCloseable
    • handleChannelUnregistration

      public void handleChannelUnregistration(ConnectionService service)
      Description copied from interface: Channel
      Called by the connection service to inform the channel that it has bee unregistered.
      Specified by:
      handleChannelUnregistration in interface Channel
      Parameters:
      service - The ConnectionService through which the channel is unregistered
    • signalChannelClosed

      public void signalChannelClosed(Throwable reason)
    • invokeChannelSignaller

      protected void invokeChannelSignaller(Consumer<ChannelListener> invoker)
    • writePacket

      public org.apache.sshd.common.io.IoWriteFuture writePacket(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      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 interface Channel
      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

      public void handleData(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Description copied from interface: Channel
      Invoked when SSH_MSG_CHANNEL_DATA received
      Specified by:
      handleData in interface Channel
      Parameters:
      buffer - The rest of the message data Buffer after decoding the channel identifiers
      Throws:
      IOException - If failed to handle the message
    • handleExtendedData

      public void handleExtendedData(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Description copied from interface: Channel
      Invoked when SSH_MSG_CHANNEL_EXTENDED_DATA received
      Specified by:
      handleExtendedData in interface Channel
      Parameters:
      buffer - The rest of the message data Buffer after decoding the channel identifiers
      Throws:
      IOException - If failed to handle the message
    • validateIncomingDataSize

      protected long validateIncomingDataSize(int cmd, long len) throws IOException
      Throws:
      IOException
    • getPacketValidator

      public AbstractChannel.PacketValidator getPacketValidator()
      Retrieves the currently set AbstractChannel.PacketValidator.
      Returns:
      the validator, never null
    • setPacketValidator

      public void setPacketValidator(AbstractChannel.PacketValidator validator)
      Parameters:
      validator - the validator to set, if null the DEFAULT_PACKET_VALIDATOR is set
    • handleEof

      public void handleEof() throws IOException
      Description copied from interface: Channel
      Invoked when SSH_MSG_CHANNEL_EOF received
      Specified by:
      handleEof in interface Channel
      Throws:
      IOException - If failed to handle the message
    • isEofSignalled

      public boolean isEofSignalled()
      Specified by:
      isEofSignalled in interface Channel
      Returns:
      true if the peer signaled that it will not send any more data
      See Also:
    • handleWindowAdjust

      public void handleWindowAdjust(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Description copied from interface: Channel
      Invoked when SSH_MSG_CHANNEL_WINDOW_ADJUST received
      Specified by:
      handleWindowAdjust in interface Channel
      Parameters:
      buffer - The rest of the message data Buffer after decoding the channel identifiers
      Throws:
      IOException - If failed to handle the message
    • handleSuccess

      public void handleSuccess() throws IOException
      Description copied from interface: Channel
      Invoked when SSH_MSG_CHANNEL_SUCCESS received
      Specified by:
      handleSuccess in interface Channel
      Throws:
      IOException - If failed to handle the message
    • handleFailure

      public void handleFailure() throws IOException
      Description copied from interface: Channel
      Invoked when SSH_MSG_CHANNEL_FAILURE received
      Specified by:
      handleFailure in interface Channel
      Throws:
      IOException - If failed to handle the message
    • doWriteData

      protected abstract void doWriteData(byte[] data, int off, long len) throws IOException
      Throws:
      IOException
    • doWriteExtendedData

      protected abstract void doWriteExtendedData(byte[] data, int off, long len) throws IOException
      Throws:
      IOException
    • sendEof

      protected org.apache.sshd.common.io.IoWriteFuture sendEof() throws IOException
      Sends SSH_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

      public Map<String,Object> getProperties()
      Specified by:
      getProperties in interface org.apache.sshd.common.PropertyResolver
    • getAttributesCount

      public int getAttributesCount()
      Specified by:
      getAttributesCount in interface org.apache.sshd.common.AttributeRepository
    • getAttribute

      public <T> T getAttribute(org.apache.sshd.common.AttributeRepository.AttributeKey<T> key)
      Specified by:
      getAttribute in interface org.apache.sshd.common.AttributeRepository
    • attributeKeys

      public Collection<org.apache.sshd.common.AttributeRepository.AttributeKey<?>> attributeKeys()
      Specified by:
      attributeKeys in interface org.apache.sshd.common.AttributeRepository
    • computeAttributeIfAbsent

      public <T> T computeAttributeIfAbsent(org.apache.sshd.common.AttributeRepository.AttributeKey<T> key, Function<? super org.apache.sshd.common.AttributeRepository.AttributeKey<T>, ? extends T> resolver)
      Specified by:
      computeAttributeIfAbsent in interface org.apache.sshd.common.AttributeStore
    • setAttribute

      public <T> T setAttribute(org.apache.sshd.common.AttributeRepository.AttributeKey<T> key, T value)
      Specified by:
      setAttribute in interface org.apache.sshd.common.AttributeStore
    • removeAttribute

      public <T> T removeAttribute(org.apache.sshd.common.AttributeRepository.AttributeKey<T> key)
      Specified by:
      removeAttribute in interface org.apache.sshd.common.AttributeStore
    • clearAttributes

      public void clearAttributes()
      Specified by:
      clearAttributes in interface org.apache.sshd.common.AttributeStore
    • configureWindow

      protected void configureWindow()
    • sendWindowAdjust

      protected void sendWindowAdjust(long len) throws IOException
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object