org.estar.miat.tcpip
Class MiatTCPIPControlServerThread

java.lang.Object
  extended by org.estar.miat.MiatBaseImplementation
      extended by org.estar.miat.tcpip.MiatTCPIPControlServerThread
All Implemented Interfaces:
java.lang.Runnable, MiatInterface, MiatThreadInterface, MiatTCPIPControlServerThreadInterface

public class MiatTCPIPControlServerThread
extends MiatBaseImplementation
implements MiatTCPIPControlServerThreadInterface, java.lang.Runnable

My Intelligent Agent Toolkit. Module that runs a Socket server on a thread. Listens for incoming connections and spawns an instance of an inner thread to handle them. Reads input, and then finds an appropriate command implementor and invokes it.

Module Dependancies:

org.estar.miat.MiatLoggerInterface
For logging.
org.estar.miat.tcpip.MiatTCPIPControlServerThreadInterface
This interface.

Version:
$Revision$
Author:
Chris Mottram

Nested Class Summary
protected  class MiatTCPIPControlServerThread.ControlServerConnectionThread
          Class implementing a connection from the control server socket.
 
Field Summary
protected  java.util.Hashtable commandMap
          A hashtable containing the mapping from command name's to their implementation classes.
protected  MiatLoggerInterface logger
          Logger to use.
protected  int logLevel
          The log level to supply to logger.log for this instance.
protected  int portNumber
          The port the control server is running on.
protected  boolean quit
          Set to true to stop the thread.
static java.lang.String RCSID
          Revision control system version id.
protected  java.net.ServerSocket serverSocket
          The server socket.
 
Fields inherited from class org.estar.miat.MiatBaseImplementation
id, interfaceRegistry, name
 
Constructor Summary
MiatTCPIPControlServerThread()
          Default Constructor.
 
Method Summary
 void addCommand(java.lang.String commandName, MiatTCPIPControlServerCommandInterface commandImplementation)
          Method called by MiatTCPIPControlServerCommandInterface implementor's initialise methods, to register themselves as a command that can be invoked when someone telnets into the TCP/IP control socket.
protected  java.lang.String doControlCommand(java.lang.String[] args)
          This method is called when a command is read from a connection spawned from the control port.
protected  java.lang.String help()
          Print out a help string.
protected  java.lang.String help(java.lang.String commandName)
          Print out a help string for the specified command.
 void initialise()
          Initialise method.
 void quit()
          Quit the server socket.
 void run()
          The run method.
protected  void startConnectionThread(java.net.Socket s)
          Start a connection thread using the specified socket for communication.
 
Methods inherited from class org.estar.miat.MiatBaseImplementation
getId, setId, setInterfaceRegistry, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.estar.miat.MiatInterface
getId, setId, setInterfaceRegistry, setName
 

Field Detail

RCSID

public static final java.lang.String RCSID
Revision control system version id.

See Also:
Constant Field Values

logger

protected MiatLoggerInterface logger
Logger to use.

See Also:
MiatLoggerInterface

logLevel

protected int logLevel
The log level to supply to logger.log for this instance.


portNumber

protected int portNumber
The port the control server is running on.


quit

protected boolean quit
Set to true to stop the thread.


serverSocket

protected java.net.ServerSocket serverSocket
The server socket.


commandMap

protected java.util.Hashtable commandMap
A hashtable containing the mapping from command name's to their implementation classes.

Constructor Detail

MiatTCPIPControlServerThread

public MiatTCPIPControlServerThread()
Default Constructor. Initialises commandMap here.

See Also:
commandMap
Method Detail

initialise

public void initialise()
                throws java.lang.Exception
Initialise method. Called after all interface s have been loaded/registered. Initialise logger.

Specified by:
initialise in interface MiatInterface
Overrides:
initialise in class MiatBaseImplementation
Throws:
java.lang.Exception - Thrown if an error occurs.
See Also:
logLevel, logger, portNumber

addCommand

public void addCommand(java.lang.String commandName,
                       MiatTCPIPControlServerCommandInterface commandImplementation)
                throws java.lang.Exception
Method called by MiatTCPIPControlServerCommandInterface implementor's initialise methods, to register themselves as a command that can be invoked when someone telnets into the TCP/IP control socket.

Specified by:
addCommand in interface MiatTCPIPControlServerThreadInterface
Parameters:
commandName - The name of the command to register as.
commandImplementation - The implementation of that command.
Throws:
java.lang.IllegalArgumentException - Thrown if a command implementor is already registered for this command.
java.lang.Exception
See Also:
commandMap

run

public void run()
The run method. Sets up the server socket. Enter a while loop until quit is true. Gets a connection socket, and calls startConnectionThread to start a connection thread to handle the connection.

Specified by:
run in interface java.lang.Runnable
Specified by:
run in interface MiatThreadInterface
See Also:
serverSocket, quit, startConnectionThread(java.net.Socket)

quit

public void quit()
          throws java.io.IOException
Quit the server socket. Set quit to true, and close the server socket (if it exists).

Specified by:
quit in interface MiatTCPIPControlServerThreadInterface
Throws:
java.io.IOException - Can be thrown when closing the server socket.
See Also:
quit, serverSocket

help

protected java.lang.String help()
Print out a help string.

See Also:
commandMap, help(java.lang.String)

help

protected java.lang.String help(java.lang.String commandName)
Print out a help string for the specified command.

Parameters:
commandName - The name of the command.
See Also:
commandMap

startConnectionThread

protected void startConnectionThread(java.net.Socket s)
Start a connection thread using the specified socket for communication.

Parameters:
s - The socket to communicate over.
See Also:
MiatTCPIPControlServerThread.ControlServerConnectionThread

doControlCommand

protected java.lang.String doControlCommand(java.lang.String[] args)
This method is called when a command is read from a connection spawned from the control port.

Parameters:
args - The command arguments (the command itself is args[0]).
Returns:
A string describing the results of the command.
See Also:
logLevel, commandMap, quit, help()