Package org.apache.sshd.common.io.nio2
Class Nio2Session
- 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.io.nio2.Nio2Session
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Channel
,Closeable
,IoSession
,PacketWriter
,ConnectionEndpointsIndicator
public class Nio2Session extends AbstractCloseable implements IoSession
- Author:
- Apache MINA SSHD Project
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
AbstractCloseable.State
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_READBUF_SIZE
-
Fields inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
closeFuture, futureLock, state
-
Fields inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
log
-
Fields inherited from interface org.apache.sshd.common.Closeable
CLOSE_WAIT_TIMEOUT, DEFAULT_CLOSE_WAIT_TIMEOUT
-
-
Constructor Summary
Constructors Constructor Description Nio2Session(Nio2Service service, FactoryManager manager, IoHandler handler, AsynchronousSocketChannel socket, SocketAddress acceptanceAddress)
-
Method Summary
-
Methods inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
addCloseFutureListener, builder, close, getFutureLock, isClosed, isClosing, preClose, removeCloseFutureListener
-
Methods inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
getSimplifiedLogger
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.sshd.common.Closeable
addCloseFutureListener, close, close, isClosed, isClosing, isOpen, removeCloseFutureListener
-
-
-
-
Field Detail
-
DEFAULT_READBUF_SIZE
public static final int DEFAULT_READBUF_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Nio2Session
public Nio2Session(Nio2Service service, FactoryManager manager, IoHandler handler, AsynchronousSocketChannel socket, SocketAddress acceptanceAddress) throws IOException
- Throws:
IOException
-
-
Method Detail
-
getId
public long getId()
-
getAttribute
public Object getAttribute(Object key)
Description copied from interface:IoSession
Returns the value of the user-defined attribute of this session.- Specified by:
getAttribute
in interfaceIoSession
- Parameters:
key
- the key of the attribute- Returns:
null
if there is no attribute with the specified key
-
setAttribute
public Object setAttribute(Object key, Object value)
Description copied from interface:IoSession
Sets a user-defined attribute.- Specified by:
setAttribute
in interfaceIoSession
- Parameters:
key
- the key of the attributevalue
- the value of the attribute- Returns:
- The old value of the attribute -
null
if it is new.
-
setAttributeIfAbsent
public Object setAttributeIfAbsent(Object key, Object value)
Description copied from interface:IoSession
Sets a user defined attribute if the attribute with the specified key is not set yet. This method is same with the following code except that the operation is performed atomically.if (containsAttribute(key)) { return getAttribute(key); } else { return setAttribute(key, value); }
- Specified by:
setAttributeIfAbsent
in interfaceIoSession
- Parameters:
key
- The key of the attribute we want to setvalue
- The value we want to set- Returns:
- The old value of the attribute -
null
if not found.
-
removeAttribute
public Object removeAttribute(Object key)
Description copied from interface:IoSession
Removes a user-defined attribute with the specified key.- Specified by:
removeAttribute
in interfaceIoSession
- Parameters:
key
- The key of the attribute we want to remove- Returns:
- The old value of the attribute -
null
if not found.
-
getRemoteAddress
public SocketAddress getRemoteAddress()
- Specified by:
getRemoteAddress
in interfaceConnectionEndpointsIndicator
- Returns:
- the socket address of remote peer.
-
getLocalAddress
public SocketAddress getLocalAddress()
- Specified by:
getLocalAddress
in interfaceConnectionEndpointsIndicator
- Returns:
- the socket address of local machine which is associated with this session.
-
getAcceptanceAddress
public SocketAddress getAcceptanceAddress()
- Specified by:
getAcceptanceAddress
in interfaceIoSession
- Returns:
- The service address through which this session was accepted -
null
if session was initiated by this peer instead of being accepted
-
getSocket
public AsynchronousSocketChannel getSocket()
-
getIoHandler
public IoHandler getIoHandler()
-
suspend
public void suspend()
-
writePacket
public IoWriteFuture writePacket(Buffer buffer) throws IOException
Description copied from interface:PacketWriter
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 interfacePacketWriter
- 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 sending the packet
-
exceptionCaught
protected void exceptionCaught(Throwable exc)
-
doCloseGracefully
protected CloseFuture doCloseGracefully()
- Overrides:
doCloseGracefully
in classAbstractCloseable
-
doCloseImmediately
protected void doCloseImmediately()
Description copied from class:AbstractCloseable
doCloseImmediately is called once and only once with state == Immediate
Overriding methods should always call the base implementation. It may be called concurrently while preClose() or doCloseGracefully is executing
- Overrides:
doCloseImmediately
in classAbstractCloseable
-
getService
public Nio2Service getService()
- Specified by:
getService
in interfaceIoSession
- Returns:
- the
IoService
that created this session.
-
shudownOutputStream
public void shudownOutputStream() throws IOException
Description copied from interface:IoSession
Handle received EOF.- Specified by:
shudownOutputStream
in interfaceIoSession
- Throws:
IOException
- If failed to shutdown the stream
-
startReading
public void startReading()
-
startReading
public void startReading(int bufSize)
-
startReading
public void startReading(byte[] buf)
-
startReading
public void startReading(byte[] buf, int offset, int len)
-
startReading
public void startReading(ByteBuffer buffer)
-
doReadCycle
protected void doReadCycle(ByteBuffer buffer, Readable bufReader)
-
createReadCycleCompletionHandler
protected Nio2CompletionHandler<Integer,Object> createReadCycleCompletionHandler(ByteBuffer buffer, Readable bufReader)
-
handleReadCycleCompletion
protected void handleReadCycleCompletion(ByteBuffer buffer, Readable bufReader, Nio2CompletionHandler<Integer,Object> completionHandler, Integer result, Object attachment)
-
handleReadCycleFailure
protected void handleReadCycleFailure(ByteBuffer buffer, Readable bufReader, Throwable exc, Object attachment)
-
doReadCycle
protected void doReadCycle(ByteBuffer buffer, Nio2CompletionHandler<Integer,Object> completion)
-
startWriting
protected void startWriting()
-
doWriteCycle
protected void doWriteCycle(ByteBuffer buffer, Nio2CompletionHandler<Integer,Object> completion)
-
createWriteCycleCompletionHandler
protected Nio2CompletionHandler<Integer,Object> createWriteCycleCompletionHandler(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket, ByteBuffer buffer)
-
handleCompletedWriteCycle
protected void handleCompletedWriteCycle(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket, ByteBuffer buffer, int writeLen, Nio2CompletionHandler<Integer,Object> completionHandler, Integer result, Object attachment)
-
handleWriteCycleFailure
protected void handleWriteCycleFailure(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket, ByteBuffer buffer, int writeLen, Throwable exc, Object attachment)
-
finishWrite
protected void finishWrite(Nio2DefaultIoWriteFuture future)
-
-