Package com.google.code.yanf4j.core.impl
Class AbstractSession
- java.lang.Object
-
- com.google.code.yanf4j.core.impl.AbstractSession
-
- All Implemented Interfaces:
Session
- Direct Known Subclasses:
AbstractNioSession
public abstract class AbstractSession extends java.lang.Object implements Session
Base connection
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classAbstractSession.FailFuture-
Nested classes/interfaces inherited from interface com.google.code.yanf4j.core.Session
Session.SessionStatus
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object>attributesprotected booleanclosedprotected java.util.concurrent.atomic.AtomicReference<WriteMessage>currentMessageprotected CodecFactory.Decoderdecoderprotected DispatcherdispatchMessageDispatcherprotected CodecFactory.Encoderencoderprotected Handlerhandlerprotected booleanhandleReadWriteConcurrentlyjava.util.concurrent.atomic.AtomicLonglastOperationTimeStampprotected static org.slf4j.Loggerlogprotected booleanloopbackprotected IoBufferreadBufferprotected java.util.concurrent.atomic.AtomicLongscheduleWritenBytesprotected longsessionIdleTimeoutprotected longsessionTimeoutprotected Statisticsstatisticsprotected booleanuseBlockingReadprotected booleanuseBlockingWriteprotected java.util.concurrent.locks.ReentrantLockwriteLockprotected java.util.Queue<WriteMessage>writeQueue
-
Constructor Summary
Constructors Constructor Description AbstractSession(SessionConfig sessionConfig)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclearAttributes()Clear attributesvoidclearWriteQueue()voidclose()Close sessionprotected abstract voidcloseChannel()abstract voiddecode()protected voiddispatchReceivedMessage(java.lang.Object message)java.lang.ObjectgetAttribute(java.lang.String key)Return attribute associated with keyCodecFactory.DecodergetDecoder()Return current decoderDispatchergetDispatchMessageDispatcher()CodecFactory.EncodergetEncoder()Return current encoderHandlergetHandler()longgetLastOperationTimeStamp()Return last operation timestamp,operation include read,write,idleIoBuffergetReadBuffer()java.nio.ByteOrdergetReadBufferByteOrder()Return the session read buffer's byte order,big end or little end.longgetScheduleWritenBytes()Return the bytes in write queue,there bytes is in memory.Use this method to controll writing speed.longgetSessionIdleTimeout()longgetSessionTimeout()StatisticsgetStatistics()java.util.concurrent.locks.ReentrantLockgetWriteLock()java.util.Queue<WriteMessage>getWriteQueue()booleanisClosed()Check if session is closedbooleanisExpired()Return true if session is expired,session is expired beacause you set the sessionTimeout,if since session's last operation form now is over this vlaue,isExpired return true,and Handler.onExpired() will be invoked.booleanisHandleReadWriteConcurrently()Return true if allow handling read and write concurrently,default is true.booleanisIdle()Check if session is idlebooleanisLoopbackConnection()return true if it is a loopback connectionbooleanisUseBlockingRead()Return true if using blocking readbooleanisUseBlockingWrite()Return true if using blocking writeprotected voidonClosed()protected voidonConnected()protected voidonCreated()voidonException(java.lang.Throwable e)voidonExpired()protected voidonIdle()private voidonMessage(java.lang.Object message, Session session)protected voidonStarted()protected WriteMessagepreprocessWriteMessage(WriteMessage writeMessage)Pre-Process WriteMessage before writing to channelvoidremoveAttribute(java.lang.String key)Remove attributevoidsetAttribute(java.lang.String key, java.lang.Object value)Set a attribute attched with this sessionjava.lang.ObjectsetAttributeIfAbsent(java.lang.String key, java.lang.Object value)voidsetClosed(boolean closed)voidsetDecoder(CodecFactory.Decoder decoder)voidsetEncoder(CodecFactory.Encoder encoder)Set encodervoidsetHandleReadWriteConcurrently(boolean handleReadWriteConcurrently)voidsetReadBuffer(IoBuffer readBuffer)voidsetReadBufferByteOrder(java.nio.ByteOrder readBufferByteOrder)voidsetSessionIdleTimeout(long sessionIdleTimeout)voidsetSessionTimeout(long sessionTimeout)voidsetUseBlockingRead(boolean useBlockingRead)voidsetUseBlockingWrite(boolean useBlockingWrite)Set if using blocking writevoidstart()Start sessionprotected abstract voidstart0()longtransferFrom(long position, long count, java.nio.channels.FileChannel source)longtransferTo(long position, long count, java.nio.channels.FileChannel target)voidupdateTimeStamp()protected abstract WriteMessagewrapMessage(java.lang.Object msg, java.util.concurrent.Future<java.lang.Boolean> writeFuture)voidwrite(java.lang.Object packet)Write a message,if you don't care when the message is writtenabstract voidwriteFromUserCode(WriteMessage message)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.google.code.yanf4j.core.Session
flush, getLocalAddress, getRemoteSocketAddress
-
-
-
-
Field Detail
-
readBuffer
protected IoBuffer readBuffer
-
log
protected static final org.slf4j.Logger log
-
attributes
protected final java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object> attributes
-
writeQueue
protected java.util.Queue<WriteMessage> writeQueue
-
sessionIdleTimeout
protected long sessionIdleTimeout
-
sessionTimeout
protected long sessionTimeout
-
encoder
protected CodecFactory.Encoder encoder
-
decoder
protected CodecFactory.Decoder decoder
-
closed
protected volatile boolean closed
-
statistics
protected Statistics statistics
-
handler
protected Handler handler
-
loopback
protected boolean loopback
-
lastOperationTimeStamp
public java.util.concurrent.atomic.AtomicLong lastOperationTimeStamp
-
scheduleWritenBytes
protected java.util.concurrent.atomic.AtomicLong scheduleWritenBytes
-
dispatchMessageDispatcher
protected final Dispatcher dispatchMessageDispatcher
-
useBlockingWrite
protected boolean useBlockingWrite
-
useBlockingRead
protected boolean useBlockingRead
-
handleReadWriteConcurrently
protected boolean handleReadWriteConcurrently
-
writeLock
protected java.util.concurrent.locks.ReentrantLock writeLock
-
currentMessage
protected java.util.concurrent.atomic.AtomicReference<WriteMessage> currentMessage
-
-
Constructor Detail
-
AbstractSession
public AbstractSession(SessionConfig sessionConfig)
-
-
Method Detail
-
getSessionIdleTimeout
public long getSessionIdleTimeout()
- Specified by:
getSessionIdleTimeoutin interfaceSession
-
setSessionIdleTimeout
public void setSessionIdleTimeout(long sessionIdleTimeout)
- Specified by:
setSessionIdleTimeoutin interfaceSession
-
getSessionTimeout
public long getSessionTimeout()
- Specified by:
getSessionTimeoutin interfaceSession
-
setSessionTimeout
public void setSessionTimeout(long sessionTimeout)
- Specified by:
setSessionTimeoutin interfaceSession
-
getWriteQueue
public java.util.Queue<WriteMessage> getWriteQueue()
-
getStatistics
public Statistics getStatistics()
-
getHandler
public Handler getHandler()
- Specified by:
getHandlerin interfaceSession
-
getDispatchMessageDispatcher
public Dispatcher getDispatchMessageDispatcher()
-
getWriteLock
public java.util.concurrent.locks.ReentrantLock getWriteLock()
-
decode
public abstract void decode()
-
updateTimeStamp
public void updateTimeStamp()
-
getLastOperationTimeStamp
public long getLastOperationTimeStamp()
Description copied from interface:SessionReturn last operation timestamp,operation include read,write,idle- Specified by:
getLastOperationTimeStampin interfaceSession- Returns:
-
isHandleReadWriteConcurrently
public final boolean isHandleReadWriteConcurrently()
Description copied from interface:SessionReturn true if allow handling read and write concurrently,default is true.- Specified by:
isHandleReadWriteConcurrentlyin interfaceSession- Returns:
-
setHandleReadWriteConcurrently
public final void setHandleReadWriteConcurrently(boolean handleReadWriteConcurrently)
- Specified by:
setHandleReadWriteConcurrentlyin interfaceSession
-
getScheduleWritenBytes
public long getScheduleWritenBytes()
Description copied from interface:SessionReturn the bytes in write queue,there bytes is in memory.Use this method to controll writing speed.- Specified by:
getScheduleWritenBytesin interfaceSession- Returns:
-
getEncoder
public CodecFactory.Encoder getEncoder()
Description copied from interface:SessionReturn current encoder- Specified by:
getEncoderin interfaceSession- Returns:
-
setEncoder
public void setEncoder(CodecFactory.Encoder encoder)
Description copied from interface:SessionSet encoder- Specified by:
setEncoderin interfaceSession
-
getDecoder
public CodecFactory.Decoder getDecoder()
Description copied from interface:SessionReturn current decoder- Specified by:
getDecoderin interfaceSession- Returns:
-
getReadBuffer
public IoBuffer getReadBuffer()
-
setReadBuffer
public void setReadBuffer(IoBuffer readBuffer)
-
setDecoder
public void setDecoder(CodecFactory.Decoder decoder)
- Specified by:
setDecoderin interfaceSession
-
getReadBufferByteOrder
public final java.nio.ByteOrder getReadBufferByteOrder()
Description copied from interface:SessionReturn the session read buffer's byte order,big end or little end.- Specified by:
getReadBufferByteOrderin interfaceSession- Returns:
-
setReadBufferByteOrder
public final void setReadBufferByteOrder(java.nio.ByteOrder readBufferByteOrder)
- Specified by:
setReadBufferByteOrderin interfaceSession
-
onIdle
protected void onIdle()
-
onConnected
protected void onConnected()
-
onExpired
public void onExpired()
-
wrapMessage
protected abstract WriteMessage wrapMessage(java.lang.Object msg, java.util.concurrent.Future<java.lang.Boolean> writeFuture)
-
preprocessWriteMessage
protected WriteMessage preprocessWriteMessage(WriteMessage writeMessage)
Pre-Process WriteMessage before writing to channel- Parameters:
writeMessage-- Returns:
-
dispatchReceivedMessage
protected void dispatchReceivedMessage(java.lang.Object message)
-
onMessage
private void onMessage(java.lang.Object message, Session session)
-
isClosed
public final boolean isClosed()
Description copied from interface:SessionCheck if session is closed
-
setClosed
public final void setClosed(boolean closed)
-
close
public void close()
Description copied from interface:SessionClose session
-
closeChannel
protected abstract void closeChannel() throws java.io.IOException- Throws:
java.io.IOException
-
onException
public void onException(java.lang.Throwable e)
-
onClosed
protected void onClosed()
-
setAttribute
public void setAttribute(java.lang.String key, java.lang.Object value)Description copied from interface:SessionSet a attribute attched with this session- Specified by:
setAttributein interfaceSession
-
setAttributeIfAbsent
public java.lang.Object setAttributeIfAbsent(java.lang.String key, java.lang.Object value)- Specified by:
setAttributeIfAbsentin interfaceSession
-
removeAttribute
public void removeAttribute(java.lang.String key)
Description copied from interface:SessionRemove attribute- Specified by:
removeAttributein interfaceSession
-
getAttribute
public java.lang.Object getAttribute(java.lang.String key)
Description copied from interface:SessionReturn attribute associated with key- Specified by:
getAttributein interfaceSession- Returns:
-
clearAttributes
public void clearAttributes()
Description copied from interface:SessionClear attributes- Specified by:
clearAttributesin interfaceSession
-
start
public void start()
Description copied from interface:SessionStart session
-
start0
protected abstract void start0()
-
onStarted
protected void onStarted()
-
write
public void write(java.lang.Object packet)
Description copied from interface:SessionWrite a message,if you don't care when the message is written
-
writeFromUserCode
public abstract void writeFromUserCode(WriteMessage message)
-
isLoopbackConnection
public final boolean isLoopbackConnection()
Description copied from interface:Sessionreturn true if it is a loopback connection- Specified by:
isLoopbackConnectionin interfaceSession- Returns:
-
isUseBlockingWrite
public boolean isUseBlockingWrite()
Description copied from interface:SessionReturn true if using blocking write- Specified by:
isUseBlockingWritein interfaceSession- Returns:
-
setUseBlockingWrite
public void setUseBlockingWrite(boolean useBlockingWrite)
Description copied from interface:SessionSet if using blocking write- Specified by:
setUseBlockingWritein interfaceSession
-
isUseBlockingRead
public boolean isUseBlockingRead()
Description copied from interface:SessionReturn true if using blocking read- Specified by:
isUseBlockingReadin interfaceSession- Returns:
-
setUseBlockingRead
public void setUseBlockingRead(boolean useBlockingRead)
- Specified by:
setUseBlockingReadin interfaceSession
-
clearWriteQueue
public void clearWriteQueue()
-
isExpired
public boolean isExpired()
Description copied from interface:SessionReturn true if session is expired,session is expired beacause you set the sessionTimeout,if since session's last operation form now is over this vlaue,isExpired return true,and Handler.onExpired() will be invoked.
-
isIdle
public boolean isIdle()
Description copied from interface:SessionCheck if session is idle
-
transferTo
public long transferTo(long position, long count, java.nio.channels.FileChannel target) throws java.io.IOException- Throws:
java.io.IOException
-
transferFrom
public long transferFrom(long position, long count, java.nio.channels.FileChannel source) throws java.io.IOException- Throws:
java.io.IOException
-
onCreated
protected void onCreated()
-
-