edu.cuny.cat.comm
Class ListenableConnection<M extends Message>

java.lang.Object
  extended by edu.cuny.cat.comm.ListenableConnection<M>
Type Parameters:
M - the type of messages that can be transmitted through the connection.
All Implemented Interfaces:
Connection<M>, ReactiveConnection<M>, java.lang.Runnable

public class ListenableConnection<M extends Message>
extends java.lang.Object
implements java.lang.Runnable, ReactiveConnection<M>

The class wraps the usual querying-based Connection to support actively pushing Message to a listener.

It should be used in the following way:

   Connection<Message> conn;
 
   ...
       
   ReactiveConnection<Message> reactiveConn = ListenableConnection.makeReactiveConnection(conn);
   reactiveConn.setListener(listener);
   reactiveConn.start();
   
   ...
       
   reactiveConn.stop();
   reactiveConn.setListener(null); // (optional) ...
 

Version:
$Revision: 1.4 $
Author:
Jinzhong Niu

Field Summary
protected  ProactiveConnection<M> connection
           
protected  ConnectionListener<M> listener
           
protected  java.lang.Thread thread
           
static long THREAD_STACK_SIZE
           
protected static java.lang.ThreadGroup threadGroup
           
 
Constructor Summary
ListenableConnection(ProactiveConnection<M> connection)
           
 
Method Summary
 void close()
          TODO: not safe to simply set listener null.
 ConnectionListener<M> getListener()
           
 java.lang.String getLocalAddressInfo()
           
 CatpMessage getMessage()
           
 java.lang.String getRemoteAddressInfo()
           
 java.lang.Thread getThread()
          This is needed only for debug purpose.
 boolean isClosed()
          checks whether the connection is closed or not.
static
<T extends Message>
ReactiveConnection<T>
makeReactiveConnection(Connection<T> conn)
          wraps a ProactiveConnection with ListenableConnection if necessary to make it support registering-and-listening message passing, i.e.
 void open()
          opens the connection before sending or receiving any message.
 void run()
          loops to attempt to read messages and notifies the listener of the arrival.
 void sendMessage(M msg)
          sends a Message through this connection.
 void setListener(ConnectionListener<M> listener)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

THREAD_STACK_SIZE

public static final long THREAD_STACK_SIZE
See Also:
Constant Field Values

listener

protected ConnectionListener<M extends Message> listener

connection

protected ProactiveConnection<M extends Message> connection

thread

protected java.lang.Thread thread

threadGroup

protected static java.lang.ThreadGroup threadGroup
Constructor Detail

ListenableConnection

public ListenableConnection(ProactiveConnection<M> connection)
Method Detail

getMessage

public CatpMessage getMessage()
                       throws CatException
Throws:
CatException

sendMessage

public void sendMessage(M msg)
                 throws CatException
Description copied from interface: Connection
sends a Message through this connection.

Specified by:
sendMessage in interface Connection<M extends Message>
Parameters:
msg - message to be sent.
Throws:
CatException

run

public void run()
loops to attempt to read messages and notifies the listener of the arrival.

Specified by:
run in interface java.lang.Runnable
See Also:
ConnectionListener.messageArrived(Message)

setListener

public void setListener(ConnectionListener<M> listener)
Specified by:
setListener in interface ReactiveConnection<M extends Message>

getListener

public ConnectionListener<M> getListener()
Specified by:
getListener in interface ReactiveConnection<M extends Message>

open

public void open()
          throws ConnectionException
Description copied from interface: Connection
opens the connection before sending or receiving any message.

Specified by:
open in interface Connection<M extends Message>
Throws:
ConnectionException

close

public void close()
           throws ConnectionException
TODO: not safe to simply set listener null.

Specified by:
close in interface Connection<M extends Message>
Throws:
ConnectionException

isClosed

public boolean isClosed()
Description copied from interface: Connection
checks whether the connection is closed or not.

Specified by:
isClosed in interface Connection<M extends Message>
Returns:
true if closed; false otherwise.

getLocalAddressInfo

public java.lang.String getLocalAddressInfo()
Specified by:
getLocalAddressInfo in interface Connection<M extends Message>
Returns:
a string describing the address of the local end of this connection

getRemoteAddressInfo

public java.lang.String getRemoteAddressInfo()
Specified by:
getRemoteAddressInfo in interface Connection<M extends Message>
Returns:
a string describing the address of the remote end of this connection

makeReactiveConnection

public static <T extends Message> ReactiveConnection<T> makeReactiveConnection(Connection<T> conn)
wraps a ProactiveConnection with ListenableConnection if necessary to make it support registering-and-listening message passing, i.e. an instance of ReactiveConnection.


getThread

public java.lang.Thread getThread()
This is needed only for debug purpose.

Returns:
the thread that runs to repeatedly read from the proactive connection and call listener to process messages.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object