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,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.SocketAddressacceptanceAddressprivate java.util.Map<java.lang.Object,java.lang.Object>attributesprivate java.util.concurrent.atomic.AtomicReference<Nio2DefaultIoWriteFuture>currentWritestatic intDEFAULT_READBUF_SIZEprivate longidprivate IoHandlerioHandlerprivate java.util.concurrent.atomic.AtomicLonglastReadCycleStartprivate java.util.concurrent.atomic.AtomicLonglastWriteCycleStartprivate java.net.SocketAddresslocalAddressprivate java.util.concurrent.atomic.AtomicBooleanoutputShutDownprivate PropertyResolverpropertyResolverprivate java.util.concurrent.atomic.AtomicLongreadCyclesCounterprivate java.lang.ThreadreaderThreadprivate java.lang.RunnablereadRunnableprivate java.net.SocketAddressremoteAddressprivate Nio2Serviceserviceprivate static java.util.concurrent.atomic.AtomicLongSESSION_ID_GENERATORprivate java.nio.channels.AsynchronousSocketChannelsocketChannelprivate booleansuspendprivate java.lang.ObjectsuspendLockprivate java.util.concurrent.atomic.AtomicLongwriteCyclesCounterprivate 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
-
-
Constructor Summary
Constructors Constructor Description Nio2Session(Nio2Service service, PropertyResolver propertyResolver, 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 CloseFuturedoCloseGracefully()protected voiddoCloseImmediately()doCloseImmediately is called once and only once with state == Immediateprotected voiddoReadCycle(java.nio.ByteBuffer buffer, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion)protected voiddoReadCycle(java.nio.ByteBuffer buffer, Readable bufReader)protected voiddoShutdownOutputStream(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket)protected voiddoWriteCycle(java.nio.ByteBuffer buffer, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion)protected voidexceptionCaught(java.lang.Throwable exc)protected voidfinishWrite(Nio2DefaultIoWriteFuture future)java.net.SocketAddressgetAcceptanceAddress()java.lang.ObjectgetAttribute(java.lang.Object key)Returns the value of the user-defined attribute of this session.longgetId()IoHandlergetIoHandler()java.net.SocketAddressgetLocalAddress()java.net.SocketAddressgetRemoteAddress()Nio2ServicegetService()java.nio.channels.AsynchronousSocketChannelgetSocket()protected voidhandleCompletedWriteCycle(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 voidhandleReadCycleCompletion(java.nio.ByteBuffer buffer, Readable bufReader, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completionHandler, java.lang.Integer result, java.lang.Object attachment)protected voidhandleReadCycleFailure(java.nio.ByteBuffer buffer, Readable bufReader, java.lang.Throwable exc, java.lang.Object attachment)protected voidhandleWriteCycleFailure(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer, int writeLen, java.lang.Throwable exc, java.lang.Object attachment)java.lang.ObjectremoveAttribute(java.lang.Object key)Removes a user-defined attribute with the specified key.voidresumeRead()Resume read operations on this session.java.lang.ObjectsetAttribute(java.lang.Object key, java.lang.Object value)Sets a user-defined attribute.java.lang.ObjectsetAttributeIfAbsent(java.lang.Object key, java.lang.Object value)Sets a user defined attribute if the attribute with the specified key is not set yet.voidshutdownOutputStream()Handle received EOF.voidstartReading()voidstartReading(byte[] buf)voidstartReading(byte[] buf, int offset, int len)voidstartReading(int bufSize)voidstartReading(java.nio.ByteBuffer buffer)protected voidstartWriting()voidsuspend()Intended for tests simulating a sudden connection drop only! Do not call otherwise.voidsuspendRead()Suspend read operations on this session.java.lang.StringtoString()IoWriteFuturewriteBuffer(Buffer buffer)Write a packet on the socket.-
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
debug, debug, debug, debug, debug, error, error, error, error, error, getSimplifiedLogger, info, info, warn, warn, warn, warn, warn, warn, warn, warn
-
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, 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
-
propertyResolver
private final PropertyResolver propertyResolver
-
writes
private final java.util.Queue<Nio2DefaultIoWriteFuture> writes
-
currentWrite
private final java.util.concurrent.atomic.AtomicReference<Nio2DefaultIoWriteFuture> currentWrite
-
readCyclesCounter
private final java.util.concurrent.atomic.AtomicLong readCyclesCounter
-
lastReadCycleStart
private final java.util.concurrent.atomic.AtomicLong lastReadCycleStart
-
writeCyclesCounter
private final java.util.concurrent.atomic.AtomicLong writeCyclesCounter
-
lastWriteCycleStart
private final java.util.concurrent.atomic.AtomicLong lastWriteCycleStart
-
outputShutDown
private final java.util.concurrent.atomic.AtomicBoolean outputShutDown
-
suspendLock
private final java.lang.Object suspendLock
-
suspend
private volatile boolean suspend
-
readRunnable
private volatile java.lang.Runnable readRunnable
-
readerThread
private java.lang.Thread readerThread
-
-
Constructor Detail
-
Nio2Session
public Nio2Session(Nio2Service service, PropertyResolver propertyResolver, 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:IoSessionReturns the value of the user-defined attribute of this session.- Specified by:
getAttributein interfaceIoSession- Parameters:
key- the key of the attribute- Returns:
nullif 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:IoSessionSets a user-defined attribute.- Specified by:
setAttributein interfaceIoSession- Parameters:
key- the key of the attributevalue- the value of the attribute- Returns:
- The old value of the attribute -
nullif it is new.
-
setAttributeIfAbsent
public java.lang.Object setAttributeIfAbsent(java.lang.Object key, java.lang.Object value)Description copied from interface:IoSessionSets 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:
setAttributeIfAbsentin 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 -
nullif not found.
-
removeAttribute
public java.lang.Object removeAttribute(java.lang.Object key)
Description copied from interface:IoSessionRemoves a user-defined attribute with the specified key.- Specified by:
removeAttributein interfaceIoSession- Parameters:
key- The key of the attribute we want to remove- Returns:
- The old value of the attribute -
nullif not found.
-
getRemoteAddress
public java.net.SocketAddress getRemoteAddress()
- Specified by:
getRemoteAddressin interfaceConnectionEndpointsIndicator- Returns:
- the socket address of remote peer.
-
getLocalAddress
public java.net.SocketAddress getLocalAddress()
- Specified by:
getLocalAddressin interfaceConnectionEndpointsIndicator- Returns:
- the socket address of local machine which is associated with this session.
-
getAcceptanceAddress
public java.net.SocketAddress getAcceptanceAddress()
- Specified by:
getAcceptanceAddressin interfaceIoSession- Returns:
- The service address through which this session was accepted -
nullif 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()
Intended for tests simulating a sudden connection drop only! Do not call otherwise.
-
writeBuffer
public IoWriteFuture writeBuffer(Buffer buffer) throws java.io.IOException
Description copied from interface:IoSessionWrite a packet on the socket. Multiple writes can be issued concurrently and will be queued.- Specified by:
writeBufferin interfaceIoSession- Parameters:
buffer- the buffer send. NOTE: the buffer must not be touched until the returned write future is completed.- Returns:
- An
IoWriteFuturethat can be used to check when the packet has actually been sent - Throws:
java.io.IOException- if an error occurred when sending the packet
-
exceptionCaught
protected void exceptionCaught(java.lang.Throwable exc)
-
doCloseGracefully
protected CloseFuture doCloseGracefully()
- Overrides:
doCloseGracefullyin classAbstractCloseable
-
doCloseImmediately
protected void doCloseImmediately()
Description copied from class:AbstractCloseabledoCloseImmediately 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:
doCloseImmediatelyin classAbstractCloseable
-
getService
public Nio2Service getService()
- Specified by:
getServicein interfaceIoSession- Returns:
- the
IoServicethat created this session.
-
shutdownOutputStream
public void shutdownOutputStream() throws java.io.IOExceptionDescription copied from interface:IoSessionHandle received EOF.- Specified by:
shutdownOutputStreamin interfaceIoSession- Throws:
java.io.IOException- If failed to shutdown the stream
-
doShutdownOutputStream
protected void doShutdownOutputStream(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket) throws java.io.IOException
- Throws:
java.io.IOException
-
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)
-
suspendRead
public void suspendRead()
Description copied from interface:IoSessionSuspend read operations on this session. If the session usage includes a graceful shutdown with messages being exchanged, the caller needs to take care of resuming reading the input in order to actually be able to carry on the conversation with the peer.- Specified by:
suspendReadin interfaceIoSession
-
resumeRead
public void resumeRead()
Description copied from interface:IoSessionResume read operations on this session.- Specified by:
resumeReadin interfaceIoSession
-
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:
toStringin classjava.lang.Object
-
-