Class ChannelNG<T extends SshContext>

java.lang.Object
com.sshtools.synergy.ssh.ChannelNG<T>
All Implemented Interfaces:
Channel
Direct Known Subclasses:
ForwardingChannel

public abstract class ChannelNG<T extends SshContext> extends Object implements Channel
This abstract class provides the basic functions of an SSH2 channel. All terminal sessions, forwarded connections etc, are channels. Either side may open a channel and multiple channels are multiplexed into a single connection. Channels are flow-controlled so that no data may be sent to a channel until a message is received to indicate that a window space is available.
  • Field Details

  • Constructor Details

    • ChannelNG

      @Deprecated(forRemoval=true, since="3.1.0") public ChannelNG(String channelType, int maximumPacketSize, int initialWindowSize, int maximumWindowSpace, int minimumWindowSpace, ChannelRequestFuture closeFuture, boolean autoConsume)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • ChannelNG

      public ChannelNG(String channelType, int maximumPacketSize, com.sshtools.common.util.UnsignedInteger32 initialWindowSize, com.sshtools.common.util.UnsignedInteger32 maximumWindowSpace, com.sshtools.common.util.UnsignedInteger32 minimumWindowSpace, ChannelRequestFuture closeFuture, boolean autoConsume)
      Construct a channel with the specified settings.
      Parameters:
      channelType - the name of the channel, for example "session" or "tcpip-forward"
      maximumPacketSize - the maximum size of an individual packet that the remote side can send through the channel
      initialWindowSize - the initial size of the local window.
    • ChannelNG

      @Deprecated(forRemoval=true, since="3.1.0") public ChannelNG(String channelType, int maximumPacketSize, int initialWindowSize, int maximumWindowSpace, int minimumWindowSpace)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • ChannelNG

      public ChannelNG(String channelType, int maximumPacketSize, com.sshtools.common.util.UnsignedInteger32 initialWindowSize, com.sshtools.common.util.UnsignedInteger32 maximumWindowSpace, com.sshtools.common.util.UnsignedInteger32 minimumWindowSpace)
  • Method Details

    • createCache

      protected CachingDataWindow createCache(int maximumWindowSpace)
    • disposeCache

      protected void disposeCache(CachingDataWindow cachingWindow)
    • isAutoConsume

      public final boolean isAutoConsume()
    • getInputStream

      public InputStream getInputStream()
    • getOutputStream

      public OutputStream getOutputStream()
    • isClosed

      public boolean isClosed()
      Indicates the channel has been closed
      Specified by:
      isClosed in interface Channel
      Returns:
      boolean
    • isConnected

      public boolean isConnected()
    • getMaxiumRemoteWindowSize

      public com.sshtools.common.util.UnsignedInteger32 getMaxiumRemoteWindowSize()
    • getMaxiumRemotePacketSize

      public int getMaxiumRemotePacketSize()
    • resetIdleState

      public void resetIdleState(IdleStateListener listener)
      Allows a channel to register and receive idle state events. Call this method to reset the idle state of a channel (i.e when activity has occurred). The IdleStateListener instance passed as a parameter will receive notification once the channel reaches an idle state. NOTE: It is the callers responsibility to call clearIdleState method to ensure references are released.
      Parameters:
      listener - IdleStateListener
    • clearIdleState

      public void clearIdleState(IdleStateListener listener)
      Clear an idle state listener.
      Parameters:
      listener -
    • addEventListener

      public void addEventListener(ChannelEventListener listener)
      Enable other objects to receive channel events
      Specified by:
      addEventListener in interface Channel
      Parameters:
      listener -
    • getChannelType

      public String getChannelType()
      The name of this channel.
      Specified by:
      getChannelType in interface Channel
      Returns:
      String
    • getOpenFuture

      public ChannelRequestFuture getOpenFuture()
      Get this channels close future.
      Returns:
    • getCloseFuture

      public ChannelRequestFuture getCloseFuture()
      Get this channels close future.
      Returns:
    • getRemoteWindow

      public com.sshtools.common.util.UnsignedInteger32 getRemoteWindow()
      The current size of the remote data window.
      Specified by:
      getRemoteWindow in interface Channel
      Returns:
      int
    • getLocalWindow

      public com.sshtools.common.util.UnsignedInteger32 getLocalWindow()
      The current size of the local data window.
      Specified by:
      getLocalWindow in interface Channel
      Returns:
      int
    • getLocalPacket

      public int getLocalPacket()
      The maximum size of a single packet that the local side will accept.
      Specified by:
      getLocalPacket in interface Channel
      Returns:
      int
    • getRemotePacket

      public int getRemotePacket()
      The maximum size of a single packet that the remote side will accept.
      Returns:
      int
    • getLocalId

      public int getLocalId()
      The local channel id
      Returns:
      int
    • getRemoteId

      public int getRemoteId()
      The remote sides channel id
      Returns:
      int
    • getSessionIdentifier

      public String getSessionIdentifier()
      Get the session id for the current connection.
      Returns:
      byte[]
    • registerExtendedDataType

      protected void registerExtendedDataType(Integer type)
    • onWindowAdjust

      protected void onWindowAdjust(com.sshtools.common.util.UnsignedInteger32 count)
    • getLastActivity

      public long getLastActivity()
    • setTimeout

      public void setTimeout(int timeout)
    • getTimeout

      public int getTimeout()
    • getConnection

      public Connection<T> getConnection()
      Specified by:
      getConnection in interface Channel
    • getConnectionProtocol

      public ConnectionProtocol<T> getConnectionProtocol()
      Returns the ConnectionProtocol associated with this channel.
      Returns:
      ConnectionProtocol
    • onChannelData

      protected void onChannelData(ByteBuffer data)
    • sendChannelDataAndBlock

      public void sendChannelDataAndBlock(byte[] data) throws IOException
      Send channel data to the remote side of the channel. The byte array passed into this method will be sent asynchronously so ensure that no other class has access to this after this method returns, failing to do so may cause data corruption.
      Parameters:
      data -
      Throws:
      IOException
    • sendChannelDataAndBlock

      public void sendChannelDataAndBlock(byte[] data, Runnable r) throws IOException
      Throws:
      IOException
    • sendData

      public void sendData(byte[] data, int off, int len) throws IOException
      Specified by:
      sendData in interface Channel
      Throws:
      IOException
    • sendChannelDataAndBlock

      public void sendChannelDataAndBlock(byte[] data, int off, int len, Runnable r) throws IOException
      Throws:
      IOException
    • sendChannelDataAndBlock

      public void sendChannelDataAndBlock(ByteBuffer buf) throws IOException
      Send channel data from a ByteBuffer
      Parameters:
      buf -
      Throws:
      IOException
    • sendChannelDataAndBlock

      public void sendChannelDataAndBlock(ByteBuffer buf, Runnable r) throws IOException
      Throws:
      IOException
    • sendChannelDataAndBlock

      public void sendChannelDataAndBlock(ByteBuffer buf, int type, Runnable r) throws IOException
      Send channel data from a ByteBuffer
      Parameters:
      buf -
      r -
      Throws:
      IOException
    • getContext

      public T getContext()
      Get the current configuration from the underlying connection.
      Specified by:
      getContext in interface Channel
      Returns:
      ConfigurationContext
    • sendExtendedData

      protected void sendExtendedData(byte[] data, int type) throws IOException
      Send extended channel data. This data is sent as an extended 'type' which should be known by the channel at the remote side. For example within a session channel an extended data type is used to transfer stderr data from the server to the client.
      Parameters:
      data -
      type -
      Throws:
      IOException
    • sendExtendedData

      protected void sendExtendedData(byte[] data, int off, int len, int type) throws IOException
      Send extended channel data. This data is sent as an extended 'type' which should be known by the channel at the remote side. For example within a session channel an extended data type is used to transfer stderr data from the server to the client.
      Parameters:
      data -
      off -
      len -
      type -
      Throws:
      IOException
    • onExtendedData

      protected void onExtendedData(ByteBuffer data, int type)
      Called by the channel when extended data arrives
      Parameters:
      data -
    • sendChannelRequest

      public void sendChannelRequest(String type, boolean wantreply, byte[] requestdata, ChannelRequestFuture future)

      Send a channel request. Many channel types have extensions that are specific to that particular channel type. An example is requresting a pty (pseudo terminal) for an interactive session. This method enables the sending of channel requests but does not support receiving responses. This should not present a problem as server implementations tend to send messages as one way information, for example the exit-status request of an interactive session.

      To handle requests from a client implement onChannelRequest(String,boolean,byte[]).

      Specified by:
      sendChannelRequest in interface Channel
      Parameters:
      type -
      wantreply -
      requestdata -
    • sendChannelRequest

      public void sendChannelRequest(String type, boolean wantreply, byte[] requestdata)
      Specified by:
      sendChannelRequest in interface Channel
    • processChannelRequestResponse

      protected void processChannelRequestResponse(boolean success)
    • onChannelOpenFailure

      protected void onChannelOpenFailure()
      Called when the remote side fails to open a channel in response to our request. It only makes sense for some channel types to override this method. A local forwarding channel will never receive this message for example.
    • onRemoteClose

      protected void onRemoteClose()
      Called when the remote side closes the channel. Override this method to change the default behaviour of instantly closing the channel unless there is buffered data remaining
    • isClosing

      public boolean isClosing()
      Indicates whether the channel is currently performing a close operation
      Returns:
      boolean
    • close

      public void close()
      Specified by:
      close in interface Channel
    • close

      public void close(Throwable closingError)
    • close

      protected void close(boolean forceClose)
      This method closes the channel and free's its resources.
    • onChannelFree

      protected abstract void onChannelFree()
      This method is called when the channel has been closed and all its resources are no longer required.
    • createChannel

      protected abstract byte[] createChannel() throws IOException
      Called when the channel is being created. You can return data to be sent in the channel open request, or null for none.
      Returns:
      byte[]
      Throws:
      IOException
    • openChannel

      protected abstract byte[] openChannel(byte[] requestdata) throws WriteOperationRequest, ChannelOpenException
      Called when the channel is being opened. You can retrun data to be sent in the channel open confirmation message, or null for none.
      Parameters:
      requestdata -
      Returns:
      byte[]
      Throws:
      WriteOperationRequest
      ChannelOpenException
    • onChannelOpenConfirmation

      protected abstract void onChannelOpenConfirmation()
      Called when the channel has been confirmed as open by the remote side - this method is only called when the channel has been requested by this side of the connection
    • onChannelClosed

      protected abstract void onChannelClosed()
      Called when the channel has been closed to enable resources to be freed.
    • onChannelOpen

      protected abstract void onChannelOpen()
      Called when the channel has been opened - this method is only called when the remote side requests a channel open.
    • onChannelClosing

      protected abstract void onChannelClosing()
      Called before the channel is closed
    • onChannelRequest

      protected abstract void onChannelRequest(String type, boolean wantreply, byte[] requestdata)
      Called when a channel request is received.
      Parameters:
      type -
      wantreply -
      requestdata -
    • evaluateWindowSpace

      protected void evaluateWindowSpace()
      Called to evaluate the window space available. Send a window adjust message if there if the minimum amount of space is not available.
      Parameters:
      remaining -
    • onRemoteEOF

      protected abstract void onRemoteEOF()
      Called when the remote side reports its OutputStream as EOF.
    • sendEOF

      public void sendEOF()
      You can send EOF when you have no more data to send. The channel will still remain open until a close message is received.
    • canClose

      protected boolean canClose()
      Can this channel close?
      Returns:
      true if there is no queued data, else false
    • onLocalEOF

      protected abstract void onLocalEOF()
      Called when the local side is EOF.
    • isOpen

      protected boolean isOpen()
      Get the open state of the channel.
      Returns:
      boolean
    • sendRequestResponse

      protected void sendRequestResponse(boolean succeeded)
      Send a channel request.
      Parameters:
      succeeded -
    • sendWindowAdjust

      protected void sendWindowAdjust()
      Adjust the local window by adding more bytes.
      Parameters:
      count -
    • sendWindowAdjust

      public void sendWindowAdjust(com.sshtools.common.util.UnsignedInteger32 count)
      Specified by:
      sendWindowAdjust in interface Channel
    • logMessage

      protected void logMessage(String message, long remoteWindow)
    • logMessage

      protected void logMessage(String message)
    • log

      protected void log(String action, String message)
    • log

      protected void log(String action, String message, long remoteWindow)
    • log

      protected void log(String message)
    • log

      protected void log(String message, Throwable t)
    • isLocalEOF

      public boolean isLocalEOF()
      Specified by:
      isLocalEOF in interface Channel
    • isRemoteEOF

      public boolean isRemoteEOF()
      Specified by:
      isRemoteEOF in interface Channel
    • isIncomingDataHalted

      public boolean isIncomingDataHalted()
    • haltIncomingData

      public void haltIncomingData()
    • resumeIncomingData

      public void resumeIncomingData()
    • checkWindowSpace

      protected boolean checkWindowSpace()
    • onChannelError

      protected void onChannelError(Throwable e)