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 class
AbstractSession.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>
attributes
protected boolean
closed
protected java.util.concurrent.atomic.AtomicReference<WriteMessage>
currentMessage
protected CodecFactory.Decoder
decoder
protected Dispatcher
dispatchMessageDispatcher
protected CodecFactory.Encoder
encoder
protected Handler
handler
protected boolean
handleReadWriteConcurrently
java.util.concurrent.atomic.AtomicLong
lastOperationTimeStamp
protected static org.slf4j.Logger
log
protected boolean
loopback
protected IoBuffer
readBuffer
protected java.util.concurrent.atomic.AtomicLong
scheduleWritenBytes
protected long
sessionIdleTimeout
protected long
sessionTimeout
protected Statistics
statistics
protected boolean
useBlockingRead
protected boolean
useBlockingWrite
protected java.util.concurrent.locks.ReentrantLock
writeLock
protected 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 void
clearAttributes()
Clear attributesvoid
clearWriteQueue()
void
close()
Close sessionprotected abstract void
closeChannel()
abstract void
decode()
protected void
dispatchReceivedMessage(java.lang.Object message)
java.lang.Object
getAttribute(java.lang.String key)
Return attribute associated with keyCodecFactory.Decoder
getDecoder()
Return current decoderDispatcher
getDispatchMessageDispatcher()
CodecFactory.Encoder
getEncoder()
Return current encoderHandler
getHandler()
long
getLastOperationTimeStamp()
Return last operation timestamp,operation include read,write,idleIoBuffer
getReadBuffer()
java.nio.ByteOrder
getReadBufferByteOrder()
Return the session read buffer's byte order,big end or little end.long
getScheduleWritenBytes()
Return the bytes in write queue,there bytes is in memory.Use this method to controll writing speed.long
getSessionIdleTimeout()
long
getSessionTimeout()
Statistics
getStatistics()
java.util.concurrent.locks.ReentrantLock
getWriteLock()
java.util.Queue<WriteMessage>
getWriteQueue()
boolean
isClosed()
Check if session is closedboolean
isExpired()
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.boolean
isHandleReadWriteConcurrently()
Return true if allow handling read and write concurrently,default is true.boolean
isIdle()
Check if session is idleboolean
isLoopbackConnection()
return true if it is a loopback connectionboolean
isUseBlockingRead()
Return true if using blocking readboolean
isUseBlockingWrite()
Return true if using blocking writeprotected void
onClosed()
protected void
onConnected()
protected void
onCreated()
void
onException(java.lang.Throwable e)
void
onExpired()
protected void
onIdle()
private void
onMessage(java.lang.Object message, Session session)
protected void
onStarted()
protected WriteMessage
preprocessWriteMessage(WriteMessage writeMessage)
Pre-Process WriteMessage before writing to channelvoid
removeAttribute(java.lang.String key)
Remove attributevoid
setAttribute(java.lang.String key, java.lang.Object value)
Set a attribute attched with this sessionjava.lang.Object
setAttributeIfAbsent(java.lang.String key, java.lang.Object value)
void
setClosed(boolean closed)
void
setDecoder(CodecFactory.Decoder decoder)
void
setEncoder(CodecFactory.Encoder encoder)
Set encodervoid
setHandleReadWriteConcurrently(boolean handleReadWriteConcurrently)
void
setReadBuffer(IoBuffer readBuffer)
void
setReadBufferByteOrder(java.nio.ByteOrder readBufferByteOrder)
void
setSessionIdleTimeout(long sessionIdleTimeout)
void
setSessionTimeout(long sessionTimeout)
void
setUseBlockingRead(boolean useBlockingRead)
void
setUseBlockingWrite(boolean useBlockingWrite)
Set if using blocking writevoid
start()
Start sessionprotected abstract void
start0()
long
transferFrom(long position, long count, java.nio.channels.FileChannel source)
long
transferTo(long position, long count, java.nio.channels.FileChannel target)
void
updateTimeStamp()
protected abstract WriteMessage
wrapMessage(java.lang.Object msg, java.util.concurrent.Future<java.lang.Boolean> writeFuture)
void
write(java.lang.Object packet)
Write a message,if you don't care when the message is writtenabstract void
writeFromUserCode(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:
getSessionIdleTimeout
in interfaceSession
-
setSessionIdleTimeout
public void setSessionIdleTimeout(long sessionIdleTimeout)
- Specified by:
setSessionIdleTimeout
in interfaceSession
-
getSessionTimeout
public long getSessionTimeout()
- Specified by:
getSessionTimeout
in interfaceSession
-
setSessionTimeout
public void setSessionTimeout(long sessionTimeout)
- Specified by:
setSessionTimeout
in interfaceSession
-
getWriteQueue
public java.util.Queue<WriteMessage> getWriteQueue()
-
getStatistics
public Statistics getStatistics()
-
getHandler
public Handler getHandler()
- Specified by:
getHandler
in 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:Session
Return last operation timestamp,operation include read,write,idle- Specified by:
getLastOperationTimeStamp
in interfaceSession
- Returns:
-
isHandleReadWriteConcurrently
public final boolean isHandleReadWriteConcurrently()
Description copied from interface:Session
Return true if allow handling read and write concurrently,default is true.- Specified by:
isHandleReadWriteConcurrently
in interfaceSession
- Returns:
-
setHandleReadWriteConcurrently
public final void setHandleReadWriteConcurrently(boolean handleReadWriteConcurrently)
- Specified by:
setHandleReadWriteConcurrently
in interfaceSession
-
getScheduleWritenBytes
public long getScheduleWritenBytes()
Description copied from interface:Session
Return the bytes in write queue,there bytes is in memory.Use this method to controll writing speed.- Specified by:
getScheduleWritenBytes
in interfaceSession
- Returns:
-
getEncoder
public CodecFactory.Encoder getEncoder()
Description copied from interface:Session
Return current encoder- Specified by:
getEncoder
in interfaceSession
- Returns:
-
setEncoder
public void setEncoder(CodecFactory.Encoder encoder)
Description copied from interface:Session
Set encoder- Specified by:
setEncoder
in interfaceSession
-
getDecoder
public CodecFactory.Decoder getDecoder()
Description copied from interface:Session
Return current decoder- Specified by:
getDecoder
in interfaceSession
- Returns:
-
getReadBuffer
public IoBuffer getReadBuffer()
-
setReadBuffer
public void setReadBuffer(IoBuffer readBuffer)
-
setDecoder
public void setDecoder(CodecFactory.Decoder decoder)
- Specified by:
setDecoder
in interfaceSession
-
getReadBufferByteOrder
public final java.nio.ByteOrder getReadBufferByteOrder()
Description copied from interface:Session
Return the session read buffer's byte order,big end or little end.- Specified by:
getReadBufferByteOrder
in interfaceSession
- Returns:
-
setReadBufferByteOrder
public final void setReadBufferByteOrder(java.nio.ByteOrder readBufferByteOrder)
- Specified by:
setReadBufferByteOrder
in 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:Session
Check if session is closed
-
setClosed
public final void setClosed(boolean closed)
-
close
public void close()
Description copied from interface:Session
Close 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:Session
Set a attribute attched with this session- Specified by:
setAttribute
in interfaceSession
-
setAttributeIfAbsent
public java.lang.Object setAttributeIfAbsent(java.lang.String key, java.lang.Object value)
- Specified by:
setAttributeIfAbsent
in interfaceSession
-
removeAttribute
public void removeAttribute(java.lang.String key)
Description copied from interface:Session
Remove attribute- Specified by:
removeAttribute
in interfaceSession
-
getAttribute
public java.lang.Object getAttribute(java.lang.String key)
Description copied from interface:Session
Return attribute associated with key- Specified by:
getAttribute
in interfaceSession
- Returns:
-
clearAttributes
public void clearAttributes()
Description copied from interface:Session
Clear attributes- Specified by:
clearAttributes
in interfaceSession
-
start
public void start()
Description copied from interface:Session
Start session
-
start0
protected abstract void start0()
-
onStarted
protected void onStarted()
-
write
public void write(java.lang.Object packet)
Description copied from interface:Session
Write 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:Session
return true if it is a loopback connection- Specified by:
isLoopbackConnection
in interfaceSession
- Returns:
-
isUseBlockingWrite
public boolean isUseBlockingWrite()
Description copied from interface:Session
Return true if using blocking write- Specified by:
isUseBlockingWrite
in interfaceSession
- Returns:
-
setUseBlockingWrite
public void setUseBlockingWrite(boolean useBlockingWrite)
Description copied from interface:Session
Set if using blocking write- Specified by:
setUseBlockingWrite
in interfaceSession
-
isUseBlockingRead
public boolean isUseBlockingRead()
Description copied from interface:Session
Return true if using blocking read- Specified by:
isUseBlockingRead
in interfaceSession
- Returns:
-
setUseBlockingRead
public void setUseBlockingRead(boolean useBlockingRead)
- Specified by:
setUseBlockingRead
in interfaceSession
-
clearWriteQueue
public void clearWriteQueue()
-
isExpired
public boolean isExpired()
Description copied from interface:Session
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.
-
isIdle
public boolean isIdle()
Description copied from interface:Session
Check 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()
-
-