libassa
3.5.1
|
#include <GenServer.h>
Public Types | |
enum | LogFlag { KEEPLOG , RMLOG } |
![]() | |
typedef void(* | OPTS_FUNC) (void) |
typedef void(* | OPTS_FUNC_ONE) (const string &) |
typedef vector< Option > | OptionSet |
Public Member Functions | |
GenServer () | |
Constructor. More... | |
virtual | ~GenServer () |
Destructor. More... | |
virtual void | init (int *argc, char *argv[], const char *help_info) |
Provide an entry point into the service and perfom initialization of the service. More... | |
virtual int | fini (void) |
This is an iterface function corresponding to the object moving back into IDLE state. More... | |
virtual int | suspend (void) |
Temporarily suspend the execution of a service. More... | |
virtual int | resume (void) |
Resume execution of a service. More... | |
virtual void | init_service ()=0 |
Interface function provided for derived classes as a place to initialize specifics of derived server. More... | |
virtual void | process_events ()=0 |
Interface function provided for derived classes as the main entry for data processing. More... | |
virtual void | fatal_signal_hook () |
Hook for derived class to do addition clean-up when terminating signal is delivered by OS. More... | |
int | handle_signal (int signum_) |
Handle fatal signals. More... | |
bool | service_is_active () |
Normally called by the main loop to find out whether 'graceful quit' flag had been raised, signaling that some application's component requested to end data processing. More... | |
void | stop_service () |
Inform server that it has to stop data processing, clean up and exit. More... | |
void | set_version (const string &release_, int revision_) |
Set Version and Revision number. More... | |
string | get_version () |
Obtain version information. More... | |
void | set_author (const string &author_) |
Set author's name. More... | |
void | set_flags (LogFlag logf_) |
New debug information is added to the old log file. More... | |
virtual void | display_help () |
List options and invocation syntax to stdout. More... | |
string | get_proc_name () |
Get name of process+instance_number. More... | |
void | set_proc_name (string proc_name_) |
Change process name. More... | |
string | get_cmdline_name () |
Get command-line process name. More... | |
string | get_default_config_file () |
Get default configuration file name: $HOME/. More... | |
string | get_config_file () |
Get alternative configuration file name. More... | |
string | get_port () |
Return assumed name of the listening port. More... | |
void | set_port (string port_) |
Set listening port name. More... | |
SigHandlers & | get_sig_manager () |
Obtain reference to the Signal Manager, class SigHandls. More... | |
Reactor * | get_reactor () |
Obtain reference to the Reactor. More... | |
int | get_exit_value () const |
Retrieve exit value of the process. More... | |
![]() | |
EventHandler () | |
Constructor. More... | |
virtual | ~EventHandler () |
Virtual destructor. More... | |
virtual int | handle_read (int fd) |
Read event callback. More... | |
virtual int | handle_write (int fd) |
Write handler callback. More... | |
virtual int | handle_except (int fd) |
Exception handler callback. More... | |
virtual int | handle_timeout (TimerId tid) |
Timeout handler callback. More... | |
virtual int | handle_close (int fd) |
EOF on peer socket handler callback. More... | |
virtual void | resetState (void) |
A hook for derived class to reset internal state as needed. More... | |
void | set_id (const std::string &id_) |
Set EventHandler ID. More... | |
std::string | get_id () const |
Retrieve EventHandler ID. More... | |
![]() | |
CmdLineOpts () | |
Default constructor. More... | |
virtual | ~CmdLineOpts () |
Do-nothing destructor. More... | |
bool | add_flag_opt (const char c, const string &s, bool *f) |
Add binary flag option. More... | |
bool | add_opt (const char c, const string &s, string *str) |
Add an option with STL string argument. More... | |
bool | add_opt (const char c, const string &s, int *i) |
Add an option with integer argument. More... | |
bool | add_opt (const char c, const string &s, unsigned int *ui) |
Add an option with unsigned integer argument. More... | |
bool | add_opt (const char c, const string &s, long *l) |
Add an option with long argument. More... | |
bool | add_opt (const char c, const string &s, unsigned long *ul) |
Add an option with unsigned long argument. More... | |
bool | add_opt (const char c, const string &s, double *d) |
Add an option with double argument. More... | |
bool | add_opt (const char c, const string &s, float *f) |
Add an option with float argument. More... | |
bool | add_opt (const char c_, const string &s_, OPTS_FUNC f_) |
Add an option with static function argument. More... | |
bool | add_opt (const char c_, const string &s_, OPTS_FUNC_ONE f_) |
Add an option with static function argument. More... | |
bool | rm_opt (const char c_, const string &s_) |
Remove option for the option list. More... | |
bool | parse_args (const char *argv[]) |
Parse command line arguments based on installed options set. More... | |
int | parse_config_file (IniFile &inifile_) |
Parse configuration parameters found in [options] section of the INI file. More... | |
const char * | get_opt_error () const |
If previous call to one of member functions returned false, retrieve detailed error message. More... | |
void | dump () const |
Write options set to the log file. More... | |
Static Public Member Functions | |
static bool | become_daemon () |
Become a daemon process. More... | |
![]() | |
static void | str_to_argv (const string &src_, int &argc_, char **&argv_) |
Static function. More... | |
static void | free_argv (char **&argv_) |
Free up memory allocated by str_to_argv() function More... | |
Protected Member Functions | |
void | set_exit_value (int v_) |
Set exit value of the process. This value is returned to the shell. More... | |
![]() | |
bool | is_valid (const char sopt_, const string &lopt_) |
Detect if supplied option is valid. More... | |
void | set_error_none () |
Reset error message to an empty string. More... | |
bool | assign (Option *node_, const char *op_) |
Perform value assignment to the node. More... | |
Option * | find_option (const char *str_) |
Locate option in the options set. More... | |
Option * | find_option (const char letter_) |
Locate option in the options set. More... | |
virtual void | pos_arg (const char *arg_) |
Process positional argument arg_. More... | |
Protected Attributes | |
string | m_proc_name |
process name (considering instance_number) More... | |
string | m_cmdline_name |
process name as appeared on command line More... | |
string | m_port |
listening port name More... | |
string | m_default_config_file |
standard configuration file name More... | |
string | m_config_file |
alternative configuration file name More... | |
u_int | m_log_size |
Max size of the log file. More... | |
int | m_instance |
Process instance. More... | |
string | m_log_file |
Full pathname of debug file. More... | |
string | m_with_log_server |
If 'yes', send log messages to the log server. More... | |
string | m_log_server |
Log server, assa-logd, address (port@host) More... | |
long | m_mask |
Debug file mask to filter debug/error messages. More... | |
bool | m_graceful_quit |
Flag that indicates wheather server outgh to stop and exit. More... | |
SigHandlers | m_sig_dispatcher |
Signal handlers dispatcher. More... | |
SIGPOLLHandler | m_sig_poll |
Function that swallows SIGPOLL calls. More... | |
Reactor | m_reactor |
GenServer object has its very own personal Reactor object. More... | |
string | m_version |
Software version. More... | |
int | m_revision |
Software revision (patch) level. More... | |
string | m_author |
Author's name. More... | |
const char * | m_help_msg |
Help information. More... | |
LogFlag | m_log_flag |
Log file initialization flag. If RM_LOG, remove old log file. More... | |
string | m_log_stdout |
If 'yes', redirects all logging messages to std::cerr. More... | |
string | m_daemon |
Daemon option flag. If 'yes', become a UNIX daemon process. More... | |
string | m_ommit_pidfile |
If 'yes', skip PID file locking creation/locking step. More... | |
int | m_log_level |
Logging level - an integer number that incrementally increases verbosity of the looing messages. More... | |
PidFileLock | m_pidfile_lock |
PID File lock. More... | |
string | m_pidfile |
PID File lock path name. More... | |
bool | m_help_flag |
Help option flag. More... | |
bool | m_version_flag |
Version option flag. More... | |
int | m_exit_value |
Exit value of the process. More... | |
![]() | |
std::string | m_id |
Private Member Functions | |
GenServer (const GenServer &) | |
No cloning. More... | |
GenServer & | operator= (const GenServer &) |
void | init_internals () |
Initialize internals. More... | |
Definition at line 59 of file GenServer.h.
Enumerator | |
---|---|
KEEPLOG | By default, append new log records to the existing log file. This is operational mode. |
RMLOG | Remove existing log file and start afresh. Convenient during development phase. |
Definition at line 66 of file GenServer.h.
GenServer::GenServer | ( | ) |
Constructor.
Corresponds to the object entering the IDLE
state.
Form a valid log server address
Definition at line 56 of file GenServer.cpp.
References ASSA::CmdLineOpts::add_flag_opt(), ASSA::CmdLineOpts::add_opt(), m_config_file, m_daemon, m_help_flag, m_instance, m_log_file, m_log_level, m_log_server, m_log_size, m_log_stdout, m_mask, m_ommit_pidfile, m_pidfile, m_port, m_version_flag, and m_with_log_server.
|
virtual |
Destructor.
Reactor needs to detach itself from the Logger before releasing memory.
Otherwise, a race condition between Logger (singleton) and GenServer (singleton) might yield core dump if Reactor was destroyed before Logger. Since Reactor is attached to the Logger with Logger::log_open () for the assa-logd connection, it is Reactor's responsibility to detach first. But, we only care about GenServer's Reactor. All others (such as those used by Connector and Acceptor classes) should not.
Definition at line 111 of file GenServer.cpp.
References ASSA::Log::log_close().
|
private |
No cloning.
|
static |
Become a daemon process.
Definition at line 357 of file GenServer.cpp.
References ASSA::Fork::IGNORE_STATUS, ASSA::Fork::isChild(), and ASSA::Fork::LEAVE_ALONE.
Referenced by init().
|
inlinevirtual |
List options and invocation syntax to stdout.
Definition at line 374 of file GenServer.h.
References ASSA::endl(), m_author, and m_help_msg.
Referenced by init().
|
inlinevirtual |
Hook for derived class to do addition clean-up when terminating signal is delivered by OS.
Note that signal handling is provided by default and no additional intervention is necessary. Use this method only to enhance it.
Definition at line 135 of file GenServer.h.
Referenced by handle_signal().
|
inlinevirtual |
This is an iterface function corresponding to the object moving back into IDLE state.
Derived class is expected to perform actions that terminate execution of the service.
Definition at line 107 of file GenServer.h.
|
inline |
Get command-line process name.
Definition at line 190 of file GenServer.h.
References m_cmdline_name.
Referenced by init_internals().
|
inline |
Get alternative configuration file name.
This name is specified as command-line argument '-f'
Definition at line 203 of file GenServer.h.
References m_config_file.
|
inline |
Get default configuration file name: $HOME/.
{command_line_name}.cfg If you want your configuration file name to be different, change the value of m_std_config_name in derived class
Definition at line 198 of file GenServer.h.
References m_default_config_file.
|
inline |
Retrieve exit value of the process.
Definition at line 227 of file GenServer.h.
References m_exit_value.
|
inline |
Return assumed name of the listening port.
Definition at line 206 of file GenServer.h.
References m_port.
|
inline |
Get name of process+instance_number.
Definition at line 182 of file GenServer.h.
References m_proc_name.
Referenced by init_internals().
|
inline |
Obtain reference to the Reactor.
Definition at line 221 of file GenServer.h.
References m_reactor.
Referenced by handle_signal(), and init_internals().
|
inline |
Obtain reference to the Signal Manager, class SigHandls.
Definition at line 216 of file GenServer.h.
References m_sig_dispatcher.
|
inline |
Obtain version information.
Definition at line 365 of file GenServer.h.
References ASSA::ends(), m_revision, and m_version.
Referenced by init().
|
virtual |
Handle fatal signals.
Hook (e.g. fatalSignalHook) is provided if derived class needs extra work before falling dead.
Reimplemented from ASSA::EventHandler.
Definition at line 408 of file GenServer.cpp.
References ASSA::APP, ASSA::Reactor::deactivate(), DL, ASSA::ends(), fatal_signal_hook(), get_reactor(), m_graceful_quit, and trace.
|
virtual |
Provide an entry point into the service and perfom initialization of the service.
Open log file and log startup options. Process standard command-line arguments. Following signals are handled in uniform manner: SIGHUP, SIGPIPE, SIGCHLD, SIGCLD, SIGALRM, SIGINT, SIGPOLL, SIGTERM.
This function corresponds to the object moving from IDLE to RUNNING state as result of service initialization, or reconfiguration of the service and remaining in RUNNING state.
argc | Pointer to number of command line arguments |
argv | Command line arguments char* array |
help_info | Title that will be displayed with -h option |
Solaris x86 whole path is returned. Scan through the path and get the process name.
Convert relative paths of all filepath options to absolute paths.
Daemonize the process if asked
Setting defaults if required
Setup signal handling. Ignore SIGHUP, SIGPIPE, SIGCHLD, SIGCLD, SIGALRM by default.
SIGHUP is generated by terminal driver (see termio(7I) for details) in response to modem hangup (or closing terminal session). I ignore it here with the assumption that GenServer is alway a daemon process that doesn't have associated controlling terminal anyway.
Catch SIGPOLL - sigPOLL handler just does nothing except of catching signal.
SIGINT is generated by the terminal driver when an interrupt key is pressed (DELETE or Ctrl-C). It is sent to all processes associated with the controlling terminal. We terminate process in this case.
Catch and handle SIGTERM signals. is the termination signal sent by kill command by default or internally as a part of fatal application exception handling to properly terminate GenServer process.
Initialize other internal stuff.
Definition at line 123 of file GenServer.cpp.
References ASSA_DIR_SEPARATOR, ASSAIOSIG, become_daemon(), display_help(), ASSA::endl(), ASSA::CmdLineOpts::get_opt_error(), get_version(), init_internals(), ASSA::SigHandlers::install(), m_author, m_cmdline_name, m_config_file, m_daemon, m_default_config_file, m_help_flag, m_help_msg, m_instance, m_log_file, m_pidfile, m_port, m_proc_name, m_sig_dispatcher, m_sig_poll, m_version_flag, ASSA::CmdLineOpts::parse_args(), ASSA::SigAction::register_action(), and ASSA::Utils::strenv().
|
private |
Initialize internals.
Set standard configuration file name. For POSIX systems, it is $HOME/.procname. For Win32, it is $cwd/procname.ini.
Remove existing log file if requested. Unlinking /dev/null character device and replacing it with a regular file leads to the system crash during consecutive reboots. See also assa/FileLogger.cpp.
Open logging facility:
–log-stdout="yes" takes precedence over –with-log-server="yes" which takes precedence over –log-file=/path/to/log
Definition at line 270 of file GenServer.cpp.
References ASSA::APP, ASSA::ASSAERR, DL, ASSA::CmdLineOpts::dump(), get_cmdline_name(), ASSA::PidFileLock::get_error_msg(), get_proc_name(), get_reactor(), ASSA::PidFileLock::lock(), m_cmdline_name, m_config_file, m_default_config_file, m_log_file, m_log_flag, m_log_server, m_log_size, m_log_stdout, m_mask, m_ommit_pidfile, m_pidfile, m_pidfile_lock, m_proc_name, m_with_log_server, ASSA::Log::open_log_file(), ASSA::Log::open_log_server(), ASSA::Log::open_log_stdout(), RMLOG, ASSA::Log::set_app_name(), ASSA::Utils::strenv(), and trace.
Referenced by init().
|
pure virtual |
Interface function provided for derived classes as a place to initialize specifics of derived server.
|
pure virtual |
Interface function provided for derived classes as the main entry for data processing.
This is the place to implement main event loop.
|
inlinevirtual |
Resume execution of a service.
Corresponds to the process returning back to RUNNING state from SUSPENDED state.
Definition at line 117 of file GenServer.h.
|
inline |
Normally called by the main loop to find out whether 'graceful quit' flag had been raised, signaling that some application's component requested to end data processing.
Definition at line 149 of file GenServer.h.
References m_graceful_quit.
|
inline |
|
inlineprotected |
Set exit value of the process. This value is returned to the shell.
Definition at line 231 of file GenServer.h.
References m_exit_value.
|
inline |
New debug information is added to the old log file.
To erase old log file, set flag to RMLOG.
logf_ | Defaulted to KEEPLOG that adds log records to the existing log file; RMLOG - remove existing log file and start afresh. |
Definition at line 176 of file GenServer.h.
References m_log_flag.
|
inline |
Set listening port name.
port_ | new listening port name |
Definition at line 211 of file GenServer.h.
References m_port.
|
inline |
Change process name.
proc_name_ | new process name |
Definition at line 187 of file GenServer.h.
References m_proc_name.
|
inline |
Set Version and Revision number.
release_ | Release number. |
revision_ | Patch level. |
Definition at line 350 of file GenServer.h.
References m_revision, and m_version.
|
inline |
Inform server that it has to stop data processing, clean up and exit.
This method will also stop internal Reactor.
Definition at line 342 of file GenServer.h.
References ASSA::Reactor::deactivate(), m_graceful_quit, and m_reactor.
|
inlinevirtual |
Temporarily suspend the execution of a service.
Corresponds to process leaving RUNNING state and entering SUSPENDED state.
Definition at line 112 of file GenServer.h.
|
protected |
Author's name.
Definition at line 289 of file GenServer.h.
Referenced by display_help(), init(), and set_author().
|
protected |
process name as appeared on command line
Definition at line 238 of file GenServer.h.
Referenced by get_cmdline_name(), init(), and init_internals().
|
protected |
alternative configuration file name
Definition at line 247 of file GenServer.h.
Referenced by GenServer(), get_config_file(), init(), and init_internals().
|
protected |
Daemon option flag. If 'yes', become a UNIX daemon process.
Definition at line 301 of file GenServer.h.
Referenced by GenServer(), and init().
|
protected |
standard configuration file name
Definition at line 244 of file GenServer.h.
Referenced by get_default_config_file(), init(), and init_internals().
|
protected |
Exit value of the process.
Definition at line 329 of file GenServer.h.
Referenced by get_exit_value(), and set_exit_value().
|
protected |
Flag that indicates wheather server outgh to stop and exit.
Definition at line 269 of file GenServer.h.
Referenced by handle_signal(), service_is_active(), and stop_service().
|
protected |
Help option flag.
If true, [-h, –help] option is being specified on command line.
Definition at line 321 of file GenServer.h.
Referenced by GenServer(), and init().
|
protected |
Help information.
Definition at line 292 of file GenServer.h.
Referenced by display_help(), and init().
|
protected |
Process instance.
Definition at line 253 of file GenServer.h.
Referenced by GenServer(), and init().
|
protected |
Full pathname of debug file.
Definition at line 256 of file GenServer.h.
Referenced by GenServer(), init(), and init_internals().
|
protected |
Log file initialization flag. If RM_LOG, remove old log file.
Definition at line 295 of file GenServer.h.
Referenced by init_internals(), and set_flags().
|
protected |
Logging level - an integer number that incrementally increases verbosity of the looing messages.
The exact meaning of each level is application-specific.
Definition at line 310 of file GenServer.h.
Referenced by GenServer().
|
protected |
Log server, assa-logd, address (port@host)
Definition at line 263 of file GenServer.h.
Referenced by GenServer(), and init_internals().
|
protected |
Max size of the log file.
Definition at line 250 of file GenServer.h.
Referenced by GenServer(), and init_internals().
|
protected |
If 'yes', redirects all logging messages to std::cerr.
Definition at line 298 of file GenServer.h.
Referenced by GenServer(), and init_internals().
|
protected |
Debug file mask to filter debug/error messages.
Definition at line 266 of file GenServer.h.
Referenced by GenServer(), and init_internals().
|
protected |
If 'yes', skip PID file locking creation/locking step.
Definition at line 304 of file GenServer.h.
Referenced by GenServer(), and init_internals().
|
protected |
PID File lock path name.
Definition at line 316 of file GenServer.h.
Referenced by GenServer(), init(), and init_internals().
|
protected |
|
protected |
listening port name
Definition at line 241 of file GenServer.h.
Referenced by GenServer(), get_port(), init(), and set_port().
|
protected |
process name (considering instance_number)
Definition at line 235 of file GenServer.h.
Referenced by get_proc_name(), init(), init_internals(), and set_proc_name().
|
protected |
GenServer object has its very own personal Reactor object.
Definition at line 280 of file GenServer.h.
Referenced by get_reactor(), and stop_service().
|
protected |
Software revision (patch) level.
Definition at line 286 of file GenServer.h.
Referenced by get_version(), and set_version().
|
protected |
Signal handlers dispatcher.
Definition at line 273 of file GenServer.h.
Referenced by get_sig_manager(), and init().
|
protected |
Function that swallows SIGPOLL calls.
Definition at line 276 of file GenServer.h.
Referenced by init().
|
protected |
Software version.
Definition at line 283 of file GenServer.h.
Referenced by get_version(), and set_version().
|
protected |
Version option flag.
If true, [-v, –version] options is being specified on command line.
Definition at line 326 of file GenServer.h.
Referenced by GenServer(), and init().
|
protected |
If 'yes', send log messages to the log server.
Definition at line 259 of file GenServer.h.
Referenced by GenServer(), and init_internals().