|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectnet.gleamynode.netty2.Session
public class Session
Represents a TCP/IP socket connection and provides methods to read/write
events from/to 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 Summary | |
|---|---|
Session()
Constructs a non-initialized session. |
|
Session(IoProcessor ioProcessor,
SocketAddress socketAddress,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher)
Constructs a new session that connects to the specified socket address with the default settings. |
|
Session(IoProcessor ioProcessor,
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,
SocketChannel channel,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher)
Constructs a new session with the specified channel and with the default settings. |
|
Session(IoProcessor ioProcessor,
SocketChannel channel,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher,
SessionConfig config)
Constructs a new session with the specified channel and with the specified settings. |
|
| Method Summary | |
|---|---|
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(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. |
Object |
getAttachment()
Returns the attachment of this session. |
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. |
SocketAddress |
getSocketAddress()
Returns the socket address this session is connected to. |
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(Object newAttachment)
Sets the attachment of this session. |
void |
setChannel(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(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. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
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,
SocketChannel channel,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher)
channel - A SocketChannelto perform the actual I/O
IllegalArgumentException - if the specified channel is not connected yet.
public Session(IoProcessor ioProcessor,
SocketAddress socketAddress,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher)
socketAddress - a SocketAddressto connect to
IllegalArgumentException - if the specified timeout is too big or less than
0.
public Session(IoProcessor ioProcessor,
SocketChannel channel,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher,
SessionConfig config)
channel - A SocketChannelto perform the actual I/Oconfig - A session settings
IllegalArgumentException - if the specified channel is not connected yet.
public Session(IoProcessor ioProcessor,
SocketAddress socketAddress,
MessageRecognizer messageRecognizer,
EventDispatcher eventDispatcher,
SessionConfig config)
socketAddress - a SocketAddressto connect toconfig - A session settings
IllegalArgumentException - if the specified timeout is too big or less than
0.| Method Detail |
|---|
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.
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
public boolean isConnected()
true if the connection is open.
public boolean isClosed()
true if the connection is closed.
public boolean isClosing()
true if once close()is called and close
operation is pending.
false if the session is not opened or already
closedpublic boolean isConnectionPending()
true if the connection attempt is being made.
public SocketAddress getSocketAddress()
public String getSocketAddressString()
public void setSocketAddress(SocketAddress socketAddress)
start()method will make this
session connect to the specified socket address.
IllegalStateException - if this session is already started.public Object getAttachment()
SelectionKey.attachment().
public void setAttachment(Object newAttachment)
SelectionKey.attach(java.lang.Object).
public Message getReadingMessage()
Messagethat is being read now. This getter is useful
to know what message was being read when an exception is thrown.
null if there is no message being read now.public Message getWritingMessage()
Messagethat is being written now. This getter is
useful to know what message was being written when an exception is
thrown.
null if there is no message being written now.public IoProcessor getIoProcessor()
public void setIoProcessor(IoProcessor ioProcessor)
IllegalStateException - if this property is already setpublic MessageRecognizer getMessageRecognizer()
MessageRecognizerwho recognizes the incoming data
from this session.
public void setMessageRecognizer(MessageRecognizer messageRecognizer)
MessageRecognizerwho recognizes the incoming data from
this session.
public EventDispatcher getEventDispatcher()
EventDispatcherwho dispatches the events of this
session.
public void setEventDispatcher(EventDispatcher eventDispatcher)
EventDispatcherwho dispatches the events of this
session. This property cannot be changed if once set.
IllegalStateException - if this property is already setpublic ExceptionMonitor getExceptionMonitor()
ExceptionMonitor.
public void setExceptionMonitor(ExceptionMonitor monitor)
ExceptionMonitor. Any uncaught exceptions will be
forwarded to the specified ExceptionMonitor
NullPointerException - if monitor is null.public SocketChannel getChannel()
null if the connection is closed. You can use
getSocketAddress(),isConnected(),
isConnectionPending(),isClosed()methods
instead.public void setChannel(SocketChannel channel)
start()method will make this session
communicate using the specified channel.
channel - the SocketChannelthis session will use
IllegalStateException - if this session is already started
IllegalArgumentException - if the specified channel is not yet connectedpublic void fireConnectionEstablished()
connectionEstablished' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
public void fireConnectionClosed()
connectionClosed' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
public void fireMessageReceived(Message m)
messageReceived' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
public void fireMessageSent(Message m)
messageSent' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
public void fireSessionIdle()
sessionIdle' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
public void fireExceptionCaught(Throwable t)
sessionIdle' event to registered
SessionListeners. This method is invoked by
EventDispatchers. DO NOT call this method
directly.
public int getConnectTimeout()
SessionConfiginstead.
public int getConnectTimeoutInMillis()
SessionConfiginstead.
public void setConnectTimeout(int connectTimeout)
SessionConfiginstead.
public int getIdleTime()
SessionConfiginstead.
public int getIdleTimeInMillis()
SessionConfiginstead.
public void setIdleTime(int idleTime)
SessionConfiginstead.
public int getMaxQueuedWriteCount()
SessionConfiginstead.
public void setMaxQueuedWriteCount(int newLimit)
SessionConfiginstead.
public int getWriteTimeout()
SessionConfiginstead.
public int getWriteTimeoutInMillis()
SessionConfiginstead.
public void setWriteTimeout(int writeTimeout)
SessionConfiginstead.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||