org.aitools.programd.interfaces.shell
Class Shell

java.lang.Object
  extended by java.lang.Thread
      extended by org.aitools.programd.interfaces.shell.Shell
All Implemented Interfaces:
java.lang.Runnable

public class Shell
extends java.lang.Thread

Provides a simple shell for interacting with the bot at a command line.

Author:
Jon Baer, Noel Bush, Eion Robb

Nested Class Summary
private  class Shell.NoCommandException
          An exception thrown if no command is specified.
private  class Shell.NoSuchCommandableException
          An exception thrown if an invalid commandable is specified.
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private  java.lang.String botid
          A bot id.
private  java.lang.String botName
          A bot name.
private  java.lang.String botNamePredicate
          The bot name predicate.
private  Bots bots
          The Bots object in use by the attached Core.
private  java.lang.String clientNamePredicate
          The client name predicate.
private  ShellCommandRegistry commandRegistry
          The command registry.
private  java.io.PrintStream consolePrompt
          Where console prompt output will go.
private  Core core
          The Core to which this Shell is attached.
private  java.io.PrintStream errStream
          Where console errors will go.
private static java.lang.String EXIT
          Shell exit command.
private  java.lang.String hostname
          The host name.
private  java.io.BufferedReader inReader
          A BufferedReader for user input to the shell.
private  boolean midLine
          An indicator used to keep track of whether we're midline in a console output (i.e., showing a prompt).
private  java.io.PrintStream outStream
          Where regular console output will go.
private  PredicateMaster predicateMaster
          The PredicateMaster in use by the attached Core.
static java.lang.String PROMPT
          The string to use for an interactive console.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Shell()
          A Shell with default input and output streams ( System.in and System.out).
Shell(java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err, java.io.PrintStream prompt)
          A Shell with custom input and output streams.
 
Method Summary
 void attachTo(Core coreToUse)
          Attach this shell to the given core.
private  void commandCommandable(java.lang.String command)
          Sends a command to a shell commandable, if possible.
 Bots getBots()
           
 java.util.Collection<ShellCommand> getCommands()
           
 Core getCore()
           
 java.lang.String getCurrentBotID()
           
 void gotLine()
          Tells the Shell that something else was printed to the console; not midLine anymore.
private  void noShell()
          Notes that the shell will not run, and sleeps.
private  void printExitMessage()
          Prints an exit message.
 void printlnErr(java.lang.String message)
          Print a message line of error to the console.
 void printlnOut(java.lang.String message)
          Print a message line of standard output to the console.
 void processCommandLine(java.lang.String commandLine)
          Allows an external class to call a command by sending a command line.
private  void promptConsole(java.lang.String preprompt)
           Displays a line for an interactive console, including the prompt.
 void run()
          Runs the shell.
private  void showConsole(java.lang.String preprompt, java.lang.String[] message)
           Displays a multi-line message (after a prompt) in an interactive console.
 void showError(java.lang.String message)
           Displays an error message (no prompt) in an interactive console.
 void showMessage(java.lang.String message)
           Displays a regular message (no prompt) in an interactive console.
private  void showPrompt()
          Displays a prompt.
 void switchToBot(java.lang.String newBotID)
          Switches to a bot, given an id.
 
Methods inherited from class java.lang.Thread
activeCount, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, checkAccess, 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

PROMPT

public static final java.lang.String PROMPT
The string to use for an interactive console.

See Also:
Constant Field Values

EXIT

private static final java.lang.String EXIT
Shell exit command.

See Also:
Constant Field Values

commandRegistry

private ShellCommandRegistry commandRegistry
The command registry.


core

private Core core
The Core to which this Shell is attached.


predicateMaster

private PredicateMaster predicateMaster
The PredicateMaster in use by the attached Core.


bots

private Bots bots
The Bots object in use by the attached Core.


inReader

private java.io.BufferedReader inReader
A BufferedReader for user input to the shell.


outStream

private java.io.PrintStream outStream
Where regular console output will go.


errStream

private java.io.PrintStream errStream
Where console errors will go.


consolePrompt

private java.io.PrintStream consolePrompt
Where console prompt output will go.


botid

private java.lang.String botid
A bot id.


botName

private java.lang.String botName
A bot name.


clientNamePredicate

private java.lang.String clientNamePredicate
The client name predicate.


botNamePredicate

private java.lang.String botNamePredicate
The bot name predicate.


hostname

private java.lang.String hostname
The host name.


midLine

private boolean midLine
An indicator used to keep track of whether we're midline in a console output (i.e., showing a prompt).

Constructor Detail

Shell

public Shell()
A Shell with default input and output streams ( System.in and System.out).


Shell

public Shell(java.io.InputStream in,
             java.io.PrintStream out,
             java.io.PrintStream err,
             java.io.PrintStream prompt)
A Shell with custom input and output streams.

Parameters:
in - the input stream
out - the output stream
err - the error stream
prompt - the prompt output stream
Method Detail

attachTo

public void attachTo(Core coreToUse)
Attach this shell to the given core.

Parameters:
coreToUse -

run

public void run()
Runs the shell.

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

noShell

private void noShell()
Notes that the shell will not run, and sleeps.


processCommandLine

public void processCommandLine(java.lang.String commandLine)
                        throws NoSuchCommandException
Allows an external class to call a command by sending a command line. Prints the command line to the console so it's possible to see what was attempted.

Parameters:
commandLine - the command line to process
Throws:
NoSuchCommandException - if the command line did not contain a command that could be processed

showPrompt

private void showPrompt()
Displays a prompt.


promptConsole

private void promptConsole(java.lang.String preprompt)

Displays a line for an interactive console, including the prompt.

Parameters:
preprompt - the text to show before the prompt

showMessage

public void showMessage(java.lang.String message)

Displays a regular message (no prompt) in an interactive console.

Parameters:
message - the message to display

showError

public void showError(java.lang.String message)

Displays an error message (no prompt) in an interactive console.

Parameters:
message - the message to display

showConsole

private void showConsole(java.lang.String preprompt,
                         java.lang.String[] message)

Displays a multi-line message (after a prompt) in an interactive console.

Parameters:
preprompt - the text to show before the prompt
message - the multi-line message to display

printlnOut

public void printlnOut(java.lang.String message)
Print a message line of standard output to the console.

Parameters:
message - the message to print

printlnErr

public void printlnErr(java.lang.String message)
Print a message line of error to the console.

Parameters:
message - the message to print

gotLine

public void gotLine()
Tells the Shell that something else was printed to the console; not midLine anymore.


printExitMessage

private void printExitMessage()
Prints an exit message.


getCurrentBotID

public java.lang.String getCurrentBotID()
Returns:
the current bot id

getCore

public Core getCore()
Returns:
the Core in use

getCommands

public java.util.Collection<ShellCommand> getCommands()
Returns:
the command registry

getBots

public Bots getBots()
Returns:
the Bots object used by this shell

switchToBot

public void switchToBot(java.lang.String newBotID)
Switches to a bot, given an id.

Parameters:
newBotID -

commandCommandable

private void commandCommandable(java.lang.String command)
                         throws Shell.NoCommandException,
                                Shell.NoSuchCommandableException
Sends a command to a shell commandable, if possible.

Parameters:
command - the command (including the shell commandable name)
Throws:
Shell.NoCommandException - if no command is given
Shell.NoSuchCommandableException - if an invalid commandable is specified