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:
java.io.Closeable
,java.lang.AutoCloseable
,java.nio.channels.Channel
,Closeable
,IoSession
,PacketWriter
,ConnectionEndpointsIndicator
public class Nio2Session extends AbstractCloseable implements IoSession
-
-
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 private java.net.SocketAddress
acceptanceAddress
private java.util.Map<java.lang.Object,java.lang.Object>
attributes
private java.util.concurrent.atomic.AtomicReference<Nio2DefaultIoWriteFuture>
currentWrite
static int
DEFAULT_READBUF_SIZE
private long
id
private IoHandler
ioHandler
private java.net.SocketAddress
localAddress
private FactoryManager
manager
private java.net.SocketAddress
remoteAddress
private Nio2Service
service
private static java.util.concurrent.atomic.AtomicLong
SESSION_ID_GENERATOR
private java.nio.channels.AsynchronousSocketChannel
socketChannel
private java.util.Queue<Nio2DefaultIoWriteFuture>
writes
-
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, java.nio.channels.AsynchronousSocketChannel socket, java.net.SocketAddress acceptanceAddress)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Nio2CompletionHandler<java.lang.Integer,java.lang.Object>
createReadCycleCompletionHandler(java.nio.ByteBuffer buffer, Readable bufReader)
protected Nio2CompletionHandler<java.lang.Integer,java.lang.Object>
createWriteCycleCompletionHandler(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer)
protected CloseFuture
doCloseGracefully()
protected void
doCloseImmediately()
doCloseImmediately is called once and only once with state == Immediateprotected void
doReadCycle(java.nio.ByteBuffer buffer, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion)
protected void
doReadCycle(java.nio.ByteBuffer buffer, Readable bufReader)
protected void
doWriteCycle(java.nio.ByteBuffer buffer, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion)
protected void
exceptionCaught(java.lang.Throwable exc)
protected void
finishWrite(Nio2DefaultIoWriteFuture future)
java.net.SocketAddress
getAcceptanceAddress()
java.lang.Object
getAttribute(java.lang.Object key)
Returns the value of the user-defined attribute of this session.long
getId()
IoHandler
getIoHandler()
java.net.SocketAddress
getLocalAddress()
java.net.SocketAddress
getRemoteAddress()
Nio2Service
getService()
java.nio.channels.AsynchronousSocketChannel
getSocket()
protected void
handleCompletedWriteCycle(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer, int writeLen, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completionHandler, java.lang.Integer result, java.lang.Object attachment)
protected void
handleReadCycleCompletion(java.nio.ByteBuffer buffer, Readable bufReader, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completionHandler, java.lang.Integer result, java.lang.Object attachment)
protected void
handleReadCycleFailure(java.nio.ByteBuffer buffer, Readable bufReader, java.lang.Throwable exc, java.lang.Object attachment)
protected void
handleWriteCycleFailure(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer, int writeLen, java.lang.Throwable exc, java.lang.Object attachment)
java.lang.Object
removeAttribute(java.lang.Object key)
Removes a user-defined attribute with the specified key.java.lang.Object
setAttribute(java.lang.Object key, java.lang.Object value)
Sets a user-defined attribute.java.lang.Object
setAttributeIfAbsent(java.lang.Object key, java.lang.Object value)
Sets a user defined attribute if the attribute with the specified key is not set yet.void
shudownOutputStream()
Handle received EOF.void
startReading()
void
startReading(byte[] buf)
void
startReading(byte[] buf, int offset, int len)
void
startReading(int bufSize)
void
startReading(java.nio.ByteBuffer buffer)
protected void
startWriting()
void
suspend()
java.lang.String
toString()
IoWriteFuture
writePacket(Buffer buffer)
Encode and send the given buffer.-
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
-
SESSION_ID_GENERATOR
private static final java.util.concurrent.atomic.AtomicLong SESSION_ID_GENERATOR
-
id
private final long id
-
service
private final Nio2Service service
-
ioHandler
private final IoHandler ioHandler
-
socketChannel
private final java.nio.channels.AsynchronousSocketChannel socketChannel
-
attributes
private final java.util.Map<java.lang.Object,java.lang.Object> attributes
-
localAddress
private final java.net.SocketAddress localAddress
-
remoteAddress
private final java.net.SocketAddress remoteAddress
-
acceptanceAddress
private final java.net.SocketAddress acceptanceAddress
-
manager
private final FactoryManager manager
-
writes
private final java.util.Queue<Nio2DefaultIoWriteFuture> writes
-
currentWrite
private final java.util.concurrent.atomic.AtomicReference<Nio2DefaultIoWriteFuture> currentWrite
-
-
Constructor Detail
-
Nio2Session
public Nio2Session(Nio2Service service, FactoryManager manager, IoHandler handler, java.nio.channels.AsynchronousSocketChannel socket, java.net.SocketAddress acceptanceAddress) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
getId
public long getId()
-
getAttribute
public java.lang.Object getAttribute(java.lang.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 java.lang.Object setAttribute(java.lang.Object key, java.lang.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 java.lang.Object setAttributeIfAbsent(java.lang.Object key, java.lang.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 java.lang.Object removeAttribute(java.lang.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 java.net.SocketAddress getRemoteAddress()
- Specified by:
getRemoteAddress
in interfaceConnectionEndpointsIndicator
- Returns:
- the socket address of remote peer.
-
getLocalAddress
public java.net.SocketAddress getLocalAddress()
- Specified by:
getLocalAddress
in interfaceConnectionEndpointsIndicator
- Returns:
- the socket address of local machine which is associated with this session.
-
getAcceptanceAddress
public java.net.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 java.nio.channels.AsynchronousSocketChannel getSocket()
-
getIoHandler
public IoHandler getIoHandler()
-
suspend
public void suspend()
-
writePacket
public IoWriteFuture writePacket(Buffer buffer) throws java.io.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:
java.io.IOException
- if an error occurred when encoding sending the packet
-
exceptionCaught
protected void exceptionCaught(java.lang.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 java.io.IOException
Description copied from interface:IoSession
Handle received EOF.- Specified by:
shudownOutputStream
in interfaceIoSession
- Throws:
java.io.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(java.nio.ByteBuffer buffer)
-
doReadCycle
protected void doReadCycle(java.nio.ByteBuffer buffer, Readable bufReader)
-
createReadCycleCompletionHandler
protected Nio2CompletionHandler<java.lang.Integer,java.lang.Object> createReadCycleCompletionHandler(java.nio.ByteBuffer buffer, Readable bufReader)
-
handleReadCycleCompletion
protected void handleReadCycleCompletion(java.nio.ByteBuffer buffer, Readable bufReader, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completionHandler, java.lang.Integer result, java.lang.Object attachment)
-
handleReadCycleFailure
protected void handleReadCycleFailure(java.nio.ByteBuffer buffer, Readable bufReader, java.lang.Throwable exc, java.lang.Object attachment)
-
doReadCycle
protected void doReadCycle(java.nio.ByteBuffer buffer, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion)
-
startWriting
protected void startWriting()
-
doWriteCycle
protected void doWriteCycle(java.nio.ByteBuffer buffer, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion)
-
createWriteCycleCompletionHandler
protected Nio2CompletionHandler<java.lang.Integer,java.lang.Object> createWriteCycleCompletionHandler(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer)
-
handleCompletedWriteCycle
protected void handleCompletedWriteCycle(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer, int writeLen, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completionHandler, java.lang.Integer result, java.lang.Object attachment)
-
handleWriteCycleFailure
protected void handleWriteCycleFailure(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer, int writeLen, java.lang.Throwable exc, java.lang.Object attachment)
-
finishWrite
protected void finishWrite(Nio2DefaultIoWriteFuture future)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-