com.mischiefbox.dmud.net
Class ConnectionHandler

java.lang.Object
  |
  +--com.mischiefbox.dmud.net.ConnectionHandler
All Implemented Interfaces:
Runnable

public class ConnectionHandler
extends Object
implements Runnable

Responsible for keeping track of connection info objects, checking for new input on connections, and passing new output to connections.

Is similar to the functionality of the UNIX select() or poll() call.

Runs in a thread to allow network I/O to operate asynchronously from the rest of the system. Network I/O is checked for input first (on each connection), then attempts to empty the output queue.

Version:
$Id: ConnectionHandler.java,v 1.1.1.1 2001/06/27 01:33:17 cjones Exp $
Author:
Chris Jones

Field Summary
protected  boolean bHandling
          Flag to indicate if this is currently handling I/O.
static String CONNECTION_HANDLER_THREAD_NAME
          The connection handler thread name.
static String CONNECTION_INFO_THREAD_GROUP
          The name of thread group for connection info objects.
protected  HashMap hmConnectionInfo
          Registered connection info objects.
protected  InputQueue qInput
          The input (incoming message) queue.
protected  Queue qOutput
          The output (outgoing message) queue.
protected static int SLEEP_MILLIS
          The sleep time for this thread (in between processing runs).
protected  ThreadGroup tgConnectionInfo
          The connection info object thread group.
protected  Thread tHandler
          The handler thread.
 
Constructor Summary
ConnectionHandler(InputQueue qInput, Queue qOutput)
          Create a new connection handler.
 
Method Summary
 ThreadGroup getConnectionInfoThreadGroup()
          Get the connection info thread group object.
 void inputMessage(ConnectionInfo conInfo, byte[] baMsg)
          Pass a message on the behalf of the connection info to the input queue.
 boolean isHandling()
          Indicates if this is handling I/O.
 void registerConnectionInfo(ConnectionInfo info)
          Register a new connection info object.
 void run()
          Process connection I/O, including checking for available messages from the client and from the message handler.
 void stopHandler()
          Stops the handler from handling I/O.
 void unregisterConnectionInfo(ConnectionInfo info)
          Unregister a connection info object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONNECTION_INFO_THREAD_GROUP

public static final String CONNECTION_INFO_THREAD_GROUP
The name of thread group for connection info objects.

CONNECTION_HANDLER_THREAD_NAME

public static final String CONNECTION_HANDLER_THREAD_NAME
The connection handler thread name.

SLEEP_MILLIS

protected static final int SLEEP_MILLIS
The sleep time for this thread (in between processing runs).

hmConnectionInfo

protected HashMap hmConnectionInfo
Registered connection info objects.

qInput

protected InputQueue qInput
The input (incoming message) queue.

qOutput

protected Queue qOutput
The output (outgoing message) queue.

tHandler

protected Thread tHandler
The handler thread.

bHandling

protected boolean bHandling
Flag to indicate if this is currently handling I/O. Set this to false to stop handling I/O and shutdown all the current connections.

tgConnectionInfo

protected ThreadGroup tgConnectionInfo
The connection info object thread group.
Constructor Detail

ConnectionHandler

public ConnectionHandler(InputQueue qInput,
                         Queue qOutput)
Create a new connection handler. Do the housekeeping to ensure this is set up correctly.
Method Detail

registerConnectionInfo

public void registerConnectionInfo(ConnectionInfo info)
Register a new connection info object.

This isn't synchronized because this should only be called from the new connection info object.


unregisterConnectionInfo

public void unregisterConnectionInfo(ConnectionInfo info)
Unregister a connection info object.

This is synchronized (internally) so that it won't be unregistered twice at once (unregister may be called from more than one place) and so that the connection won't be used while it's being closed.


isHandling

public boolean isHandling()
Indicates if this is handling I/O.
Returns:
true if this is handling connection I/O.

stopHandler

public void stopHandler()
Stops the handler from handling I/O.

Create a new handler to start handling I/O again. This will disconnect all the current clients.


run

public void run()
Process connection I/O, including checking for available messages from the client and from the message handler.
Specified by:
run in interface Runnable

inputMessage

public void inputMessage(ConnectionInfo conInfo,
                         byte[] baMsg)
                  throws MessageTooLargeException
Pass a message on the behalf of the connection info to the input queue.
Parameters:
conInfo - the connection info that received the message.
baMsg - the message received.
Throws:
MessageTooLargeException - when the input message exceeded protocol limits.

getConnectionInfoThreadGroup

public ThreadGroup getConnectionInfoThreadGroup()
Get the connection info thread group object.
Returns:
the connection info thread group object.


Copyright © 2001 by Christopher R. Jones. All Rights Reserved.