inputOutputHandler
Class ClientConnectionHandler

java.lang.Object
  extended by java.lang.Thread
      extended by inputOutputHandler.ClientConnectionHandler
All Implemented Interfaces:
java.lang.Runnable

final class ClientConnectionHandler
extends java.lang.Thread

Handles communication with Clients. Accepts connections, receives Requests and sends Replyies. Requests are put in the InputOutputHandlerController's requestInputQueue (see InputOutputHandlerController.addUnprocessedRequest()). Replyies are taken from the InputOutputHandlerController's replyOutputQueue (see InputOutputHandlerController.getProcessedReply()).

Adds/removes user to/from UserDatabase.

Uses non-blocking I/O.

Author:
Karl-Peter Fuchs

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private  int BACKLOG
          The maximum queue length for incoming connection indications.
private  java.net.InetAddress BIND_ADDRESS
          Address this ClientConnectionHandler's socket shall be bound to.
private  InputOutputHandlerController inputOutputHandler
          Reference on InputOutputHandlerController (Used to add messages).
private static InternalInformationPortController internalInformationPort
          Reference on component InternalInformationPort.
private static java.util.logging.Logger LOGGER
          Logger used to log and display information.
private  int MAX_CONNECTIONS
          Maximum number of connections (=connected Clients).
private  int NUMBER_OF_FURTHER_HOPS
          Number of further mixes between the mix this InputOutputHandler belongs to and the receiver.
private  java.lang.Integer numberOfActiveConnections
          Number of Clients currently connected.
private  OutputStrategyController outputStrategy
          Reference on component OutputStrategy (Used to send InternalMessages).
private  int PORT
          Port number this ClientConnectionHandler runs on.
private  int POSITION_OF_MIX_IN_CASCADE
          The mix' position in the cascade this object belongs to. "1" means "first mix", "2" means "a middle mix" and "3" means "last mix" of cascade.
private  java.security.SecureRandom secureRandom
          Random number generator used to generate User identifiers.
private  java.nio.channels.Selector selector
          Selector used for non-blocking I/O.
private  java.nio.channels.ServerSocketChannel serverSocketChannel
          ServerSocketChannel used for accepting connections.
private  int SO_TIMEOUT
          Maximum amount of time an accept (of a client's connection) may take before being canceled in ms.
private  UserDatabaseController userDatabase
          Reference on component UserDatabase (Used to add/remove User)s.
private  java.util.LinkedList<User> writeRequests
          List of Users with data ready to be sent.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected ClientConnectionHandler(InputOutputHandlerController inputOutputHandler, UserDatabaseController userDatabase, OutputStrategyController outputStrategy)
          Constructs a new ClientConnectionHandler which accepts connections, receives Requests and sends Replyies.
 
Method Summary
protected  void acceptConnections()
          Makes this ClientConnectionHandler wait for connections, Requests and Replyies.
private static java.lang.String getProperty(java.lang.String key)
          Simply used to shorten method calls (calls internalInformationPort.getProperty(key)).
private  void handleAcceptRequest()
          Handles an accept request.
private  void handleReadRequest(java.nio.channels.SelectionKey key)
          Handles a read request.
private  void handleWriteRequest(java.nio.channels.SelectionKey key)
          Handles a write request.
private  void initializeReplyProcess()
          Waits for Replyies ready to be sent (to Clients).
private  void registerWriteRequests()
          Registers all write requests from writeRequests with selector.
 void run()
          Handles read, write and accept events (non-blocking I/O).
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, 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
 

Field Detail

internalInformationPort

private static InternalInformationPortController internalInformationPort
Reference on component InternalInformationPort. Used to display and/or log data and read general settings.


LOGGER

private static final java.util.logging.Logger LOGGER
Logger used to log and display information.


PORT

private final int PORT
Port number this ClientConnectionHandler runs on.


BIND_ADDRESS

private final java.net.InetAddress BIND_ADDRESS
Address this ClientConnectionHandler's socket shall be bound to.


BACKLOG

private final int BACKLOG
The maximum queue length for incoming connection indications. If a connection indication arrives when the queue is full, the connection is refused.


SO_TIMEOUT

private final int SO_TIMEOUT
Maximum amount of time an accept (of a client's connection) may take before being canceled in ms.


MAX_CONNECTIONS

private final int MAX_CONNECTIONS
Maximum number of connections (=connected Clients).


POSITION_OF_MIX_IN_CASCADE

private final int POSITION_OF_MIX_IN_CASCADE
The mix' position in the cascade this object belongs to. "1" means "first mix", "2" means "a middle mix" and "3" means "last mix" of cascade.


NUMBER_OF_FURTHER_HOPS

private final int NUMBER_OF_FURTHER_HOPS
Number of further mixes between the mix this InputOutputHandler belongs to and the receiver.


inputOutputHandler

private InputOutputHandlerController inputOutputHandler
Reference on InputOutputHandlerController (Used to add messages).


userDatabase

private UserDatabaseController userDatabase
Reference on component UserDatabase (Used to add/remove User)s.


outputStrategy

private OutputStrategyController outputStrategy
Reference on component OutputStrategy (Used to send InternalMessages).


writeRequests

private java.util.LinkedList<User> writeRequests
List of Users with data ready to be sent.


secureRandom

private java.security.SecureRandom secureRandom
Random number generator used to generate User identifiers.


numberOfActiveConnections

private java.lang.Integer numberOfActiveConnections
Number of Clients currently connected.


selector

private java.nio.channels.Selector selector
Selector used for non-blocking I/O.


serverSocketChannel

private java.nio.channels.ServerSocketChannel serverSocketChannel
ServerSocketChannel used for accepting connections.

Constructor Detail

ClientConnectionHandler

protected ClientConnectionHandler(InputOutputHandlerController inputOutputHandler,
                                  UserDatabaseController userDatabase,
                                  OutputStrategyController outputStrategy)
Constructs a new ClientConnectionHandler which accepts connections, receives Requests and sends Replyies. Requests are put in the InputOutputHandlerController's requestInputQueue (see InputOutputHandlerController.addUnprocessedRequest()). Replyies are taken from the InputOutputHandlerController's replyOutputQueue (see InputOutputHandlerController.getProcessedReply()).

Adds/removes user to/from UserDatabase.

Uses non-blocking I/O.

Parameters:
inputOutputHandler - Reference on InputOutputHandlerController (Used to add messages).
userDatabase - Reference on component UserDatabase (Used to add/remove User)s.
outputStrategy - Reference on component OutputStrategy (Used to send InternalMessages).
Method Detail

acceptConnections

protected void acceptConnections()
Makes this ClientConnectionHandler wait for connections, Requests and Replyies.


initializeReplyProcess

private void initializeReplyProcess()
Waits for Replyies ready to be sent (to Clients). Ready Replyies are put in the suiting User's buffer. Afterwards the selector is notified about the data being ready to send (non-blocking I/O).

See Also:
writeRequests, selector

run

public void run()
Handles read, write and accept events (non-blocking I/O).

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

registerWriteRequests

private void registerWriteRequests()
Registers all write requests from writeRequests with selector.

See Also:
writeRequests, selector

handleAcceptRequest

private void handleAcceptRequest()
                          throws java.io.IOException
Handles an accept request. Accepts connections until the maximum number of connections is reached (see numberOfActiveConnections, MAX_CONNECTIONS). Generates User objects and adds them to the UserDatabase (if connection accepted).

Throws:
java.io.IOException - If an I/O error occurres.

handleReadRequest

private void handleReadRequest(java.nio.channels.SelectionKey key)
                        throws java.io.IOException
Handles a read request. Writes received data to the suiting User's buffer. If a buffer contains a whole message, a Message object is created and passed to the InputOutputHandlerController (which provides it to the MessageProcessor).

Parameters:
key - Token representing the registration of a SelectableChannel with the selector.
Throws:
java.io.IOException - If an I/O error occurres.
See Also:
selector

handleWriteRequest

private void handleWriteRequest(java.nio.channels.SelectionKey key)
                         throws java.io.IOException
Handles a write request. Writes data from a User's buffer to the suiting SocketChannel.

Parameters:
key - Token representing the registration of a SelectableChannel with the selector.
Throws:
java.io.IOException - If an I/O error occurres.

getProperty

private static java.lang.String getProperty(java.lang.String key)
Simply used to shorten method calls (calls internalInformationPort.getProperty(key)). Returns the property with the specified key from the property file.

Parameters:
key - The property key.
Returns:
The property with the specified key in the property file.