com.mischiefbox.dmud.net
Class ConnectionInfo

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

public class ConnectionInfo
extends Object
implements Runnable

Connection information object. Provides information about a single connection. Handles the input for the connection in an asynchronous thread--passes the input to the connection handler which will delegate processing to the input queue object. Ouput is handled via an immediate method call from the connection handler.

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

Field Summary
protected  BufferedInputStream bis
          The input stream associated with the socket.
protected  BufferedOutputStream bos
          The output stream associated with the socket.
protected  boolean bProcessing
          Indicates this is is processing input.
protected  ConnectionHandler handler
          The connection handler that created and is responsible for the connection.
protected  int iErrorCount
          The consecutive error count on the connection.
protected  long lLastUseMillis
          The last recorded time I/O was performed on the socket.
static int MAX_ERROR
          The maximum number of consecutive errors permitted for the connection.
protected  String sConnectionId
          The connection id (used to identify the connection).
protected static int SLEEP_MILLIS
          The sleep time for this thread (in between checks for new input).
protected  Socket sock
          The Socket associated with the connection.
protected  Thread tProcessor
          The thread that processes input.
 
Constructor Summary
ConnectionInfo(ConnectionHandler handler, Socket sock, int iConnectionId)
          Create a new connection info object.
 
Method Summary
 void addError()
          Add to and check the error count.
protected  void finalize()
          Finalizer, in case this goes out of scope.
 String getConnectionId()
          Get the connection id.
 int getErrors()
          Get the number of errors on the connection.
 BufferedInputStream getInputStream()
          Get the input stream.
 long getLastUsedTime()
          Get the last used time (in milliseconds).
 BufferedOutputStream getOutputStream()
          Get the output stream.
 boolean isProcessing()
          Indicates if this is processing input.
 void resetError()
          Reset the error count to zero (success in I/O).
 void run()
          Process input on the socket.
 void setLastUsedTime()
          Set the last used time to now.
 void shutdown()
          Shut down the connection gracefully.
 void terminate()
          Close the socket connection immediately.
 boolean writeMessage(byte[] baMessage)
          Accept a message to be written to the client socket.
 
Methods inherited from class java.lang.Object
clone, equals, 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 checks for new input).

MAX_ERROR

public static final int MAX_ERROR
The maximum number of consecutive errors permitted for the connection.

handler

protected ConnectionHandler handler
The connection handler that created and is responsible for the connection.

sock

protected Socket sock
The Socket associated with the connection.

bis

protected BufferedInputStream bis
The input stream associated with the socket.

bos

protected BufferedOutputStream bos
The output stream associated with the socket.

sConnectionId

protected String sConnectionId
The connection id (used to identify the connection).

iErrorCount

protected int iErrorCount
The consecutive error count on the connection. After MAX_ERROR errors, the connection will be dropped.

lLastUseMillis

protected long lLastUseMillis
The last recorded time I/O was performed on the socket. I/O may be performed without recording that it was done.

bProcessing

protected boolean bProcessing
Indicates this is is processing input.

tProcessor

protected Thread tProcessor
The thread that processes input.
Constructor Detail

ConnectionInfo

public ConnectionInfo(ConnectionHandler handler,
                      Socket sock,
                      int iConnectionId)
Create a new connection info object. The new ConnectionInfo object is responsible for registering itself with the ConnectionHandler if the info object initializes properly.
Parameters:
handler - the connection handler that created the connection info object.
sock - the socket this connection will use for I/O.
iConnectionId - the connection id.
Method Detail

run

public void run()
Process input on the socket.
Specified by:
run in interface Runnable

writeMessage

public boolean writeMessage(byte[] baMessage)
Accept a message to be written to the client socket.
Parameters:
baMessage - the message to be written.
Returns:
true on successful write.

isProcessing

public boolean isProcessing()
Indicates if this is processing input. Stop this by using the shutdown() method.
Returns:
true if this is processing input.

getConnectionId

public String getConnectionId()
Get the connection id.
Returns:
the connection id.

getLastUsedTime

public long getLastUsedTime()
Get the last used time (in milliseconds).
Returns:
the last used time.

setLastUsedTime

public void setLastUsedTime()
Set the last used time to now. Synchronized access to last used time field.

getInputStream

public BufferedInputStream getInputStream()
Get the input stream.
Returns:
the input stream for the connection.

getOutputStream

public BufferedOutputStream getOutputStream()
Get the output stream.
Returns:
the output stream for the connection.

terminate

public void terminate()
Close the socket connection immediately. This will not unregister the connection from the handler, nor will it do any housekeeping outside this class. Intended to be called by the ConnectionHandler only! This should be the last call in the chain when a connection is shut down gracefully (the connection info object unregistered, etc).

shutdown

public void shutdown()
Shut down the connection gracefully. This will ask the handler to unregister this connection info. The handler will invoke the terminate() method and null the reference to the connection info object.

getErrors

public int getErrors()
Get the number of errors on the connection.
Returns:
the number of consecutive errors on this connection.

addError

public void addError()
Add to and check the error count.

resetError

public void resetError()
Reset the error count to zero (success in I/O).

finalize

protected void finalize()
                 throws Throwable
Finalizer, in case this goes out of scope.
Overrides:
finalize in class Object


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