net.gleamynode.netty2
Class Session

java.lang.Object
  extended by net.gleamynode.netty2.Session

public class Session
extends Object

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:

Subscribe first (addSessionListener(SessionListener)) and call start()to start connection attempt and communication.

Version:
$Rev: 4 $, $Date: 2005-04-18 12:04:09 +0900 $
Author:
Trustin Lee (http://gleamynode.net/dev/)
See Also:
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

Session

public Session()
Constructs a non-initialized session. This constructor is useful when you're using a lightweight container which provides setter injections (i.e. Spring). You must call 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.


Session

public Session(IoProcessor ioProcessor,
               SocketChannel channel,
               MessageRecognizer messageRecognizer,
               EventDispatcher eventDispatcher)
Constructs a new session with the specified channel and with the default settings.

Parameters:
channel - A SocketChannelto perform the actual I/O
Throws:
IllegalArgumentException - if the specified channel is not connected yet.

Session

public Session(IoProcessor ioProcessor,
               SocketAddress socketAddress,
               MessageRecognizer messageRecognizer,
               EventDispatcher eventDispatcher)
Constructs a new session that connects to the specified socket address with the default settings.

Parameters:
socketAddress - a SocketAddressto connect to
Throws:
IllegalArgumentException - if the specified timeout is too big or less than 0.

Session

public Session(IoProcessor ioProcessor,
               SocketChannel channel,
               MessageRecognizer messageRecognizer,
               EventDispatcher eventDispatcher,
               SessionConfig config)
Constructs a new session with the specified channel and with the specified settings.

Parameters:
channel - A SocketChannelto perform the actual I/O
config - A session settings
Throws:
IllegalArgumentException - if the specified channel is not connected yet.

Session

public 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.

Parameters:
socketAddress - a SocketAddressto connect to
config - A session settings
Throws:
IllegalArgumentException - if the specified timeout is too big or less than 0.
Method Detail

addSessionListener

public void addSessionListener(SessionListener listener)
Subscribe a SessionListenerto receive incoming events.


removeSessionListener

public void removeSessionListener(SessionListener listener)
Unsubscribe a SessionListenerto stop receiving incoming events.


start

public boolean start()
Starts communication. I/O processor will will try to connect to the address that 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.

Returns:
true, if and only if the communication has been started. false if the session is already started or closing.
Throws:
IllegalStateException - if any required property is not set or the specified IoProcessor is not started.

close

public void close()
Closes the session. Socket connection will be closed and 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.


getConfig

public SessionConfig getConfig()
Returns the current settings of this session.


setConfig

public void setConfig(SessionConfig config)
Sets the current settings of this session.


write

public boolean write(Message message)
Writes the specified message to the socket channel. This method does not directly write the message using I/O operations, but it just queues the message into the internal queue and notify it to 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.

Returns:
true if and only if the write request has been queued. false if the connection is closed or closing.

write

public boolean write(Message message,
                     long timeout)
Writes the specified message to the socket channel. This method is identical with write(Message) except that it provides a timeout option in milliseconds unit.


getQueuedWriteCount

public int getQueuedWriteCount()
Returns the numbers of remaining write requests which were queued by write(Message).

See Also:
setMaxQueuedWriteCount(int)

isIdle

public boolean isIdle()
Returns true if and only if this session is idle.


getLastIoTime

public long getLastIoTime()
Returns millis time that I/O occurred last.


isStarted

public boolean isStarted()
Returns 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 SocketAddress getSocketAddress()
Returns the socket address this session is connected to.


getSocketAddressString

public String getSocketAddressString()
Returns the string representation of the socket address this session is connected to.


setSocketAddress

public void setSocketAddress(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:
IllegalStateException - if this session is already started.

getAttachment

public Object getAttachment()
Returns the attachment of this session. The purpose of this method is identical to SelectionKey.attachment().


setAttachment

public void setAttachment(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:
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:
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:
NullPointerException - if monitor is null.

getChannel

public 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(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:
IllegalStateException - if this session is already started
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(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-2010 Trustin Lee. All Rights Reserved.