|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.gleamynode.netty2.Session
Represents a TCP/IP socket connection and provides methods to read/write
events from/to IoProcessor
via 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 SessionListener to receive incoming events. |
void |
close()
Closes the session. |
void |
fireConnectionClosed()
Fires ' connectionClosed ' event to registered
SessionListener s. |
void |
fireConnectionEstablished()
Fires ' connectionEstablished ' event to registered
SessionListener s. |
void |
fireExceptionCaught(Throwable t)
Fires ' sessionIdle ' event to registered
SessionListener s. |
void |
fireMessageReceived(Message m)
Fires ' messageReceived ' event to registered
SessionListener s. |
void |
fireMessageSent(Message m)
Fires ' messageSent ' event to registered
SessionListener s. |
void |
fireSessionIdle()
Fires ' sessionIdle ' event to registered
SessionListener s. |
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 SessionConfig instead. |
int |
getConnectTimeoutInMillis()
Deprecated. Use SessionConfig instead. |
EventDispatcher |
getEventDispatcher()
Returns the EventDispatcher who dispatches the events of this
session. |
ExceptionMonitor |
getExceptionMonitor()
Returns the ExceptionMonitor . |
int |
getIdleTime()
Deprecated. Use SessionConfig instead. |
int |
getIdleTimeInMillis()
Deprecated. Use SessionConfig instead. |
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 SessionConfig instead. |
MessageRecognizer |
getMessageRecognizer()
Returns the MessageRecognizer who 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 Message that 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 SessionConfig instead. |
int |
getWriteTimeoutInMillis()
Deprecated. Use SessionConfig instead. |
Message |
getWritingMessage()
Returns the Message that 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 SessionListener to 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 SessionConfig instead. |
void |
setEventDispatcher(EventDispatcher eventDispatcher)
Sets the EventDispatcher who dispatches the events of this
session. |
void |
setExceptionMonitor(ExceptionMonitor monitor)
Sets the ExceptionMonitor . |
void |
setIdleTime(int idleTime)
Deprecated. Use SessionConfig instead. |
void |
setIoProcessor(IoProcessor ioProcessor)
Sets the I/O processor this session reads and writes the message via. |
void |
setMaxQueuedWriteCount(int newLimit)
Deprecated. Use SessionConfig instead. |
void |
setMessageRecognizer(MessageRecognizer messageRecognizer)
Sets the MessageRecognizer who 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 SessionConfig instead. |
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 SocketChannel
to 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 SocketAddress
to 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 SocketChannel
to 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 SocketAddress
to connect toconfig
- A session settings
IllegalArgumentException
- if the specified timeout is too big or less than
0
.Method Detail |
public void addSessionListener(SessionListener listener)
SessionListener
to receive incoming events.
public void removeSessionListener(SessionListener listener)
SessionListener
to 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)
IoProcessor
to
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()
Message
that 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()
Message
that 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()
MessageRecognizer
who recognizes the incoming data
from this session.
public void setMessageRecognizer(MessageRecognizer messageRecognizer)
MessageRecognizer
who recognizes the incoming data from
this session.
public EventDispatcher getEventDispatcher()
EventDispatcher
who dispatches the events of this
session.
public void setEventDispatcher(EventDispatcher eventDispatcher)
EventDispatcher
who 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 SocketChannel
this 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
SessionListener
s. This method is invoked by
EventDispatcher
s. DO NOT call this method
directly.
public void fireConnectionClosed()
connectionClosed
' event to registered
SessionListener
s. This method is invoked by
EventDispatcher
s. DO NOT call this method
directly.
public void fireMessageReceived(Message m)
messageReceived
' event to registered
SessionListener
s. This method is invoked by
EventDispatcher
s. DO NOT call this method
directly.
public void fireMessageSent(Message m)
messageSent
' event to registered
SessionListener
s. This method is invoked by
EventDispatcher
s. DO NOT call this method
directly.
public void fireSessionIdle()
sessionIdle
' event to registered
SessionListener
s. This method is invoked by
EventDispatcher
s. DO NOT call this method
directly.
public void fireExceptionCaught(Throwable t)
sessionIdle
' event to registered
SessionListener
s. This method is invoked by
EventDispatcher
s. DO NOT call this method
directly.
public int getConnectTimeout()
SessionConfig
instead.
public int getConnectTimeoutInMillis()
SessionConfig
instead.
public void setConnectTimeout(int connectTimeout)
SessionConfig
instead.
public int getIdleTime()
SessionConfig
instead.
public int getIdleTimeInMillis()
SessionConfig
instead.
public void setIdleTime(int idleTime)
SessionConfig
instead.
public int getMaxQueuedWriteCount()
SessionConfig
instead.
public void setMaxQueuedWriteCount(int newLimit)
SessionConfig
instead.
public int getWriteTimeout()
SessionConfig
instead.
public int getWriteTimeoutInMillis()
SessionConfig
instead.
public void setWriteTimeout(int writeTimeout)
SessionConfig
instead.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |