com.mischiefbox.pollserve
Class ConnectionHandler

java.lang.Object
  |
  +--com.mischiefbox.pollserve.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$
Author:
Chris Jones

Field Summary
protected  boolean bHandling
          Flag to indicate if this is currently handling I/O.
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  Thread tHandler
          The handler thread.
 
Constructor Summary
ConnectionHandler(InputQueue qInput, Queue qOutput)
          Create a new connection handler.
 
Method Summary
 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

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


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