|
Open Model Railroad Network (OpenMRN)
|
This class provides a console available from stdin/stdout as well as via telnet. More...
#include <Console.hxx>
Classes | |
| struct | Command |
| Console command metadata. More... | |
| class | CommandFlow |
| State flow base class that handles interactive commands. More... | |
| class | Session |
| Console session metadata. More... | |
Public Types | |
| enum | CommandStatus { COMMAND_OK , COMMAND_NEXT , COMMAND_ERROR , COMMAND_CLOSE , COMMAND_NOT_FOUND } |
| Enumeration of recognized command callback results. More... | |
| typedef CommandStatus(* | Callback) (FILE *, int, const char *argv[], void *) |
| Console command callback. | |
Public Member Functions | |
| Console (ExecutorBase *executor, uint16_t port) | |
| Constructor. | |
| Console (ExecutorBase *executor, int fd_in, int fd_out, int port=-1) | |
| Constructor. | |
| ~Console () | |
| Destructor. | |
| void | add_session (int fd_in, int fd_out) |
| Add a console session. | |
| void | add_command (const char *name, Callback callback, void *context=NULL) |
| Add a new command to the console. | |
Public Member Functions inherited from Service | |
| Service (ExecutorBase *e) | |
| Constructor. | |
| ~Service () | |
| Destructor. | |
| ExecutorBase * | executor () |
Static Public Attributes | |
| static const int | FD_STDIN = 0 |
| Default STDIN file descriptor. | |
| static const int | FD_STDOUT = 1 |
| Default STDOUT file descriptor. | |
Private Member Functions | |
| CommandStatus | help_command (FILE *fp, int argc, const char *argv[]) |
| Print out the help menu. | |
| CommandStatus | quit_command (FILE *fp, int argc, const char *argv[]) |
| Quit out of the current login session. | |
| void | open_session (int fd_in, int fd_out) |
| Open and initialize a new session. | |
| DISALLOW_COPY_AND_ASSIGN (Console) | |
Static Private Member Functions | |
| static CommandStatus | help_command (FILE *fp, int argc, const char *argv[], void *context) |
| Print out the help menu by calling the in context helper function. | |
| static CommandStatus | quit_command (FILE *fp, int argc, const char *argv[], void *context) |
| Quit out of the current login session by calling the in context helper function. | |
Private Attributes | |
| Command | help |
| the "help" command instance | |
| Command | helpMark |
| the help "?" command instance | |
Static Private Attributes | |
| static const size_t | MAX_ARGS = 10 |
| Maximum number of supported arguments including the command itself. | |
Friends | |
| class | CommandFlow |
| Give CommandFlow class access to Console private members. | |
| class | Listen |
| Give Listen class access to Console private members. | |
| class | Session |
| Give Session class access to Console private members. | |
This class provides a console available from stdin/stdout as well as via telnet.
Adding a command to the console is as simple as using the add_command() method or instantiating a new derived instance if CommandFlow. A command callback is responsible for providing its own help text. When the command callback is called with argc = 0, this is a clue to the callback to print its help information. In this case, argv[0] will still have the command name itself, but argv[1] will have an additional parameter that can be used to indent and align multi-line help information.
New CommandFlow instances and add_command() calls must occur either in global construction (before main() is called), or within the same executor context as the Console instance they are being attached to in order to avoid a problem with mutual exclusion.
add_command() is used to generate a standard out of context callback when the specified command is entered on the console.
A CommandFlow is specialized in order to generate a state flow based command. This may be useful for interactive commands, but may also be used with non-interactive commands.
Definition at line 67 of file Console.hxx.
| typedef CommandStatus(* Console::Callback) (FILE *, int, const char *argv[], void *) |
Console command callback.
Definition at line 90 of file Console.hxx.
Enumeration of recognized command callback results.
| Enumerator | |
|---|---|
| COMMAND_OK | Command executed successfully. |
| COMMAND_NEXT | Command waiting for input. |
| COMMAND_ERROR | Command had some kind of error. |
| COMMAND_CLOSE | Command wants to close the session. |
| COMMAND_NOT_FOUND | Command not found. |
Definition at line 79 of file Console.hxx.
| Console::Console | ( | ExecutorBase * | executor, |
| uint16_t | port | ||
| ) |
Constructor.
| executor | the executor thread that the Console flows will execute on |
| port | TCP port number to open a telnet listen socket on, -1 mean do not open a listen port. |
Definition at line 54 of file Console.cxx.
| Console::Console | ( | ExecutorBase * | executor, |
| int | fd_in, | ||
| int | fd_out, | ||
| int | port = -1 |
||
| ) |
Constructor.
| executor | the executor thread that the Console flows will execute on |
| fd_in | input file descriptor for the session |
| fd_out | output file descriptor for the session |
| port | TCP port number to open a telnet listen socket on, -1 mean do not open a listen port. |
Definition at line 68 of file Console.cxx.
|
inline |
Destructor.
Definition at line 112 of file Console.hxx.
| void Console::add_command | ( | const char * | name, |
| Callback | callback, | ||
| void * | context = NULL |
||
| ) |
Add a new command to the console.
| name | command name |
| callback | callback function for command |
| context | context pointer to pass into callback |
add the command to the end of the list
Definition at line 94 of file Console.cxx.
|
inline |
Add a console session.
| fd_in | input file descriptor for the session |
| fd_out | output file descriptor for the session |
Definition at line 120 of file Console.hxx.
|
private |
Print out the help menu.
| fp | file pointer to console |
| argc | number of arguments including the command itself |
| argv | array of arguments starting with the command itself |
Definition at line 111 of file Console.cxx.
|
inlinestaticprivate |
Print out the help menu by calling the in context helper function.
| fp | file pointer to console |
| argc | number of arguments including the command itself |
| argv | array of arguments starting with the command itself |
| context | pointer to a Console context |
Definition at line 447 of file Console.hxx.
|
private |
Open and initialize a new session.
| fd_in | input file descriptor belonging to session |
| fd_out | output file descriptor belonging to session |
Definition at line 83 of file Console.cxx.
|
private |
Quit out of the current login session.
| fp | file pointer to console |
| argc | number of arguments including the command itself |
| argv | array of arguments starting with the command itself |
Definition at line 129 of file Console.cxx.
|
inlinestaticprivate |
Quit out of the current login session by calling the in context helper function.
| fp | file pointer to console |
| argc | number of arguments including the command itself |
| argv | array of arguments starting with the command itself |
| context | pointer to a Console context |
Definition at line 468 of file Console.hxx.
|
friend |
Give CommandFlow class access to Console private members.
Definition at line 493 of file Console.hxx.
|
friend |
Give Listen class access to Console private members.
Definition at line 496 of file Console.hxx.
|
friend |
Give Session class access to Console private members.
Definition at line 499 of file Console.hxx.
|
static |
Default STDIN file descriptor.
Definition at line 133 of file Console.hxx.
|
static |
Default STDOUT file descriptor.
Definition at line 136 of file Console.hxx.
|
private |
the "help" command instance
Definition at line 487 of file Console.hxx.
|
private |
the help "?" command instance
Definition at line 488 of file Console.hxx.
|
staticprivate |
Maximum number of supported arguments including the command itself.
Definition at line 267 of file Console.hxx.