com.google.code.yanf4j.core.impl
Class AbstractSession

java.lang.Object
  extended by 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

Author:
dennis

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.code.yanf4j.core.Session
Session.SessionStatus
 
Field Summary
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
AbstractSession(SessionConfig sessionConfig)
           
 
Method Summary
 void clearAttributes()
          Clear attributes
 void clearWriteQueue()
           
 void close()
          Close session
protected 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 key
 CodecFactory.Decoder getDecoder()
          Return current decoder
 Dispatcher getDispatchMessageDispatcher()
           
 CodecFactory.Encoder getEncoder()
          Return current encoder
 Handler getHandler()
           
 long getLastOperationTimeStamp()
          Return last operation timestamp,operation include read,write,idle
 IoBuffer 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 closed
 boolean 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 idle
 boolean isLoopbackConnection()
          return true if it is a loopback connection
 boolean isUseBlockingRead()
          Return true if using blocking read
 boolean isUseBlockingWrite()
          Return true if using blocking write
protected  void onClosed()
           
protected  void onConnected()
           
protected  void onCreated()
           
 void onException(java.lang.Throwable e)
           
 void onExpired()
           
protected  void onIdle()
           
protected  void onStarted()
           
protected  WriteMessage preprocessWriteMessage(WriteMessage writeMessage)
          Pre-Process WriteMessage before writing to channel
 void removeAttribute(java.lang.String key)
          Remove attribute
 void setAttribute(java.lang.String key, java.lang.Object value)
          Set a attribute attched with this session
 java.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 encoder
 void 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 write
 void start()
          Start session
protected 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 written
abstract  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 volatile long sessionIdleTimeout

sessionTimeout

protected volatile 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 volatile boolean useBlockingWrite

useBlockingRead

protected volatile boolean useBlockingRead

handleReadWriteConcurrently

protected volatile 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 interface Session

setSessionIdleTimeout

public void setSessionIdleTimeout(long sessionIdleTimeout)
Specified by:
setSessionIdleTimeout in interface Session

getSessionTimeout

public long getSessionTimeout()
Specified by:
getSessionTimeout in interface Session

setSessionTimeout

public void setSessionTimeout(long sessionTimeout)
Specified by:
setSessionTimeout in interface Session

getWriteQueue

public java.util.Queue<WriteMessage> getWriteQueue()

getStatistics

public Statistics getStatistics()

getHandler

public Handler getHandler()
Specified by:
getHandler in interface Session

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 interface Session
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 interface Session
Returns:

setHandleReadWriteConcurrently

public final void setHandleReadWriteConcurrently(boolean handleReadWriteConcurrently)
Specified by:
setHandleReadWriteConcurrently in interface Session

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 interface Session
Returns:

getEncoder

public CodecFactory.Encoder getEncoder()
Description copied from interface: Session
Return current encoder

Specified by:
getEncoder in interface Session
Returns:

setEncoder

public void setEncoder(CodecFactory.Encoder encoder)
Description copied from interface: Session
Set encoder

Specified by:
setEncoder in interface Session

getDecoder

public CodecFactory.Decoder getDecoder()
Description copied from interface: Session
Return current decoder

Specified by:
getDecoder in interface Session
Returns:

getReadBuffer

public IoBuffer getReadBuffer()

setReadBuffer

public void setReadBuffer(IoBuffer readBuffer)

setDecoder

public void setDecoder(CodecFactory.Decoder decoder)
Specified by:
setDecoder in interface Session

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 interface Session
Returns:

setReadBufferByteOrder

public final void setReadBufferByteOrder(java.nio.ByteOrder readBufferByteOrder)
Specified by:
setReadBufferByteOrder in interface Session

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)

isClosed

public final boolean isClosed()
Description copied from interface: Session
Check if session is closed

Specified by:
isClosed in interface Session
Returns:

setClosed

public final void setClosed(boolean closed)

close

public void close()
Description copied from interface: Session
Close session

Specified by:
close in interface 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 interface Session

setAttributeIfAbsent

public java.lang.Object setAttributeIfAbsent(java.lang.String key,
                                             java.lang.Object value)
Specified by:
setAttributeIfAbsent in interface Session

removeAttribute

public void removeAttribute(java.lang.String key)
Description copied from interface: Session
Remove attribute

Specified by:
removeAttribute in interface Session

getAttribute

public java.lang.Object getAttribute(java.lang.String key)
Description copied from interface: Session
Return attribute associated with key

Specified by:
getAttribute in interface Session
Returns:

clearAttributes

public void clearAttributes()
Description copied from interface: Session
Clear attributes

Specified by:
clearAttributes in interface Session

start

public void start()
Description copied from interface: Session
Start session

Specified by:
start in interface 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

Specified by:
write in interface Session

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 interface Session
Returns:

isUseBlockingWrite

public boolean isUseBlockingWrite()
Description copied from interface: Session
Return true if using blocking write

Specified by:
isUseBlockingWrite in interface Session
Returns:

setUseBlockingWrite

public void setUseBlockingWrite(boolean useBlockingWrite)
Description copied from interface: Session
Set if using blocking write

Specified by:
setUseBlockingWrite in interface Session

isUseBlockingRead

public boolean isUseBlockingRead()
Description copied from interface: Session
Return true if using blocking read

Specified by:
isUseBlockingRead in interface Session
Returns:

setUseBlockingRead

public void setUseBlockingRead(boolean useBlockingRead)
Specified by:
setUseBlockingRead in interface Session

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.

Specified by:
isExpired in interface Session
Returns:

isIdle

public boolean isIdle()
Description copied from interface: Session
Check if session is idle

Specified by:
isIdle in interface Session
Returns:

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()


Copyright © 2011. All Rights Reserved.