Package com.sshtools.synergy.ssh
Class ChannelNG<T extends SshContext>
java.lang.Object
com.sshtools.synergy.ssh.ChannelNG<T>
- All Implemented Interfaces:
Channel
- Direct Known Subclasses:
ForwardingChannel
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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CachingDataWindowprotected SshConnectionprotected ConnectionProtocol<T>The Connection Protocol instance managing this session, use this instance to disconnect the session or obtain the ip address of the remote client.protected ChannelDataWindowprotected ChannelDataWindow -
Constructor Summary
ConstructorsConstructorDescriptionChannelNG(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(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(String channelType, int maximumPacketSize, com.sshtools.common.util.UnsignedInteger32 initialWindowSize, com.sshtools.common.util.UnsignedInteger32 maximumWindowSpace, com.sshtools.common.util.UnsignedInteger32 minimumWindowSpace) 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. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEventListener(ChannelEventListener listener) Enable other objects to receive channel eventsprotected booleancanClose()Can this channel close?protected booleanvoidclearIdleState(IdleStateListener listener) Clear an idle state listener.voidclose()protected voidclose(boolean forceClose) This method closes the channel and free's its resources.voidprotected CachingDataWindowcreateCache(int maximumWindowSpace) protected abstract byte[]Called when the channel is being created.protected voiddisposeCache(CachingDataWindow cachingWindow) protected voidCalled to evaluate the window space available.The name of this channel.Get this channels close future.Returns theConnectionProtocolassociated with this channel.Get the current configuration from the underlying connection.longintThe local channel idintThe maximum size of a single packet that the local side will accept.com.sshtools.common.util.UnsignedInteger32The current size of the local data window.intcom.sshtools.common.util.UnsignedInteger32Get this channels close future.intThe remote sides channel idintThe maximum size of a single packet that the remote side will accept.com.sshtools.common.util.UnsignedInteger32The current size of the remote data window.Get the session id for the current connection.intvoidfinal booleanbooleanisClosed()Indicates the channel has been closedbooleanIndicates whether the channel is currently performing a close operationbooleanbooleanbooleanprotected booleanisOpen()Get the open state of the channel.booleanprotected voidprotected voidprotected voidprotected voidprotected voidlogMessage(String message) protected voidlogMessage(String message, long remoteWindow) protected abstract voidCalled when the channel has been closed to enable resources to be freed.protected abstract voidCalled before the channel is closedprotected voidonChannelData(ByteBuffer data) protected voidprotected abstract voidThis method is called when the channel has been closed and all its resources are no longer required.protected abstract voidCalled when the channel has been opened - this method is only called when the remote side requests a channel open.protected abstract voidCalled 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 connectionprotected voidCalled when the remote side fails to open a channel in response to our request.protected abstract voidonChannelRequest(String type, boolean wantreply, byte[] requestdata) Called when a channel request is received.protected voidonExtendedData(ByteBuffer data, int type) Called by the channel when extended data arrivesprotected abstract voidCalled when the local side is EOF.protected voidCalled when the remote side closes the channel.protected abstract voidCalled when the remote side reports its OutputStream as EOF.protected voidonWindowAdjust(com.sshtools.common.util.UnsignedInteger32 count) protected abstract byte[]openChannel(byte[] requestdata) Called when the channel is being opened.protected voidprocessChannelRequestResponse(boolean success) protected voidvoidresetIdleState(IdleStateListener listener) Allows a channel to register and receive idle state events.voidvoidsendChannelDataAndBlock(byte[] data) Send channel data to the remote side of the channel.voidsendChannelDataAndBlock(byte[] data, int off, int len, Runnable r) voidsendChannelDataAndBlock(byte[] data, Runnable r) voidSend channel data from a ByteBuffervoidsendChannelDataAndBlock(ByteBuffer buf, int type, Runnable r) Send channel data from a ByteBuffervoidvoidsendChannelRequest(String type, boolean wantreply, byte[] requestdata) voidsendChannelRequest(String type, boolean wantreply, byte[] requestdata, ChannelRequestFuture future) Send a channel request.voidsendData(byte[] data, int off, int len) voidsendEOF()You can send EOF when you have no more data to send.protected voidsendExtendedData(byte[] data, int type) Send extended channel data.protected voidsendExtendedData(byte[] data, int off, int len, int type) Send extended channel data.protected voidsendRequestResponse(boolean succeeded) Send a channel request.protected voidAdjust the local window by adding more bytes.voidsendWindowAdjust(com.sshtools.common.util.UnsignedInteger32 count) voidsetTimeout(int timeout)
-
Field Details
-
connection
The Connection Protocol instance managing this session, use this instance to disconnect the session or obtain the ip address of the remote client. -
localWindow
-
remoteWindow
-
cache
-
con
-
-
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 channelinitialWindowSize- 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
-
disposeCache
-
isAutoConsume
public final boolean isAutoConsume() -
getInputStream
-
getOutputStream
-
isClosed
public boolean isClosed()Indicates the channel has been closed -
isConnected
public boolean isConnected() -
getMaxiumRemoteWindowSize
public com.sshtools.common.util.UnsignedInteger32 getMaxiumRemoteWindowSize() -
getMaxiumRemotePacketSize
public int getMaxiumRemotePacketSize() -
resetIdleState
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). TheIdleStateListenerinstance 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
Clear an idle state listener.- Parameters:
listener-
-
addEventListener
Enable other objects to receive channel events- Specified by:
addEventListenerin interfaceChannel- Parameters:
listener-
-
getChannelType
The name of this channel.- Specified by:
getChannelTypein interfaceChannel- Returns:
- String
-
getOpenFuture
Get this channels close future.- Returns:
-
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:
getRemoteWindowin interfaceChannel- Returns:
- int
-
getLocalWindow
public com.sshtools.common.util.UnsignedInteger32 getLocalWindow()The current size of the local data window.- Specified by:
getLocalWindowin interfaceChannel- Returns:
- int
-
getLocalPacket
public int getLocalPacket()The maximum size of a single packet that the local side will accept.- Specified by:
getLocalPacketin interfaceChannel- 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
Get the session id for the current connection.- Returns:
- byte[]
-
registerExtendedDataType
-
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
- Specified by:
getConnectionin interfaceChannel
-
getConnectionProtocol
Returns theConnectionProtocolassociated with this channel.- Returns:
- ConnectionProtocol
-
onChannelData
-
sendChannelDataAndBlock
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
- Throws:
IOException
-
sendData
- Specified by:
sendDatain interfaceChannel- Throws:
IOException
-
sendChannelDataAndBlock
- Throws:
IOException
-
sendChannelDataAndBlock
Send channel data from a ByteBuffer- Parameters:
buf-- Throws:
IOException
-
sendChannelDataAndBlock
- Throws:
IOException
-
sendChannelDataAndBlock
Send channel data from a ByteBuffer- Parameters:
buf-r-- Throws:
IOException
-
getContext
Get the current configuration from the underlying connection.- Specified by:
getContextin interfaceChannel- Returns:
- ConfigurationContext
-
sendExtendedData
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
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
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:
sendChannelRequestin interfaceChannel- Parameters:
type-wantreply-requestdata-
-
sendChannelRequest
- Specified by:
sendChannelRequestin interfaceChannel
-
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() -
close
-
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
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:
WriteOperationRequestChannelOpenException
-
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
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:
sendWindowAdjustin interfaceChannel
-
logMessage
-
logMessage
-
log
-
log
-
log
-
log
-
isLocalEOF
public boolean isLocalEOF()- Specified by:
isLocalEOFin interfaceChannel
-
isRemoteEOF
public boolean isRemoteEOF()- Specified by:
isRemoteEOFin interfaceChannel
-
isIncomingDataHalted
public boolean isIncomingDataHalted() -
haltIncomingData
public void haltIncomingData() -
resumeIncomingData
public void resumeIncomingData() -
checkWindowSpace
protected boolean checkWindowSpace() -
onChannelError
-