gumbo.net.msg
Class MessageRouterThread

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--gumbo.net.msg.MessageRouterThread
All Implemented Interfaces:
java.lang.Runnable

public class MessageRouterThread
extends java.lang.Thread

A thread for asynchronously receiving messages and routing them to registered handlers. The reader is used to receive whole messages from the remote. The writer is passed to the router for use by its message handlers to send a response.

Note that the thread is separate from its router since the thread will die if the socket or its connection is lost, but the router should endure in case of a reconnect.

Version:
$Revision: 1.8 $
Author:
Jon Barrilleaux (jonb@jmbaai.com) of JMB and Associates Inc.
See Also:
MessageRouter

Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
MessageRouterThread(MessageRouter router, MessageReader reader, MessageWriter writer)
           
 
Method Summary
 boolean isRemoteBroken()
          Returns true if remote broke the connection for any reason (stream closed or socket exception).
 boolean isStopping()
          Returns true if pleaseStop() has been called.
 void pleaseStop()
          Gracefully stops this thread from running by setting a stop flag (see isStopping()) and closing the reader to break any blocked read.
 void run()
          Runs until the remote breaks the connection or the local closes the socket, such as a result of pleaseStop().
 void setRouter(MessageRouter router)
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MessageRouterThread

public MessageRouterThread(MessageRouter router,
                           MessageReader reader,
                           MessageWriter writer)
                    throws java.io.IOException
Parameters:
router - The message router connected to message handlers. Null if none. Note that messages received while there is no router will be lost.
reader - Used to read messages. Never null. To assure thread safety all entities reading messages should use this reader (i.e. it is the thread lock).
writer - Sent with routed messages for use by message handlers to send reply messages. Null if none (i.e. the message handlers can get the writer through some other means). To assure thread safety all entities writing messages should use this writer (i.e. it is the thread lock).
Method Detail

setRouter

public void setRouter(MessageRouter router)
Parameters:
router - The message router used to route messages from the remote. Null if none. Note that messages received while there is no router will be lost.

isRemoteBroken

public boolean isRemoteBroken()
Returns true if remote broke the connection for any reason (stream closed or socket exception).


pleaseStop

public final void pleaseStop()
Gracefully stops this thread from running by setting a stop flag (see isStopping()) and closing the reader to break any blocked read.


isStopping

public final boolean isStopping()
Returns true if pleaseStop() has been called. Used by thread implementors to determine when they should stop this thread.


run

public void run()
Runs until the remote breaks the connection or the local closes the socket, such as a result of pleaseStop().

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread