Class AbstractConnectionService

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.session.helpers.AbstractConnectionService
All Implemented Interfaces:
Closeable, AutoCloseable, Channel, EventListener, org.apache.sshd.common.Closeable, PortForwardingEventListenerManager, PortForwardingEventListenerManagerHolder, org.apache.sshd.common.PropertyResolver, Service, ConnectionService, ReservedSessionMessagesHandler, org.apache.sshd.common.session.SessionContextHolder, org.apache.sshd.common.session.SessionHeartbeatController, SessionHolder<Session>, UnknownChannelReferenceHandlerManager, org.apache.sshd.common.util.SshdEventListener
Direct Known Subclasses:
ClientConnectionService, ServerConnectionService

public abstract class AbstractConnectionService extends org.apache.sshd.common.util.closeable.AbstractInnerCloseable implements ConnectionService, ReservedSessionMessagesHandler
Base implementation of ConnectionService.
Author:
Apache MINA SSHD Project
  • Field Details

    • RESPONSE_BUFFER_GROWTH_FACTOR

      public static final IntUnaryOperator RESPONSE_BUFFER_GROWTH_FACTOR
      Default growth factor function used to resize response buffers
    • DEFAULT_SESSION_IGNORE_HEARTBEAT_STRING

      public static final String DEFAULT_SESSION_IGNORE_HEARTBEAT_STRING
      Used in SSH_MSH_IGNORE messages for the keep-alive mechanism
      See Also:
    • channels

      protected final Map<Long,Channel> channels
      Map of channels keyed by the identifier
    • nextChannelId

      protected final AtomicLong nextChannelId
      Next channel identifier - a UINT32 represented as a long
    • heartbeatCount

      protected final AtomicLong heartbeatCount
  • Constructor Details

    • AbstractConnectionService

      protected AbstractConnectionService(AbstractSession session)
  • Method Details

    • getProperties

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

      public PortForwardingEventListener getPortForwardingEventListenerProxy()
      Specified by:
      getPortForwardingEventListenerProxy in interface PortForwardingEventListenerManager
      Returns:
      A proxy listener representing all the currently registered listeners in this manager
    • addPortForwardingEventListener

      public void addPortForwardingEventListener(PortForwardingEventListener listener)
      Description copied from interface: PortForwardingEventListenerManager
      Add a port forwarding listener
      Specified by:
      addPortForwardingEventListener in interface PortForwardingEventListenerManager
      Parameters:
      listener - The PortForwardingEventListener to add - never null
    • removePortForwardingEventListener

      public void removePortForwardingEventListener(PortForwardingEventListener listener)
      Description copied from interface: PortForwardingEventListenerManager
      Remove a port forwarding listener
      Specified by:
      removePortForwardingEventListener in interface PortForwardingEventListenerManager
      Parameters:
      listener - The PortForwardingEventListener to remove - ignored if null
    • getUnknownChannelReferenceHandler

      public UnknownChannelReferenceHandler getUnknownChannelReferenceHandler()
      Specified by:
      getUnknownChannelReferenceHandler in interface UnknownChannelReferenceHandlerManager
      Returns:
      The UnknownChannelReferenceHandlerManager to use - if null then any reference to unknown channel causes an SshChannelNotFoundException
    • setUnknownChannelReferenceHandler

      public void setUnknownChannelReferenceHandler(UnknownChannelReferenceHandler handler)
      Specified by:
      setUnknownChannelReferenceHandler in interface UnknownChannelReferenceHandlerManager
      Parameters:
      handler - The UnknownChannelReferenceHandlerManager to use - if null then any reference to unknown channel causes an SshChannelNotFoundException
    • getRegisteredManagers

      public Collection<PortForwardingEventListenerManager> getRegisteredManagers()
      Specified by:
      getRegisteredManagers in interface PortForwardingEventListenerManagerHolder
      Returns:
      The currently registered managers. Note: it is highly recommended that implementors return either an un-modifiable collection or a copy of the current one. Callers, should avoid modifying the retrieved value.
    • addPortForwardingEventListenerManager

      public boolean addPortForwardingEventListenerManager(PortForwardingEventListenerManager manager)
      Specified by:
      addPortForwardingEventListenerManager in interface PortForwardingEventListenerManagerHolder
    • removePortForwardingEventListenerManager

      public boolean removePortForwardingEventListenerManager(PortForwardingEventListenerManager manager)
      Specified by:
      removePortForwardingEventListenerManager in interface PortForwardingEventListenerManagerHolder
    • getChannels

      public Collection<Channel> getChannels()
    • getSession

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

      public void start()
      Specified by:
      start in interface Service
    • startHeartBeat

      protected ScheduledFuture<?> startHeartBeat()
    • sendHeartBeat

      protected boolean sendHeartBeat()
      Sends a heartbeat message/packet
      Returns:
      true if heartbeat successfully sent
    • futureDone

      protected void futureDone(org.apache.sshd.common.future.HasException future)
    • stopHeartBeat

      protected void stopHeartBeat()
    • getForwarder

      public Forwarder getForwarder()
      Description copied from interface: ConnectionService
      Retrieve the forwarder instance
      Specified by:
      getForwarder in interface ConnectionService
      Returns:
      The Forwarder
    • request

      public org.apache.sshd.common.util.buffer.Buffer request(String request, org.apache.sshd.common.util.buffer.Buffer buffer, long maxWaitMillis) throws IOException
      Description copied from interface: ConnectionService
      Send a global request and wait for the response, if the request is sent with want-reply = true.
      Specified by:
      request in interface ConnectionService
      Parameters:
      request - the request name - used mainly for logging and debugging
      buffer - the buffer containing the global request
      maxWaitMillis - maximum time in milliseconds to wait for the request to finish - must be positive
      Returns:
      the return buffer if the request was successful, null otherwise.
      Throws:
      IOException - if an error occurred when encoding or sending the packet
    • request

      public GlobalRequestFuture request(org.apache.sshd.common.util.buffer.Buffer buffer, String request, GlobalRequestFuture.ReplyHandler replyHandler) throws IOException
      Description copied from interface: ConnectionService
      Send a global request and handle the reply asynchronously. If want-reply = true, pass the received Buffer to the given GlobalRequestFuture.ReplyHandler, which may execute in a different thread.
      want-reply == true && replyHandler != null
      The returned future is fulfilled with null when the request was sent, or with an exception if the request could not be sent. The replyHandler is invoked once the reply is received, with the SSH reply code and the data received.
      want-reply == true && replyHandler == null
      The returned future is fulfilled with an exception if the request could not be sent, or a failure reply was received. If a success reply was received, the future is fulfilled with the received data buffer.
      want-reply == false
      The returned future is fulfilled with an empty Buffer when the request was sent, or with an exception if the request could not be sent. If a reply handler is given, it is invoked with that empty buffer. The handler is not invoked if sending the request failed.
      Specified by:
      request in interface ConnectionService
      Parameters:
      buffer - the Buffer containing the global request, with the want-reply flag set as appropriate
      request - the request name
      replyHandler - GlobalRequestFuture.ReplyHandler for handling the reply; may be null
      Returns:
      Created GlobalRequestFuture
      Throws:
      IOException - if an error occurred while encoding or sending the packet
    • handleUnimplementedMessage

      public boolean handleUnimplementedMessage(Session session, int cmd, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Description copied from interface: ReservedSessionMessagesHandler
      Invoked when a packet with an un-implemented message is received - including SSH_MSG_UNIMPLEMENTED itself
      Specified by:
      handleUnimplementedMessage in interface ReservedSessionMessagesHandler
      Parameters:
      session - The Session through which the message was received
      cmd - The received (un-implemented) command
      buffer - The Buffer containing the data - positioned just beyond the command
      Returns:
      true if message handled internally, false if should return a SSH_MSG_UNIMPLEMENTED reply (default behavior)
      Throws:
      Exception - If failed to handle the message
      See Also:
    • preClose

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

      protected Forwarder createForwardingFilter(Session session)
    • getX11ForwardSupport

      public X11ForwardSupport getX11ForwardSupport()
      Specified by:
      getX11ForwardSupport in interface ConnectionService
    • createX11ForwardSupport

      protected X11ForwardSupport createX11ForwardSupport(Session session)
    • getAgentForwardSupport

      public AgentForwardSupport getAgentForwardSupport()
      Specified by:
      getAgentForwardSupport in interface ConnectionService
    • createAgentForwardSupport

      protected AgentForwardSupport createAgentForwardSupport(Session session)
    • getInnerCloseable

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

      protected long getNextChannelId()
    • registerChannel

      public long registerChannel(Channel channel) throws IOException
      Description copied from interface: ConnectionService
      Register a newly created channel with a new unique identifier
      Specified by:
      registerChannel in interface ConnectionService
      Parameters:
      channel - The Channel to register
      Returns:
      The assigned id of this channel - a UINT32 represented as a long
      Throws:
      IOException - If failed to initialize and register the channel
    • unregisterChannel

      public void unregisterChannel(Channel channel)
      Remove this channel from the list of managed channels
      Specified by:
      unregisterChannel in interface ConnectionService
      Parameters:
      channel - the channel
    • process

      public void process(int cmd, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Description copied from interface: Service
      Service the request.
      Specified by:
      process in interface Service
      Parameters:
      cmd - The incoming command type
      buffer - The Buffer containing optional command parameters
      Throws:
      Exception - If failed to process the command
    • isAllowMoreSessions

      public boolean isAllowMoreSessions()
      Specified by:
      isAllowMoreSessions in interface ConnectionService
    • setAllowMoreSessions

      public void setAllowMoreSessions(boolean allow)
      Specified by:
      setAllowMoreSessions in interface ConnectionService
    • channelOpenConfirmation

      public void channelOpenConfirmation(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Throws:
      IOException
    • channelOpenFailure

      public void channelOpenFailure(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Throws:
      IOException
    • channelData

      public void channelData(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Process incoming data on a channel
      Parameters:
      buffer - the buffer containing the data
      Throws:
      IOException - if an error occurs
    • channelExtendedData

      public void channelExtendedData(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Process incoming extended data on a channel
      Parameters:
      buffer - the buffer containing the data
      Throws:
      IOException - if an error occurs
    • channelWindowAdjust

      public void channelWindowAdjust(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Process a window adjust packet on a channel
      Parameters:
      buffer - the buffer containing the window adjustment parameters
      Throws:
      IOException - if an error occurs
    • channelEof

      public void channelEof(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Process end of file on a channel
      Parameters:
      buffer - the buffer containing the packet
      Throws:
      IOException - if an error occurs
    • channelClose

      public void channelClose(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Close a channel due to a close packet received
      Parameters:
      buffer - the buffer containing the packet
      Throws:
      IOException - if an error occurs
    • channelRequest

      public void channelRequest(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Service a request on a channel
      Parameters:
      buffer - the buffer containing the request
      Throws:
      IOException - if an error occurs
    • channelFailure

      public void channelFailure(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Process a failure on a channel
      Parameters:
      buffer - the buffer containing the packet
      Throws:
      IOException - if an error occurs
    • channelSuccess

      public void channelSuccess(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Process a success on a channel
      Parameters:
      buffer - the buffer containing the packet
      Throws:
      IOException - if an error occurs
    • getChannel

      protected Channel getChannel(byte cmd, org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Retrieve the channel designated by the given packet
      Parameters:
      cmd - The command being processed for the channel
      buffer - the incoming packet
      Returns:
      the target channel
      Throws:
      IOException - if the channel does not exists
    • getChannel

      protected Channel getChannel(byte cmd, long recipient, org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Throws:
      IOException
    • resolveUnknownChannelReferenceHandler

      public UnknownChannelReferenceHandler resolveUnknownChannelReferenceHandler()
      Description copied from interface: UnknownChannelReferenceHandlerManager
      Check if current manager has a specific handler set for it - if not, try and resolve one from the "parent" container (if any)
      Specified by:
      resolveUnknownChannelReferenceHandler in interface UnknownChannelReferenceHandlerManager
      Returns:
      The resolved handler instance
    • channelOpen

      protected void channelOpen(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • sendChannelOpenFailure

      protected org.apache.sshd.common.io.IoWriteFuture sendChannelOpenFailure(org.apache.sshd.common.util.buffer.Buffer buffer, long sender, int reasonCode, String message, String lang) throws IOException
      Throws:
      IOException
    • globalRequest

      protected org.apache.sshd.common.io.IoWriteFuture globalRequest(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Process global requests
      Parameters:
      buffer - The request Buffer
      Returns:
      An IoWriteFuture representing the sent packet - Note: if no reply sent then an "empty" future is returned - i.e., any added listeners are triggered immediately with a synthetic "success"
      Throws:
      Exception - If failed to process the request
    • handleUnknownRequest

      protected org.apache.sshd.common.io.IoWriteFuture handleUnknownRequest(org.apache.sshd.common.util.buffer.Buffer buffer, String req, boolean wantReply) throws IOException
      Throws:
      IOException
    • sendGlobalResponse

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

      protected void requestSuccess(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Indicates the reception of a SSH_MSG_REQUEST_SUCCESS message
      Parameters:
      buffer - The Buffer containing the message data
      Throws:
      Exception - If failed to handle the message
    • requestFailure

      protected void requestFailure(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Indicates the reception of a SSH_MSG_REQUEST_FAILURE message
      Parameters:
      buffer - The Buffer containing the message data
      Throws:
      Exception - If failed to handle the message
    • toString

      public String toString()
      Overrides:
      toString in class Object