public class Session
extends java.lang.Object
IoProcessorvia EventDispatcher.
There are three constructors to create a session:
SocketChannel
.SocketAddress first attempts
to connect to the specified address and then starts communication.
Subscribe first (addSessionListener(SessionListener)) and call
start()to start connection attempt and communication.
SessionConfig| Constructor and Description |
|---|
Session()
Constructs a non-initialized session.
|
Session(IoProcessor ioProcessor,
java.net.SocketAddress socketAddress,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher)
Constructs a new session that connects to the specified socket address
with the default settings.
|
Session(IoProcessor ioProcessor,
java.net.SocketAddress socketAddress,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher,
SessionConfig config)
Constructs a new session that connects to the specified socket address
with the specified settings.
|
Session(IoProcessor ioProcessor,
java.nio.channels.SocketChannel channel,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher)
Constructs a new session with the specified channel and with the default
settings.
|
Session(IoProcessor ioProcessor,
java.nio.channels.SocketChannel channel,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher,
SessionConfig config)
Constructs a new session with the specified channel and with the
specified settings.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addSessionListener(SessionListener listener)
Subscribe a
SessionListenerto receive incoming events. |
void |
close()
Closes the session.
|
void |
fireConnectionClosed()
Fires '
connectionClosed' event to registered
SessionListeners. |
void |
fireConnectionEstablished()
Fires '
connectionEstablished' event to registered
SessionListeners. |
void |
fireExceptionCaught(java.lang.Throwable t)
Fires '
sessionIdle' event to registered
SessionListeners. |
void |
fireMessageReceived(Message m)
Fires '
messageReceived' event to registered
SessionListeners. |
void |
fireMessageSent(Message m)
Fires '
messageSent' event to registered
SessionListeners. |
void |
fireSessionIdle()
Fires '
sessionIdle' event to registered
SessionListeners. |
java.lang.Object |
getAttachment()
Returns the attachment of this session.
|
java.nio.channels.SocketChannel |
getChannel()
Returns the underlying socket channel of this session.
|
SessionConfig |
getConfig()
Returns the current settings of this session.
|
int |
getConnectTimeout()
Deprecated.
Use
SessionConfiginstead. |
int |
getConnectTimeoutInMillis()
Deprecated.
Use
SessionConfiginstead. |
EventDispatcher |
getEventDispatcher()
Returns the
EventDispatcherwho dispatches the events of this
session. |
ExceptionMonitor |
getExceptionMonitor()
Returns the
ExceptionMonitor. |
int |
getIdleTime()
Deprecated.
Use
SessionConfiginstead. |
int |
getIdleTimeInMillis()
Deprecated.
Use
SessionConfiginstead. |
IoProcessor |
getIoProcessor()
Returns the I/O processor this session reads and writes the message via.
|
long |
getLastIoTime()
Returns millis time that I/O occurred last.
|
int |
getMaxQueuedWriteCount()
Deprecated.
Use
SessionConfiginstead. |
MessageRecognizer |
getMessageRecognizer()
Returns the
MessageRecognizerwho recognizes the incoming data
from this session. |
int |
getQueuedWriteCount()
Returns the numbers of remaining write requests which were queued by
write(Message). |
Message |
getReadingMessage()
Returns the
Messagethat is being read now. |
java.net.SocketAddress |
getSocketAddress()
Returns the socket address this session is connected to.
|
java.lang.String |
getSocketAddressString()
Returns the string representation of the socket address this session is
connected to.
|
int |
getWriteTimeout()
Deprecated.
Use
SessionConfiginstead. |
int |
getWriteTimeoutInMillis()
Deprecated.
Use
SessionConfiginstead. |
Message |
getWritingMessage()
Returns the
Messagethat is being written now. |
boolean |
isClosed()
Returns
true if the connection is closed. |
boolean |
isClosing()
Returns
true if once close()is called and close
operation is pending. |
boolean |
isConnected()
Returns
true if the connection is open. |
boolean |
isConnectionPending()
Returns
true if the connection attempt is being made. |
boolean |
isIdle()
Returns
true if and only if this session is idle. |
boolean |
isStarted()
Returns true
|
void |
removeSessionListener(SessionListener listener)
Unsubscribe a
SessionListenerto stop receiving incoming events. |
void |
setAttachment(java.lang.Object newAttachment)
Sets the attachment of this session.
|
void |
setChannel(java.nio.channels.SocketChannel channel)
Sets the underlying socket channel of this session.
|
void |
setConfig(SessionConfig config)
Sets the current settings of this session.
|
void |
setConnectTimeout(int connectTimeout)
Deprecated.
Use
SessionConfiginstead. |
void |
setEventDispatcher(EventDispatcher eventDispatcher)
Sets the
EventDispatcherwho dispatches the events of this
session. |
void |
setExceptionMonitor(ExceptionMonitor monitor)
Sets the
ExceptionMonitor. |
void |
setIdleTime(int idleTime)
Deprecated.
Use
SessionConfiginstead. |
void |
setIoProcessor(IoProcessor ioProcessor)
Sets the I/O processor this session reads and writes the message via.
|
void |
setMaxQueuedWriteCount(int newLimit)
Deprecated.
Use
SessionConfiginstead. |
void |
setMessageRecognizer(MessageRecognizer messageRecognizer)
Sets the
MessageRecognizerwho recognizes the incoming data from
this session. |
void |
setSocketAddress(java.net.SocketAddress socketAddress)
Sets the socket address this session will connect to.
|
void |
setWriteTimeout(int writeTimeout)
Deprecated.
Use
SessionConfiginstead. |
boolean |
start()
Starts communication.
|
boolean |
write(Message message)
Writes the specified message to the socket channel.
|
boolean |
write(Message message,
long timeout)
Writes the specified message to the socket channel.
|
public Session()
setIoProcessor(IoProcessor),
setMessageRecognizer(MessageRecognizer),
setEventDispatcher(EventDispatcher), and
setSocketAddress(SocketAddress)or
setChannel(SocketChannel)to complete the initialization of the
session. Otherwise, you can use this constructor to create a mock object
for unit testing.public Session(IoProcessor ioProcessor, java.nio.channels.SocketChannel channel, MessageRecognizer messageRecognizer, EventDispatcher eventDispatcher)
channel - A SocketChannelto perform the actual I/Ojava.lang.IllegalArgumentException - if the specified channel is not connected yet.public Session(IoProcessor ioProcessor, java.net.SocketAddress socketAddress, MessageRecognizer messageRecognizer, EventDispatcher eventDispatcher)
socketAddress - a SocketAddressto connect tojava.lang.IllegalArgumentException - if the specified timeout is too big or less than
0.public Session(IoProcessor ioProcessor, java.nio.channels.SocketChannel channel, MessageRecognizer messageRecognizer, EventDispatcher eventDispatcher, SessionConfig config)
channel - A SocketChannelto perform the actual I/Oconfig - A session settingsjava.lang.IllegalArgumentException - if the specified channel is not connected yet.public Session(IoProcessor ioProcessor, java.net.SocketAddress socketAddress, MessageRecognizer messageRecognizer, EventDispatcher eventDispatcher, SessionConfig config)
socketAddress - a SocketAddressto connect toconfig - A session settingsjava.lang.IllegalArgumentException - if the specified timeout is too big or less than
0.public void addSessionListener(SessionListener listener)
SessionListenerto receive incoming events.public void removeSessionListener(SessionListener listener)
SessionListenerto stop receiving incoming events.public boolean start()
socketAddress property specifies if and only
if it is set. Otherwise, it will try to start with the socket channel
which is already connected. Please note that this method returns
immediately and you'll get notified from registered SessionListener's
SessionListener.connectionEstablished(Session) method.true, if and only if the communication has been
started. false if the session is already started
or closing.java.lang.IllegalStateException - if any required property is not set or the specified IoProcessor is not started.public void close()
connectionClosed event will be dispatched to
SessionListener. Please note that the messages you've wrote
using write(Message)will be discarded if they are not actually
written to the socket channel.public SessionConfig getConfig()
public void setConfig(SessionConfig config)
public boolean write(Message message)
IoProcessorto
let it to handle I/O operations, so it does not mean the message is
written to the socket channel even if this method returns
true. If the message is really written,
SessionListener.messageSent(Session, Message)method will be
invoked by EventDispatcher.true if and only if the write request has been
queued. false if the connection is closed or
closing.public boolean write(Message message, long timeout)
write(Message) except that it provides a
timeout option in milliseconds unit.public int getQueuedWriteCount()
write(Message).setMaxQueuedWriteCount(int)public boolean isIdle()
true if and only if this session is idle.public long getLastIoTime()
public boolean isStarted()
true
-
isConnected
public boolean isConnected()
Returns true if the connection is open.
-
isClosed
public boolean isClosed()
Returns true if the connection is closed.
-
isClosing
public boolean isClosing()
Returns true if once close()is called and close
operation is pending.
- Returns:
false if the session is not opened or already
closed
-
isConnectionPending
public boolean isConnectionPending()
Returns true if the connection attempt is being made.
-
getSocketAddress
public java.net.SocketAddress getSocketAddress()
Returns the socket address this session is connected to.
-
getSocketAddressString
public java.lang.String getSocketAddressString()
Returns the string representation of the socket address this session is
connected to.
-
setSocketAddress
public void setSocketAddress(java.net.SocketAddress socketAddress)
Sets the socket address this session will connect to. You can reuse this
session using this method. Calling start()method will make this
session connect to the specified socket address.
- Throws:
java.lang.IllegalStateException - if this session is already started.
-
getAttachment
public java.lang.Object getAttachment()
Returns the attachment of this session. The purpose of this method is
identical to SelectionKey.attachment().
-
setAttachment
public void setAttachment(java.lang.Object newAttachment)
Sets the attachment of this session. The purpose of this method is
identical to SelectionKey.attach(java.lang.Object).
-
getReadingMessage
public Message getReadingMessage()
Returns the Messagethat is being read now. This getter is useful
to know what message was being read when an exception is thrown.
- Returns:
null if there is no message being read now.
-
getWritingMessage
public Message getWritingMessage()
Returns the Messagethat is being written now. This getter is
useful to know what message was being written when an exception is
thrown.
- Returns:
null if there is no message being written now.
-
getIoProcessor
public IoProcessor getIoProcessor()
Returns the I/O processor this session reads and writes the message via.
-
setIoProcessor
public void setIoProcessor(IoProcessor ioProcessor)
Sets the I/O processor this session reads and writes the message via.
This property cannot be changed if once set.
- Throws:
java.lang.IllegalStateException - if this property is already set
-
getMessageRecognizer
public MessageRecognizer getMessageRecognizer()
Returns the MessageRecognizerwho recognizes the incoming data
from this session.
-
setMessageRecognizer
public void setMessageRecognizer(MessageRecognizer messageRecognizer)
Sets the MessageRecognizerwho recognizes the incoming data from
this session.
-
getEventDispatcher
public EventDispatcher getEventDispatcher()
Returns the EventDispatcherwho dispatches the events of this
session.
-
setEventDispatcher
public void setEventDispatcher(EventDispatcher eventDispatcher)
Sets the EventDispatcherwho dispatches the events of this
session. This property cannot be changed if once set.
- Throws:
java.lang.IllegalStateException - if this property is already set
-
getExceptionMonitor
public ExceptionMonitor getExceptionMonitor()
Returns the ExceptionMonitor.
-
setExceptionMonitor
public void setExceptionMonitor(ExceptionMonitor monitor)
Sets the ExceptionMonitor. Any uncaught exceptions will be
forwarded to the specified ExceptionMonitor
- Throws:
java.lang.NullPointerException - if monitor is null.
-
getChannel
public java.nio.channels.SocketChannel getChannel()
Returns the underlying socket channel of this session.
- Returns:
null if the connection is closed. You can use
getSocketAddress(),isConnected(),
isConnectionPending(),isClosed()methods
instead.
-
setChannel
public void setChannel(java.nio.channels.SocketChannel channel)
Sets the underlying socket channel of this session. You can reuse this
session using this method. start()method will make this session
communicate using the specified channel.
- Parameters:
channel - the SocketChannelthis session will use
- Throws:
java.lang.IllegalStateException - if this session is already started
java.lang.IllegalArgumentException - if the specified channel is not yet connected
-
fireConnectionEstablished
public void fireConnectionEstablished()
Fires 'connectionEstablished' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
-
fireConnectionClosed
public void fireConnectionClosed()
Fires 'connectionClosed' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
-
fireMessageReceived
public void fireMessageReceived(Message m)
Fires 'messageReceived' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
-
fireMessageSent
public void fireMessageSent(Message m)
Fires 'messageSent' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
-
fireSessionIdle
public void fireSessionIdle()
Fires 'sessionIdle' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
-
fireExceptionCaught
public void fireExceptionCaught(java.lang.Throwable t)
Fires 'sessionIdle' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
-
getConnectTimeout
public int getConnectTimeout()
Deprecated. Use SessionConfiginstead.
-
getConnectTimeoutInMillis
public int getConnectTimeoutInMillis()
Deprecated. Use SessionConfiginstead.
-
setConnectTimeout
public void setConnectTimeout(int connectTimeout)
Deprecated. Use SessionConfiginstead.
-
getIdleTime
public int getIdleTime()
Deprecated. Use SessionConfiginstead.
-
getIdleTimeInMillis
public int getIdleTimeInMillis()
Deprecated. Use SessionConfiginstead.
-
setIdleTime
public void setIdleTime(int idleTime)
Deprecated. Use SessionConfiginstead.
-
getMaxQueuedWriteCount
public int getMaxQueuedWriteCount()
Deprecated. Use SessionConfiginstead.
-
setMaxQueuedWriteCount
public void setMaxQueuedWriteCount(int newLimit)
Deprecated. Use SessionConfiginstead.
-
getWriteTimeout
public int getWriteTimeout()
Deprecated. Use SessionConfiginstead.
-
getWriteTimeoutInMillis
public int getWriteTimeoutInMillis()
Deprecated. Use SessionConfiginstead.
-
setWriteTimeout
public void setWriteTimeout(int writeTimeout)
Deprecated. Use SessionConfiginstead.
Copyright © 2004-2012 Trustin Lee. All Rights Reserved.