Package edu.caltech.nanodb.client
Class InteractiveClient
- java.lang.Object
-
- edu.caltech.nanodb.client.InteractiveClient
-
- Direct Known Subclasses:
ExclusiveClient
,SharedServerClient
public abstract class InteractiveClient extends java.lang.Object
This abstract class implements the basic functionality necessary for providing an interactive SQL client.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
CMDPROMPT_FIRST
A string constant specifying the "first-line" command-prompt.private static java.lang.String
CMDPROMPT_NEXT
A string constant specifying the "subsequent-lines" command-prompt.private java.lang.StringBuilder
enteredText
The buffer that accumulates each command's text.private boolean
exiting
A flag that records if we are exiting the interactive client.private static org.apache.logging.log4j.Logger
logger
-
Constructor Summary
Constructors Constructor Description InteractiveClient()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private java.lang.String
getCommandString()
This helper method goes through theenteredText
buffer, trying to identify the extent of the next command string.abstract CommandResult
handleCommand(java.lang.String command)
Subclasses can implement this method to handle each command entered by the user.private void
handleShellCommand(java.lang.String shellCommand)
Handle "shell commands," which are commands that the client itself handles on behalf of the user.protected void
mainloop()
This is the interactive mainloop that handles input from the standard input stream of the program.private void
outputCommandResult(CommandResult result)
Outputs relevant information from the command-result object.private void
processEnteredText()
This helper function processes the contents of theenteredText
field, consuming comments, handling client "shell commands" and regular commands that are handled by theNanoDBServer
.abstract void
shutdown()
Shut down the interactive client.abstract void
startup()
Start up the interactive client.
-
-
-
Field Detail
-
logger
private static org.apache.logging.log4j.Logger logger
-
CMDPROMPT_FIRST
private static final java.lang.String CMDPROMPT_FIRST
A string constant specifying the "first-line" command-prompt.- See Also:
- Constant Field Values
-
CMDPROMPT_NEXT
private static final java.lang.String CMDPROMPT_NEXT
A string constant specifying the "subsequent-lines" command-prompt.- See Also:
- Constant Field Values
-
enteredText
private java.lang.StringBuilder enteredText
The buffer that accumulates each command's text.
-
exiting
private boolean exiting
A flag that records if we are exiting the interactive client.
-
-
Method Detail
-
startup
public abstract void startup() throws java.lang.Exception
Start up the interactive client. The specific way the client interacts with the server dictates how this startup mechanism will work.- Throws:
java.lang.Exception
- if any error occurs during startup
-
mainloop
protected void mainloop()
This is the interactive mainloop that handles input from the standard input stream of the program.
-
processEnteredText
private void processEnteredText()
This helper function processes the contents of theenteredText
field, consuming comments, handling client "shell commands" and regular commands that are handled by theNanoDBServer
. Whatever command or comment is processed will also be removed from theenteredText
buffer by this function. Note also that multiple commands will be processed, if present.
-
getCommandString
private java.lang.String getCommandString()
This helper method goes through theenteredText
buffer, trying to identify the extent of the next command string. This is done using semicolons (that are not enclosed with single or double quotes). If a command is identified, it is removed from the internal buffer and returned. If no complete command is identified,null
is returned.- Returns:
- the first semicolon-terminated command in the internal data
buffer, or
null
if the buffer contains no complete commands.
-
handleCommand
public abstract CommandResult handleCommand(java.lang.String command)
Subclasses can implement this method to handle each command entered by the user. For example, a subclass may send the command over a socket to the server, wait for a response, then output the response to the console.- Parameters:
command
- the command to handle.- Returns:
- the command-result from executing the command
-
handleShellCommand
private void handleShellCommand(java.lang.String shellCommand)
Handle "shell commands," which are commands that the client itself handles on behalf of the user. Shell commands start with a backslash "\" character.- Parameters:
shellCommand
- the command to handle.
-
outputCommandResult
private void outputCommandResult(CommandResult result)
Outputs relevant information from the command-result object.- Parameters:
result
- the command-result object to output
-
shutdown
public abstract void shutdown() throws java.lang.Exception
Shut down the interactive client. The specific way the client interacts with the server dictates how this shutdown mechanism will work.- Throws:
java.lang.Exception
- if any error occurs during shutdown
-
-