public abstract class AbstractIoSession extends Object implements IoSession
IoSession
.Modifier and Type | Field and Description |
---|---|
static WriteRequest |
CLOSE_REQUEST
An internal write request object that triggers session close.
|
protected IoSessionConfig |
config
The session config
|
static WriteRequest |
MESSAGE_SENT_REQUEST
An internal write request object that triggers message sent events.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractIoSession(IoService service)
Create a Session for a service
|
Modifier and Type | Method and Description |
---|---|
CloseFuture |
close()
Closes this session after all queued write requests
are flushed.
|
CloseFuture |
close(boolean rightNow)
Closes this session immediately or after all queued write requests
are flushed.
|
CloseFuture |
closeNow()
Closes this session immediately.
|
CloseFuture |
closeOnFlush()
Closes this session after all queued write requests are flushed.
|
boolean |
containsAttribute(Object key) |
void |
decreaseReadBufferSize()
Decrease the ReadBuffer size (it will be divided by a factor 2)
|
void |
decreaseScheduledBytesAndMessages(WriteRequest request)
Decrease the counters of written messages and written bytes when a message has been written
|
protected void |
destroy()
Destroy the session
|
boolean |
equals(Object o)
TODO This is a ridiculous implementation.
|
Object |
getAttachment()
Returns an attachment of this session.
|
Object |
getAttribute(Object key)
Returns the value of the user-defined attribute of this session.
|
Object |
getAttribute(Object key,
Object defaultValue)
Returns the value of user defined attribute associated with the
specified key.
|
Set<Object> |
getAttributeKeys() |
IoSessionAttributeMap |
getAttributeMap() |
int |
getBothIdleCount() |
CloseFuture |
getCloseFuture() |
IoSessionConfig |
getConfig() |
long |
getCreationTime() |
Object |
getCurrentWriteMessage()
Returns the message which is being written by
IoService . |
WriteRequest |
getCurrentWriteRequest()
Returns the
WriteRequest which is being processed by
IoService . |
IoHandler |
getHandler() |
long |
getId()
We use an AtomicLong to guarantee that the session ID are unique.
|
int |
getIdleCount(IdleStatus status) |
long |
getLastBothIdleTime() |
long |
getLastIdleTime(IdleStatus status) |
long |
getLastIoTime() |
long |
getLastReaderIdleTime() |
long |
getLastReadTime() |
long |
getLastWriterIdleTime() |
long |
getLastWriteTime() |
abstract IoProcessor |
getProcessor() |
long |
getReadBytes() |
double |
getReadBytesThroughput() |
int |
getReaderIdleCount() |
long |
getReadMessages() |
double |
getReadMessagesThroughput() |
long |
getScheduledWriteBytes() |
int |
getScheduledWriteMessages() |
IoService |
getService() |
SocketAddress |
getServiceAddress() |
WriteRequestQueue |
getWriteRequestQueue()
Get the queue that contains the message waiting for being written.
|
int |
getWriterIdleCount() |
long |
getWrittenBytes() |
double |
getWrittenBytesThroughput() |
long |
getWrittenMessages() |
double |
getWrittenMessagesThroughput() |
int |
hashCode() |
void |
increaseIdleCount(IdleStatus status,
long currentTime)
Increase the count of the various Idle counter
|
void |
increaseReadBufferSize()
Increase the ReadBuffer size (it will double)
|
void |
increaseReadBytes(long increment,
long currentTime)
Increase the number of read bytes
|
void |
increaseReadMessages(long currentTime)
Increase the number of read messages
|
void |
increaseScheduledWriteBytes(int increment)
Increase the number of scheduled write bytes for the session
|
void |
increaseScheduledWriteMessages()
Increase the number of scheduled message to write
|
void |
increaseWrittenBytes(int increment,
long currentTime)
Increase the number of written bytes
|
void |
increaseWrittenMessages(WriteRequest request,
long currentTime)
Increase the number of written messages
|
boolean |
isActive() |
boolean |
isBothIdle() |
boolean |
isClosing() |
boolean |
isConnected() |
boolean |
isIdle(IdleStatus status) |
boolean |
isReaderIdle() |
boolean |
isReadSuspended()
Is read operation is suspended for this session.
|
boolean |
isScheduledForFlush()
Tells if the session is scheduled for flushed
|
boolean |
isSecured() |
boolean |
isServer() |
boolean |
isWriterIdle() |
boolean |
isWriteSuspended()
Is write operation is suspended for this session.
|
static void |
notifyIdleness(Iterator<? extends IoSession> sessions,
long currentTime)
Fires a
IoEventType.SESSION_IDLE event to any applicable sessions
in the specified collection. |
static void |
notifyIdleSession(IoSession session,
long currentTime)
Fires a
IoEventType.SESSION_IDLE event if applicable for the
specified session . |
void |
offerClosedReadFuture()
Inform the ReadFuture that the session has been closed
|
void |
offerFailedReadFuture(Throwable exception)
Associates a failure to a ReadFuture
|
void |
offerReadFuture(Object message)
Associates a message to a ReadFuture
|
ReadFuture |
read()
TODO This javadoc is wrong.
|
Object |
removeAttribute(Object key)
Removes a user-defined attribute with the specified key.
|
boolean |
removeAttribute(Object key,
Object value)
Removes a user defined attribute with the specified key if the current
attribute value is equal to the specified value.
|
boolean |
replaceAttribute(Object key,
Object oldValue,
Object newValue)
Replaces a user defined attribute with the specified key if the
value of the attribute is equals to the specified old value.
|
void |
resumeRead()
Resumes read operations for this session.
|
void |
resumeWrite()
Resumes write operations for this session.
|
void |
scheduledForFlush()
Schedule the session for flushed
|
Object |
setAttachment(Object attachment)
Sets an attachment of this session.
|
Object |
setAttribute(Object key)
Sets a user defined attribute without a value.
|
Object |
setAttribute(Object key,
Object value)
Sets a user-defined attribute.
|
Object |
setAttributeIfAbsent(Object key)
Sets a user defined attribute without a value if the attribute with
the specified key is not set yet.
|
Object |
setAttributeIfAbsent(Object key,
Object value)
Sets a user defined attribute if the attribute with the specified key
is not set yet.
|
void |
setAttributeMap(IoSessionAttributeMap attributes)
Set the map of attributes associated with the session
|
void |
setCurrentWriteRequest(WriteRequest currentWriteRequest)
Associate the current write request with the session
|
boolean |
setScheduledForFlush(boolean schedule)
Set the scheduledForFLush flag.
|
protected void |
setScheduledWriteBytes(int byteCount)
Set the number of scheduled write bytes
|
protected void |
setScheduledWriteMessages(int messages)
Set the number of scheduled write messages
|
void |
setWriteRequestQueue(WriteRequestQueue writeRequestQueue)
Create a new close aware write queue, based on the given write queue.
|
void |
suspendRead()
Suspends read operations for this session.
|
void |
suspendWrite()
Suspends write operations for this session.
|
String |
toString() |
void |
unscheduledForFlush()
Change the session's status : it's not anymore scheduled for flush
|
void |
updateThroughput(long currentTime,
boolean force)
Update all statistical properties related with throughput assuming
the specified time is the current time.
|
WriteFuture |
write(Object message)
Writes the specified
message to remote peer. |
WriteFuture |
write(Object message,
SocketAddress remoteAddress)
(Optional) Writes the specified message to the specified destination.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
getFilterChain, getLocalAddress, getRemoteAddress, getTransportMetadata
protected IoSessionConfig config
public static final WriteRequest CLOSE_REQUEST
public static final WriteRequest MESSAGE_SENT_REQUEST
protected AbstractIoSession(IoService service)
service
- the Service for this sessionpublic final long getId()
getId
in interface IoSession
public abstract IoProcessor getProcessor()
public final boolean isConnected()
isConnected
in interface IoSession
public boolean isActive()
public final boolean isClosing()
public boolean isSecured()
public boolean isServer()
public final CloseFuture getCloseFuture()
getCloseFuture
in interface IoSession
CloseFuture
of this session. This method returns
the same instance whenever user calls it.public final boolean isScheduledForFlush()
public final void scheduledForFlush()
public final void unscheduledForFlush()
public final boolean setScheduledForFlush(boolean schedule)
schedule
- the new value to set if not already set.public final CloseFuture close(boolean rightNow)
CloseFuture
if you want to wait for the session actually closed.public final CloseFuture close()
CloseFuture
if you want to wait for the session actually closed.public final CloseFuture closeOnFlush()
CloseFuture
if you want to wait
for the session actually closed.closeOnFlush
in interface IoSession
public final CloseFuture closeNow()
CloseFuture
.closeNow
in interface IoSession
CloseFuture
that can be use to wait for the completion of this operationprotected void destroy()
public IoHandler getHandler()
getHandler
in interface IoSession
IoHandler
which handles this session.public IoSessionConfig getConfig()
public final ReadFuture read()
read
in interface IoSession
ReadFuture
which is notified when a new message is
received, the connection is closed or an exception is caught. This
operation is especially useful when you implement a client application.
TODO : Describe here how we enable this feature.
However, please note that this operation is disabled by default and
throw IllegalStateException
because all received events must be
queued somewhere to support this operation, possibly leading to memory
leak. This means you have to keep calling IoSession.read()
once you
enabled this operation. To enable this operation, please call
IoSessionConfig.setUseReadOperation(boolean)
with true.public final void offerReadFuture(Object message)
message
- the message to associate to the ReadFuturepublic final void offerFailedReadFuture(Throwable exception)
exception
- the exception to associate to the ReadFuturepublic final void offerClosedReadFuture()
public WriteFuture write(Object message)
message
to remote peer. This
operation is asynchronous; IoHandler.messageSent(IoSession,Object)
will be invoked when the message is actually sent to remote peer.
You can also wait for the returned WriteFuture
if you want
to wait for the message actually written.public WriteFuture write(Object message, SocketAddress remoteAddress)
IoHandler.messageSent(IoSession, Object)
will be invoked when the message is actually sent to remote peer. You can
also wait for the returned WriteFuture
if you want to wait for
the message actually written.
When you implement a client that receives a broadcast message from a server
such as DHCP server, the client might need to send a response message for the
broadcast message the server sent. Because the remote address of the session
is not the address of the server in case of broadcasting, there should be a
way to specify the destination when you write the response message.
This interface provides IoSession.write(Object, SocketAddress)
method so you
can specify the destination.
public final Object getAttachment()
getAttachment
in interface IoSession
public final Object setAttachment(Object attachment)
setAttachment
in interface IoSession
attachment
- The attachmentpublic final Object getAttribute(Object key)
getAttribute
in interface IoSession
key
- the key of the attributepublic final Object getAttribute(Object key, Object defaultValue)
if (containsAttribute(key)) { return getAttribute(key); } else { setAttribute(key, defaultValue); return defaultValue; }
getAttribute
in interface IoSession
key
- the key of the attribute we want to retreivedefaultValue
- the default value of the attributepublic final Object setAttribute(Object key, Object value)
setAttribute
in interface IoSession
key
- the key of the attributevalue
- the value of the attributepublic final Object setAttribute(Object key)
Boolean.TRUE
.setAttribute
in interface IoSession
key
- the key of the attributepublic final Object setAttributeIfAbsent(Object key, Object value)
if (containsAttribute(key)) { return getAttribute(key); } else { return setAttribute(key, value); }
setAttributeIfAbsent
in interface IoSession
key
- The key of the attribute we want to setvalue
- The value we want to setpublic final Object setAttributeIfAbsent(Object key)
Boolean.TRUE
.
This method is same with the following code except that the operation
is performed atomically.
if (containsAttribute(key)) { return getAttribute(key); // might not always be Boolean.TRUE. } else { return setAttribute(key); }
setAttributeIfAbsent
in interface IoSession
key
- The key of the attribute we want to setpublic final Object removeAttribute(Object key)
removeAttribute
in interface IoSession
key
- The key of the attribute we want to removepublic final boolean removeAttribute(Object key, Object value)
if (containsAttribute(key) && getAttribute(key).equals(value)) { removeAttribute(key); return true; } else { return false; }
removeAttribute
in interface IoSession
key
- The key we want to removevalue
- The value we want to removepublic final boolean replaceAttribute(Object key, Object oldValue, Object newValue)
if (containsAttribute(key) && getAttribute(key).equals(oldValue)) { setAttribute(key, newValue); return true; } else { return false; }
replaceAttribute
in interface IoSession
key
- The key we want to replaceoldValue
- The previous valuenewValue
- The new valuepublic final boolean containsAttribute(Object key)
containsAttribute
in interface IoSession
key
- The key of the attribute we are looking for in the sessionpublic final Set<Object> getAttributeKeys()
getAttributeKeys
in interface IoSession
public final IoSessionAttributeMap getAttributeMap()
public final void setAttributeMap(IoSessionAttributeMap attributes)
attributes
- The Map of attributespublic final void setWriteRequestQueue(WriteRequestQueue writeRequestQueue)
writeRequestQueue
- The write request queuepublic final void suspendRead()
suspendRead
in interface IoSession
public final void suspendWrite()
suspendWrite
in interface IoSession
public final void resumeRead()
resumeRead
in interface IoSession
public final void resumeWrite()
resumeWrite
in interface IoSession
public boolean isReadSuspended()
isReadSuspended
in interface IoSession
public boolean isWriteSuspended()
isWriteSuspended
in interface IoSession
public final long getReadBytes()
getReadBytes
in interface IoSession
public final long getWrittenBytes()
getWrittenBytes
in interface IoSession
public final long getReadMessages()
getReadMessages
in interface IoSession
public final long getWrittenMessages()
getWrittenMessages
in interface IoSession
public final double getReadBytesThroughput()
getReadBytesThroughput
in interface IoSession
public final double getWrittenBytesThroughput()
getWrittenBytesThroughput
in interface IoSession
public final double getReadMessagesThroughput()
getReadMessagesThroughput
in interface IoSession
public final double getWrittenMessagesThroughput()
getWrittenMessagesThroughput
in interface IoSession
public final void updateThroughput(long currentTime, boolean force)
calculation interval
.
If, however, force is specified as true, this method
updates the throughput properties immediately.updateThroughput
in interface IoSession
currentTime
- the current time in millisecondsforce
- Force the update if truepublic final long getScheduledWriteBytes()
getScheduledWriteBytes
in interface IoSession
public final int getScheduledWriteMessages()
getScheduledWriteMessages
in interface IoSession
protected void setScheduledWriteBytes(int byteCount)
byteCount
- The number of scheduled bytes for writeprotected void setScheduledWriteMessages(int messages)
messages
- The number of scheduled messages for writepublic final void increaseReadBytes(long increment, long currentTime)
increment
- The number of read bytescurrentTime
- The current timepublic final void increaseReadMessages(long currentTime)
currentTime
- The current timepublic final void increaseWrittenBytes(int increment, long currentTime)
increment
- The number of written bytescurrentTime
- The current timepublic final void increaseWrittenMessages(WriteRequest request, long currentTime)
request
- The written messagecurrentTime
- The current tilepublic final void increaseScheduledWriteBytes(int increment)
increment
- The number of newly added bytes to writepublic final void increaseScheduledWriteMessages()
public final void decreaseScheduledBytesAndMessages(WriteRequest request)
request
- The written messagepublic final WriteRequestQueue getWriteRequestQueue()
getWriteRequestQueue
in interface IoSession
public final WriteRequest getCurrentWriteRequest()
WriteRequest
which is being processed by
IoService
.getCurrentWriteRequest
in interface IoSession
public final Object getCurrentWriteMessage()
IoService
.getCurrentWriteMessage
in interface IoSession
public final void setCurrentWriteRequest(WriteRequest currentWriteRequest)
setCurrentWriteRequest
in interface IoSession
currentWriteRequest
- the current write request to associatepublic final void increaseReadBufferSize()
public final void decreaseReadBufferSize()
public final long getCreationTime()
getCreationTime
in interface IoSession
public final long getLastIoTime()
getLastIoTime
in interface IoSession
public final long getLastReadTime()
getLastReadTime
in interface IoSession
public final long getLastWriteTime()
getLastWriteTime
in interface IoSession
public final boolean isIdle(IdleStatus status)
isIdle
in interface IoSession
status
- The researched idle statusIdleStatus
.public final boolean isBothIdle()
isBothIdle
in interface IoSession
IdleStatus.BOTH_IDLE
.IoSession.isIdle(IdleStatus)
public final boolean isReaderIdle()
isReaderIdle
in interface IoSession
IdleStatus.READER_IDLE
.IoSession.isIdle(IdleStatus)
public final boolean isWriterIdle()
isWriterIdle
in interface IoSession
IdleStatus.WRITER_IDLE
.IoSession.isIdle(IdleStatus)
public final int getIdleCount(IdleStatus status)
getIdleCount
in interface IoSession
status
- The researched idle statusIdleStatus
.
If sessionIdle event is fired first after some time after I/O, idleCount becomes 1. idleCount resets to 0 if any I/O occurs again, otherwise it increases to 2 and so on if sessionIdle event is fired again without any I/O between two (or more) sessionIdle events.
public final long getLastIdleTime(IdleStatus status)
getLastIdleTime
in interface IoSession
status
- The researched idle statusIdleStatus
.public final void increaseIdleCount(IdleStatus status, long currentTime)
status
- The current statuscurrentTime
- The current timepublic final int getBothIdleCount()
getBothIdleCount
in interface IoSession
IdleStatus.BOTH_IDLE
.IoSession.getIdleCount(IdleStatus)
public final long getLastBothIdleTime()
getLastBothIdleTime
in interface IoSession
IdleStatus.BOTH_IDLE
.IoSession.getLastIdleTime(IdleStatus)
public final long getLastReaderIdleTime()
getLastReaderIdleTime
in interface IoSession
IdleStatus.READER_IDLE
.IoSession.getLastIdleTime(IdleStatus)
public final long getLastWriterIdleTime()
getLastWriterIdleTime
in interface IoSession
IdleStatus.WRITER_IDLE
.IoSession.getLastIdleTime(IdleStatus)
public final int getReaderIdleCount()
getReaderIdleCount
in interface IoSession
IdleStatus.READER_IDLE
.IoSession.getIdleCount(IdleStatus)
public final int getWriterIdleCount()
getWriterIdleCount
in interface IoSession
IdleStatus.WRITER_IDLE
.IoSession.getIdleCount(IdleStatus)
public SocketAddress getServiceAddress()
getServiceAddress
in interface IoSession
IoService
listens to to manage
this session. If this session is managed by IoAcceptor
, it
returns the SocketAddress
which is specified as a parameter of
IoAcceptor.bind()
. If this session is managed by
IoConnector
, this method returns the same address with
that of IoSession.getRemoteAddress()
.public final boolean equals(Object o)
public IoService getService()
getService
in interface IoSession
IoService
which provides I/O service to this session.public static void notifyIdleness(Iterator<? extends IoSession> sessions, long currentTime)
IoEventType.SESSION_IDLE
event to any applicable sessions
in the specified collection.sessions
- The sessions that are notifiedcurrentTime
- the current time (i.e. System.currentTimeMillis()
)public static void notifyIdleSession(IoSession session, long currentTime)
IoEventType.SESSION_IDLE
event if applicable for the
specified session
.session
- The session that is notifiedcurrentTime
- the current time (i.e. System.currentTimeMillis()
)Copyright © 2004–2022 Apache MINA Project. All rights reserved.