Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Console Class Reference

This class provides a console available from stdin/stdout as well as via telnet. More...

#include <Console.hxx>

Inheritance diagram for Console:
Service

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.
 
ExecutorBaseexecutor ()
 

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.
 

Detailed Description

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.

Member Typedef Documentation

◆ Callback

typedef CommandStatus(* Console::Callback) (FILE *, int, const char *argv[], void *)

Console command callback.

Definition at line 90 of file Console.hxx.

Member Enumeration Documentation

◆ CommandStatus

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.

Constructor & Destructor Documentation

◆ Console() [1/2]

Console::Console ( ExecutorBase executor,
uint16_t  port 
)

Constructor.

Parameters
executorthe executor thread that the Console flows will execute on
portTCP 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() [2/2]

Console::Console ( ExecutorBase executor,
int  fd_in,
int  fd_out,
int  port = -1 
)

Constructor.

Parameters
executorthe executor thread that the Console flows will execute on
fd_ininput file descriptor for the session
fd_outoutput file descriptor for the session
portTCP 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.

◆ ~Console()

Console::~Console ( )
inline

Destructor.

Definition at line 112 of file Console.hxx.

Member Function Documentation

◆ add_command()

void Console::add_command ( const char *  name,
Callback  callback,
void *  context = NULL 
)

Add a new command to the console.

Parameters
namecommand name
callbackcallback function for command
contextcontext pointer to pass into callback

add the command to the end of the list

Definition at line 94 of file Console.cxx.

◆ add_session()

void Console::add_session ( int  fd_in,
int  fd_out 
)
inline

Add a console session.

Parameters
fd_ininput file descriptor for the session
fd_outoutput file descriptor for the session

Definition at line 120 of file Console.hxx.

◆ help_command() [1/2]

Console::CommandStatus Console::help_command ( FILE *  fp,
int  argc,
const char *  argv[] 
)
private

Print out the help menu.

Parameters
fpfile pointer to console
argcnumber of arguments including the command itself
argvarray of arguments starting with the command itself
Returns
COMMAND_OK

Definition at line 111 of file Console.cxx.

◆ help_command() [2/2]

static CommandStatus Console::help_command ( FILE *  fp,
int  argc,
const char *  argv[],
void *  context 
)
inlinestaticprivate

Print out the help menu by calling the in context helper function.

Parameters
fpfile pointer to console
argcnumber of arguments including the command itself
argvarray of arguments starting with the command itself
contextpointer to a Console context
Returns
COMMAND_OK

Definition at line 447 of file Console.hxx.

◆ open_session()

void Console::open_session ( int  fd_in,
int  fd_out 
)
private

Open and initialize a new session.

Parameters
fd_ininput file descriptor belonging to session
fd_outoutput file descriptor belonging to session

Definition at line 83 of file Console.cxx.

◆ quit_command() [1/2]

Console::CommandStatus Console::quit_command ( FILE *  fp,
int  argc,
const char *  argv[] 
)
private

Quit out of the current login session.

Parameters
fpfile pointer to console
argcnumber of arguments including the command itself
argvarray of arguments starting with the command itself
Returns
COMMAND_OK

Definition at line 129 of file Console.cxx.

◆ quit_command() [2/2]

static CommandStatus Console::quit_command ( FILE *  fp,
int  argc,
const char *  argv[],
void *  context 
)
inlinestaticprivate

Quit out of the current login session by calling the in context helper function.

Parameters
fpfile pointer to console
argcnumber of arguments including the command itself
argvarray of arguments starting with the command itself
contextpointer to a Console context
Returns
COMMAND_OK

Definition at line 468 of file Console.hxx.

Friends And Related Symbol Documentation

◆ CommandFlow

friend class CommandFlow
friend

Give CommandFlow class access to Console private members.

Definition at line 493 of file Console.hxx.

◆ Listen

friend class Listen
friend

Give Listen class access to Console private members.

Definition at line 496 of file Console.hxx.

◆ Session

friend class Session
friend

Give Session class access to Console private members.

Definition at line 499 of file Console.hxx.

Member Data Documentation

◆ FD_STDIN

const int Console::FD_STDIN = 0
static

Default STDIN file descriptor.

Definition at line 133 of file Console.hxx.

◆ FD_STDOUT

const int Console::FD_STDOUT = 1
static

Default STDOUT file descriptor.

Definition at line 136 of file Console.hxx.

◆ help

Command Console::help
private

the "help" command instance

Definition at line 487 of file Console.hxx.

◆ helpMark

Command Console::helpMark
private

the help "?" command instance

Definition at line 488 of file Console.hxx.

◆ MAX_ARGS

const size_t Console::MAX_ARGS = 10
staticprivate

Maximum number of supported arguments including the command itself.

Definition at line 267 of file Console.hxx.


The documentation for this class was generated from the following files: