stella.net
Class CommandReceiver.CommandHandler

java.lang.Object
  extended by java.lang.Thread
      extended by stella.net.Handler
          extended by stella.net.CommandReceiver.CommandHandler
All Implemented Interfaces:
Runnable
Enclosing class:
CommandReceiver

protected class CommandReceiver.CommandHandler
extends Handler

Handles the command protocol. To allow fast-return of the command-receiving end of the outer class, CommandReceiver.protocol(java.net.Socket), the actual command handling is done with a seperate class that is an extension to Thread. It is constructed with the socket connected to the client and a reference to the calling class. A threadgroup and a name for this handler should also be specified. Note that changes in the handlers name and running priority using the apropriate Thread methods must occur prior to starting the command handler.

This thread wraps the superclass's generic I/O-Streams into BufferedReader/Writers, first to allow buffering and second to allow character stream reading (remeber our protocol is based on ascii strings).


Nested Class Summary
private  class CommandReceiver.CommandHandler.OneCommandCycle
          A class that deals with the completion of one command cycle.
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
private  List active
          A list of all active commands.
private  BufferedReader buffIn
          The buffered reader wrapped around the socket's input stream.
private  BufferedWriter buffOut
          The print writer wrapped around the socket's output stream.
private  boolean cancel
          Becomes true if the protocol should be terminated.
 
Fields inherited from class stella.net.Handler
in, out, spawner
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected CommandReceiver.CommandHandler(ErrorProcessing spawn, Socket client, ThreadGroup group, String name)
          Constructs a new command-handling thread.
 
Method Summary
 void cancel()
          Indicates that the protocol submission should be terminated.
 void init()
          Wrapps buffered readers/writers around the generic socket streams.
protected  void runProtocol()
          The main method of the command receiver protocol.
 
Methods inherited from class stella.net.Handler
close, run
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, 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
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buffIn

private BufferedReader buffIn
The buffered reader wrapped around the socket's input stream.


buffOut

private BufferedWriter buffOut
The print writer wrapped around the socket's output stream.


cancel

private boolean cancel
Becomes true if the protocol should be terminated.


active

private List active
A list of all active commands.

Constructor Detail

CommandReceiver.CommandHandler

protected CommandReceiver.CommandHandler(ErrorProcessing spawn,
                                         Socket client,
                                         ThreadGroup group,
                                         String name)
Constructs a new command-handling thread. The streams associated to the client-connection socket are opened in the parent class, see Handler.

Parameters:
spawn - A reference to the spawning receiver server.
client - The client-connection socket.
group - The thread group this command handler should run in.
name - The name of the handler thread.
Method Detail

init

public void init()
Wrapps buffered readers/writers around the generic socket streams.

Overrides:
init in class Handler

runProtocol

protected void runProtocol()
The main method of the command receiver protocol. It reads from the input until it encounters a CommandEvent. Then, it sends back acknowledge objects as appropriate counting down the acknowledge number from the initial number returned by CommandReceiver.getAcknowledgeNumber(stella.CommandEvent). As long as calls to the CommandReceiver.processCommand(stella.CommandEvent, int) returns true the acknowledge number is reduced until 1 is reached and a final done event is sent. If a call to CommandReceiver.processCommand(stella.CommandEvent, int) returns false this acknowledge chaining is broken: The error event is fetched with CommandReceiver.getLastError(), sent back to the error listeners, and the command caster is notified with a final, successful, done event (Note that unsuccessful done events are spared for unexpected errors that have no individual error handling procedure).

Specified by:
runProtocol in class Handler

cancel

public void cancel()
Indicates that the protocol submission should be terminated. No terminal done is sent back, this method more or less is a surrogate to the deprechated stop method.