Loading...
Searching...
No Matches
ASSA::GenServer Class Referenceabstract

`#include <GenServer.h>`

Inheritance diagram for ASSA::GenServer:

## Public Types

enum | LogFlag { KEEPLOG , RMLOG }
Public Types inherited from ASSA::CmdLineOpts
typedef void(* | OPTS_FUNC) (void)
typedef void(* | OPTS_FUNC_ONE) (const string &)
typedef vector< Option > | OptionSet

## Public Member Functions

| GenServer ()
| Constructor.
virtual | ~GenServer ()
| Destructor.
virtual void | init (int *argc, char *argv[], const char *help_info)
| Provide an entry point into the service and perfom initialization of the service.
virtual int | fini (void)
| This is an iterface function corresponding to the object moving back into IDLE state.
virtual int | suspend (void)
| Temporarily suspend the execution of a service.
virtual int | resume (void)
| Resume execution of a service.
virtual void | init_service ()=0
| Interface function provided for derived classes as a place to initialize specifics of derived server.
virtual void | process_events ()=0
| Interface function provided for derived classes as the main entry for data processing.
virtual void | fatal_signal_hook ()
| Hook for derived class to do addition clean-up when terminating signal is delivered by OS.
int | handle_signal (int signum_)
| Handle fatal signals.
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.
void | stop_service ()
| Inform server that it has to stop data processing, clean up and exit.
void | set_version (const string &release_, int revision_)
| Set Version and Revision number.
string | get_version ()
| Obtain version information.
void | set_author (const string &author_)
| Set author's name.
void | set_flags (LogFlag logf_)
| New debug information is added to the old log file.
virtual void | display_help ()
| List options and invocation syntax to stdout.
string | get_proc_name ()
| Get name of process+instance_number.
void | set_proc_name (string proc_name_)
| Change process name.
string | get_cmdline_name ()
| Get command-line process name.
string | get_default_config_file ()
| Get default configuration file name: $HOME/.
string | get_config_file ()
| Get alternative configuration file name.
string | get_port ()
| Return assumed name of the listening port.
void | set_port (string port_)
| Set listening port name.
SigHandlers & | get_sig_manager ()
| Obtain reference to the Signal Manager, class SigHandls.
Reactor * | get_reactor ()
| Obtain reference to the Reactor.
int | get_exit_value () const
| Retrieve exit value of the process.
Public Member Functions inherited from ASSA::EventHandler
| EventHandler ()
| Constructor.
virtual | ~EventHandler ()
| Virtual destructor.
virtual int | handle_read (int fd)
| Read event callback.
virtual int | handle_write (int fd)
| Write handler callback.
virtual int | handle_except (int fd)
| Exception handler callback.
virtual int | handle_timeout (TimerId tid)
| Timeout handler callback.
virtual int | handle_close (int fd)
| EOF on peer socket handler callback.
virtual void | resetState (void)
| A hook for derived class to reset internal state as needed.
void | set_id (const std::string &id_)
| Set EventHandler ID.
std::string | get_id () const
| Retrieve EventHandler ID.
Public Member Functions inherited from ASSA::CmdLineOpts
| CmdLineOpts ()
| Default constructor.
virtual | ~CmdLineOpts ()
| Do-nothing destructor.
bool | add_flag_opt (const char c, const string &s, bool *f)
| Add binary flag option.
bool | add_opt (const char c, const string &s, string *str)
| Add an option with STL string argument.
bool | add_opt (const char c, const string &s, int *i)
| Add an option with integer argument.
bool | add_opt (const char c, const string &s, unsigned int *ui)
| Add an option with unsigned integer argument.
bool | add_opt (const char c, const string &s, long *l)
| Add an option with long argument.
bool | add_opt (const char c, const string &s, unsigned long *ul)
| Add an option with unsigned long argument.
bool | add_opt (const char c, const string &s, double *d)
| Add an option with double argument.
bool | add_opt (const char c, const string &s, float *f)
| Add an option with float argument.
bool | add_opt (const char c_, const string &s_, OPTS_FUNC f_)
| Add an option with static function argument.
bool | add_opt (const char c_, const string &s_, OPTS_FUNC_ONE f_)
| Add an option with static function argument.
bool | rm_opt (const char c_, const string &s_)
| Remove option for the option list.
bool | parse_args (const char *argv[])
| Parse command line arguments based on installed options set.
int | parse_config_file (IniFile &inifile_)
| Parse configuration parameters found in [options] section of the INI file.
const char * | get_opt_error () const
| If previous call to one of member functions returned false, retrieve detailed error message.
void | dump () const
| Write options set to the log file.

## Static Public Member Functions

static bool | become_daemon ()
| Become a daemon process.
Static Public Member Functions inherited from ASSA::CmdLineOpts
static void | str_to_argv (const string &src_, int &argc_, char **&argv_)
| Static function.
static void | free_argv (char **&argv_)
| Free up memory allocated by `str_to_argv()` function

## Protected Member Functions

void | set_exit_value (int v_)
| Set exit value of the process. This value is returned to the shell.
Protected Member Functions inherited from ASSA::CmdLineOpts
bool | is_valid (const char sopt_, const string &lopt_)
| Detect if supplied option is valid.
void | set_error_none ()
| Reset error message to an empty string.
bool | assign (Option *node_, const char *op_)
| Perform value assignment to the node.
Option * | find_option (const char *str_)
| Locate option in the options set.
Option * | find_option (const char letter_)
| Locate option in the options set.
virtual void | pos_arg (const char *arg_)
| Process positional argument arg_.

## Protected Attributes

string | m_proc_name
| process name (considering instance_number)
string | m_cmdline_name
| process name as appeared on command line
string | m_port
| listening port name
string | m_default_config_file
| standard configuration file name
string | m_config_file
| alternative configuration file name
u_int | m_log_size
| Max size of the log file.
int | m_instance
| Process instance.
string | m_log_file
| Full pathname of debug file.
string | m_with_log_server
| If 'yes', send log messages to the log server.
string | m_log_server
| Log server, assa-logd, address (port@host)
long | m_mask
| Debug file mask to filter debug/error messages.
bool | m_graceful_quit
| Flag that indicates wheather server outgh to stop and exit.
SigHandlers | m_sig_dispatcher
| Signal handlers dispatcher.
SIGPOLLHandler | m_sig_poll
| Function that swallows SIGPOLL calls.
Reactor | m_reactor
| GenServer object has its very own personal Reactor object.
string | m_version
| Software version.
int | m_revision
| Software revision (patch) level.
string | m_author
| Author's name.
const char * | m_help_msg
| Help information.
LogFlag | m_log_flag
| Log file initialization flag. If RM_LOG, remove old log file.
string | m_log_stdout
| If 'yes', redirects all logging messages to std::cerr.
string | m_daemon
| Daemon option flag. If 'yes', become a UNIX daemon process.
string | m_ommit_pidfile
| If 'yes', skip PID file locking creation/locking step.
int | m_log_level
| Logging level - an integer number that incrementally increases verbosity of the looing messages.
PidFileLock | m_pidfile_lock
| PID File lock.
string | m_pidfile
| PID File lock path name.
bool | m_help_flag
| Help option flag.
bool | m_version_flag
| Version option flag.
int | m_exit_value
| Exit value of the process.
Protected Attributes inherited from ASSA::EventHandler
std::string | m_id

## Private Member Functions

| GenServer (const GenServer &)
| No cloning.
GenServer & | operator= (const GenServer &)
void | init_internals ()
| Initialize internals.

## Detailed Description

Definition at line 59 of file GenServer.h.

## Member Enumeration Documentation

## LogFlag

enum ASSA::GenServer::LogFlag

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.

{
KEEPLOG,
RMLOG
};
ASSA::GenServer::KEEPLOG
@ KEEPLOG
By default, append new log records to the existing log file.
Definition GenServer.h:67
ASSA::GenServer::RMLOG
@ RMLOG
Remove existing log file and start afresh.
Definition GenServer.h:70

## Constructor & Destructor Documentation

## GenServer() [1/2]

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.

:
m_log_size (10485760), // 10 Mb
m_instance (-1),
m_with_log_server ("no"),
m_log_server ("assalogd@"),
m_mask (ALL),
m_graceful_quit (false),
m_version ("unknown"),
m_revision (0),
m_author ("John Doe"),
m_help_msg ("No help available"),
m_log_flag (KEEPLOG),
m_log_stdout ("no"),
m_daemon ("no"),
m_ommit_pidfile ("no"),
m_log_level (-1),
m_help_flag (false),
m_version_flag (false),
m_exit_value (0)
{
add_flag_opt ('h', "help", &m_help_flag);
add_flag_opt ('v', "version", &m_version_flag);
add_opt ('d', "log-stdout", &m_log_stdout);
add_opt ('b', "daemon", &m_daemon);
add_opt ('L', "ommit-pidfile", &m_ommit_pidfile);
add_opt ('s', "with-log-server", &m_with_log_server);
add_opt ('m', "mask", &m_mask);
add_opt ('D', "log-file", &m_log_file);
add_opt ('f', "config-file", &m_config_file);
add_opt ('n', "instance", &m_instance);
add_opt ('p', "port", &m_port);
add_opt ('z', "log-size", &m_log_size);
add_opt ('l', "pidfile", &m_pidfile);
add_opt ('S', "log-server", &m_log_server);
add_opt ('c', "log-level", &m_log_level);
char hn[64];
::gethostname (hn, sizeof (hn)-1);
m_log_server += hn;
}
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::CmdLineOpts::add_opt
bool add_opt(const char c, const string &s, string *str)
Add an option with STL string argument.
Definition CmdLineOpts.cpp:220
ASSA::CmdLineOpts::add_flag_opt
bool add_flag_opt(const char c, const string &s, bool *f)
Add binary flag option.
Definition CmdLineOpts.cpp:206
ASSA::GenServer::m_log_server
string m_log_server
Log server, assa-logd, address (port@host)
Definition GenServer.h:263
ASSA::GenServer::m_author
string m_author
Author's name.
Definition GenServer.h:289
ASSA::GenServer::m_version
string m_version
Software version.
Definition GenServer.h:283
ASSA::GenServer::m_instance
int m_instance
Process instance.
Definition GenServer.h:253
ASSA::GenServer::m_log_file
string m_log_file
Full pathname of debug file.
Definition GenServer.h:256
ASSA::GenServer::m_graceful_quit
bool m_graceful_quit
Flag that indicates wheather server outgh to stop and exit.
Definition GenServer.h:269
ASSA::GenServer::m_config_file
string m_config_file
alternative configuration file name
Definition GenServer.h:247
ASSA::GenServer::m_ommit_pidfile
string m_ommit_pidfile
If 'yes', skip PID file locking creation/locking step.
Definition GenServer.h:304
ASSA::GenServer::m_pidfile
string m_pidfile
PID File lock path name.
Definition GenServer.h:316
ASSA::GenServer::m_revision
int m_revision
Software revision (patch) level.
Definition GenServer.h:286
ASSA::GenServer::m_with_log_server
string m_with_log_server
If 'yes', send log messages to the log server.
Definition GenServer.h:259
ASSA::GenServer::m_version_flag
bool m_version_flag
Version option flag.
Definition GenServer.h:326
ASSA::GenServer::m_log_size
u_int m_log_size
Max size of the log file.
Definition GenServer.h:250
ASSA::GenServer::m_mask
long m_mask
Debug file mask to filter debug/error messages.
Definition GenServer.h:266
ASSA::GenServer::m_log_stdout
string m_log_stdout
If 'yes', redirects all logging messages to std::cerr.
Definition GenServer.h:298
ASSA::GenServer::m_port
string m_port
listening port name
Definition GenServer.h:241
ASSA::GenServer::m_log_flag
LogFlag m_log_flag
Log file initialization flag. If RM_LOG, remove old log file.
Definition GenServer.h:295
ASSA::GenServer::m_exit_value
int m_exit_value
Exit value of the process.
Definition GenServer.h:329
ASSA::GenServer::m_help_msg
const char * m_help_msg
Help information.
Definition GenServer.h:292
ASSA::GenServer::m_log_level
int m_log_level
Logging level - an integer number that incrementally increases verbosity of the looing messages.
Definition GenServer.h:310
ASSA::GenServer::m_help_flag
bool m_help_flag
Help option flag.
Definition GenServer.h:321
ASSA::GenServer::m_daemon
string m_daemon
Daemon option flag. If 'yes', become a UNIX daemon process.
Definition GenServer.h:301
ASSA::ALL
@ ALL
All messages: library + application
Definition LogMask.h:62

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.

## ~GenServer()

GenServer::~GenServer | ( |  | ) |

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.

{
Log::log_close ();
}
ASSA::Log::log_close
int log_close(void)
Close logging stream.
Definition Logger.h:359

References ASSA::Log::log_close().

## GenServer() [2/2]

ASSA::GenServer::GenServer | ( | const GenServer & |  | ) |

private

No cloning.

## Member Function Documentation

## become_daemon()

bool GenServer::become_daemon | ( |  | ) |

static

Become a daemon process.

Definition at line 357 of file GenServer.cpp.

{
#if defined(WIN32)
return true;
#else
Fork f (Fork::LEAVE_ALONE, Fork::IGNORE_STATUS);
if (!f.isChild ()) { // parent exits
exit (0);
}
int size = 1024;
int i = 0;
pid_t nullfd;
for (i = 0; i < size; i++) {
(void) close (i);
}
nullfd = open ("/dev/null", O_WRONLY | O_CREAT, 0666);
if (nullfd == -1) {
syslog (LOG_ERR,"failed to open \"/dev/null\"");
return false;
}
(void) dup2 (nullfd, 1);
(void) dup2 (nullfd, 2);
(void) close (nullfd);
if ( setsid() == -1 ) {
syslog (LOG_ERR,"setsid() failed");
return false;
}
/*---
Changing to root directory would be the right thing to do for a
server (so that it wouldn't possibly depend on any mounted file
systems. But, in practice, it might cause a lot of problems.
---*/
#if 0
if ( chdir("/") == -1 ) {
return false;
}
#endif
return (true);
#endif // defined(WIN32)
}
ASSA::Fork
Fork class is a simple wrapper around C library function fork().
Definition Fork.h:86
ASSA::Fork::LEAVE_ALONE
@ LEAVE_ALONE
Ignore all running children on exit.
Definition Fork.h:94
ASSA::Fork::IGNORE_STATUS
@ IGNORE_STATUS
Don't wait for child to complete.
Definition Fork.h:100

References ASSA::Fork::IGNORE_STATUS, and ASSA::Fork::LEAVE_ALONE.

Referenced by init().

## display_help()

void ASSA::GenServer::display_help | ( |  | ) |

inline virtual

List options and invocation syntax to stdout.

Definition at line 374 of file GenServer.h.

{
std::cout << m_help_msg << '\n'
<< "Written by " << m_author << "\n" << std::endl;
}

References m_author, and m_help_msg.

Referenced by init().

## fatal_signal_hook()

virtual void ASSA::GenServer::fatal_signal_hook | ( |  | ) |

inline virtual

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.

{ /*--- empty ---*/ }

Referenced by handle_signal().

## fini()

virtual int ASSA::GenServer::fini | ( | void |  | ) |

inline virtual

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.

{ return 0; }

## get_cmdline_name()

string ASSA::GenServer::get_cmdline_name | ( |  | ) |

inline

Get command-line process name.

Definition at line 190 of file GenServer.h.

{ return m_cmdline_name; }
ASSA::GenServer::m_cmdline_name
string m_cmdline_name
process name as appeared on command line
Definition GenServer.h:238

References m_cmdline_name.

Referenced by init_internals().

## get_config_file()

string ASSA::GenServer::get_config_file | ( |  | ) |

inline

Get alternative configuration file name.

This name is specified as command-line argument '-f'

Definition at line 203 of file GenServer.h.

{ return m_config_file; }

References m_config_file.

## get_default_config_file()

string ASSA::GenServer::get_default_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.

{ return m_default_config_file; }
ASSA::GenServer::m_default_config_file
string m_default_config_file
standard configuration file name
Definition GenServer.h:244

References m_default_config_file.

## get_exit_value()

int ASSA::GenServer::get_exit_value | ( |  | ) | const

inline

Retrieve exit value of the process.

Definition at line 227 of file GenServer.h.

{ return m_exit_value; }

References m_exit_value.

## get_port()

string ASSA::GenServer::get_port | ( |  | ) |

inline

Return assumed name of the listening port.

Definition at line 206 of file GenServer.h.

{ return m_port; }

References m_port.

## get_proc_name()

string ASSA::GenServer::get_proc_name | ( |  | ) |

inline

Get name of process+instance_number.

Definition at line 182 of file GenServer.h.

{ return m_proc_name; }
ASSA::GenServer::m_proc_name
string m_proc_name
process name (considering instance_number)
Definition GenServer.h:235

References m_proc_name.

Referenced by init_internals().

## get_reactor()

Reactor * ASSA::GenServer::get_reactor | ( |  | ) |

inline

Obtain reference to the Reactor.

Definition at line 221 of file GenServer.h.

{ return &m_reactor; }
ASSA::GenServer::m_reactor
Reactor m_reactor
GenServer object has its very own personal Reactor object.
Definition GenServer.h:280

References m_reactor.

Referenced by handle_signal(), and init_internals().

## get_sig_manager()

SigHandlers & ASSA::GenServer::get_sig_manager | ( |  | ) |

inline

Obtain reference to the Signal Manager, class SigHandls.

Definition at line 216 of file GenServer.h.

{ return m_sig_dispatcher; }
ASSA::GenServer::m_sig_dispatcher
SigHandlers m_sig_dispatcher
Signal handlers dispatcher.
Definition GenServer.h:273

References m_sig_dispatcher.

## get_version()

string ASSA::GenServer::get_version | ( |  | ) |

inline

Obtain version information.

Definition at line 365 of file GenServer.h.

{
std::ostringstream v;
v << "Version: " << m_version << " Revision: " << m_revision << std::ends;
return (v.str ());
}

References m_revision, and m_version.

Referenced by init().

## handle_signal()

int GenServer::handle_signal | ( | int | signum_ | ) |

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.

{
trace("GenServer::handle_signal");
std::ostringstream m;
switch (signum_)
{
case SIGTERM: m << "SIGTERM signal caugth. "; break;
case SIGINT: m << "SIGINT signal caugth. "; break;
default: m << "Unexpected signal caugth.";
}
m << "Signal # " << signum_ << std::ends;
DL((APP,"%s\n", m.str ().c_str () ));
DL((APP,"Initiating shutdown sequence...\n"));
fatal_signal_hook ();
DL((APP, "Shutdown sequence completed - Exiting !\n"));
/* Calling stop_service () triggers a call to Reactor::stopReactor()
with subsequent call to Reactor::removeIOHandler() and then
EventHandler::handle_close(). If EventHandler is in the middle
of the *slow* system call such as read(2), handle_close() will
destry EventHandler, and after cotrol is returned from
GenServer::handle_signal(), *slow* system call is restarted
and proceeds to operate on the memory that has been deleted already.
Calling Reactor::deactivate() instead delays memory release.
*/
get_reactor()->deactivate ();
m_graceful_quit = true;
return 0;
}
trace
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition Logger.h:429
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
ASSA::GenServer::get_reactor
Reactor * get_reactor()
Obtain reference to the Reactor.
Definition GenServer.h:221
ASSA::GenServer::fatal_signal_hook
virtual void fatal_signal_hook()
Hook for derived class to do addition clean-up when terminating signal is delivered by OS.
Definition GenServer.h:135
ASSA::Reactor::deactivate
void deactivate(void)
Deactivate Reactor.
Definition Reactor.h:234
ASSA::APP
@ APP
Application-level messages
Definition LogMask.h:27

References ASSA::APP, ASSA::Reactor::deactivate(), DL, fatal_signal_hook(), get_reactor(), m_graceful_quit, and trace.

## init()

void GenServer::init | ( | int * | argc,
|  | char * | argv[],
|  | const char * | help_info
| ) |  |

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.

Parameters

  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.

{
char* cp = argv [0];
m_help_msg = ht_;
if (strchr(cp, ASSA_DIR_SEPARATOR)) {
cp += strlen(argv[0]); // position at the end
while (*cp-- != ASSA_DIR_SEPARATOR) {
;
}
cp += 2;
}
#if defined (WIN32) // get rid of '.exe'
char* extidx = cp;
while (*extidx) {
if (*extidx == '.') {
*extidx = '\0';
break;
}
extidx++;
}
#endif
m_cmdline_name = cp;
if (!parse_args ((const char **)argv)) {
std::cerr << "Error in arguments: " << get_opt_error () << std::endl;
std::cerr << "Try '" << argv[0] << " --help' for details.\n";
exit (1);
}
if (m_help_flag) {
display_help ();
exit (0);
}
if (m_version_flag) {
std::cerr << '\n' << argv[0] << " " << get_version () << '\n' << '\n'
<< "Written by " << m_author << "\n\n";
exit (0);
}
std::string s;
if (m_default_config_file.size ()) {
s = ASSA::Utils::strenv (m_default_config_file.c_str ());
m_default_config_file = s;
}
if (m_config_file.size ()) {
s = ASSA::Utils::strenv (m_config_file.c_str ());
m_config_file = s;
}
if (m_log_file.size ()) {
s = ASSA::Utils::strenv (m_log_file.c_str ());
m_log_file = s;
}
if (m_pidfile.size ()) {
s = ASSA::Utils::strenv (m_pidfile.c_str ());
m_pidfile = s;
}
if (m_daemon == "yes") {
assert(become_daemon ());
}
char instbuf[16]; // INT_MAX [-]2147483647
sprintf(instbuf, "%d", m_instance);
if (m_proc_name.length() == 0) {
m_proc_name = m_cmdline_name;
if (m_instance != -1) {
m_proc_name += instbuf;
}
}
if (m_port.length() == 0) {
m_port = m_proc_name;
}
#if !defined(WIN32)
SigAction ignore_act( SIG_IGN );
ignore_act.register_action( SIGHUP );
ignore_act.register_action( SIGPIPE );
ignore_act.register_action( SIGCHLD );
#if !(defined (__FreeBSD__) || defined(__FreeBSD_kernel__) \
|| defined (__NetBSD__))
ignore_act.register_action( SIGCLD );
#endif
ignore_act.register_action( SIGALRM );
m_sig_dispatcher.install ( ASSAIOSIG, &m_sig_poll );
m_sig_dispatcher.install ( SIGINT, (EventHandler*) this );
m_sig_dispatcher.install ( SIGTERM, (EventHandler*) this );
#endif // !defined(WIN32)
init_internals ();
}
ASSA_DIR_SEPARATOR
#define ASSA_DIR_SEPARATOR
CommonUtils.h.
Definition CommonUtils.h:47
ASSAIOSIG
#define ASSAIOSIG
Definition Handlers.h:38
ASSA::CmdLineOpts::parse_args
bool parse_args(const char *argv[])
Parse command line arguments based on installed options set.
Definition CmdLineOpts.cpp:365
ASSA::CmdLineOpts::get_opt_error
const char * get_opt_error() const
If previous call to one of member functions returned false, retrieve detailed error message.
Definition CmdLineOpts.h:309
ASSA::EventHandler
EventHandler class.
Definition EventHandler.h:103
ASSA::GenServer::become_daemon
static bool become_daemon()
Become a daemon process.
Definition GenServer.cpp:358
ASSA::GenServer::init_internals
void init_internals()
Initialize internals.
Definition GenServer.cpp:271
ASSA::GenServer::display_help
virtual void display_help()
List options and invocation syntax to stdout.
Definition GenServer.h:375
ASSA::GenServer::m_sig_poll
SIGPOLLHandler m_sig_poll
Function that swallows SIGPOLL calls.
Definition GenServer.h:276
ASSA::GenServer::get_version
string get_version()
Obtain version information.
Definition GenServer.h:366
ASSA::SigAction
Definition SigAction.h:95
ASSA::SigHandlers::install
virtual int install(int signum_, EventHandler *new_hand_, SigAction *new_disp_=0, EventHandler **old_hand_=0, SigAction *old_disp_=0)
Register EventHandler with dispatching system.
Definition SigHandlers.cpp:36
ASSA::Utils::strenv
std::string strenv(const char *in_)
Expand the passed string in_ by substituting environment variable names for their values.
Definition CommonUtils.cpp:123

References ASSA_DIR_SEPARATOR, ASSAIOSIG, become_daemon(), display_help(), 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(), and ASSA::Utils::strenv().

## init_internals()

void GenServer::init_internals | ( |  | ) |

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.

{
static const char self[] = "GenServer::init_internals";
#if defined (WIN32)
m_default_config_file = this->get_cmdline_name () + ".ini";
#else
m_default_config_file = "$HOME/." + this->get_cmdline_name ();
m_default_config_file = Utils::strenv (m_default_config_file.c_str ());
#endif
if (m_log_flag == RMLOG && m_log_stdout == "no")
{
struct stat fst;
if (::stat (m_log_file.c_str(), &fst) == 0)
{
if (S_ISREG (fst.st_mode)) {
::unlink (m_log_file.c_str());
}
}
}
Log::set_app_name (get_proc_name ());
if (m_log_stdout == "yes") {
Log::open_log_stdout (m_mask);
}
else {
if (m_with_log_server == "yes") {
Log::open_log_server (m_log_server,
m_log_file.c_str(),
get_reactor (),
m_mask,
m_log_size) ;
}
else {
Log::open_log_file (m_log_file.c_str(), m_mask, m_log_size);
}
}
trace(self);
if (m_ommit_pidfile == "no")
{
if (m_pidfile.size () == 0) {
string s ("~/." + m_proc_name + ".pid");
m_pidfile = ASSA::Utils::strenv (s.c_str ());
}
if (! m_pidfile_lock.lock (m_pidfile)) {
DL((ASSAERR,"Failed to lock PID file: %s\n",
m_pidfile_lock.get_error_msg ()));
exit (1);
}
}
DL((APP,"\n" ));
DL((APP,"========================================================\n"));
DL((APP,"|| Server configuration settings ||\n"));
DL((APP,"========================================================\n"));
DL((APP," cmd_line_name = '%s'\n", m_cmdline_name.c_str() ));
DL((APP," name = '%s'\n", m_proc_name.c_str() ));
DL((APP," default config file = '%s'\n", m_default_config_file.c_str()));
DL((APP," config file = '%s'\n", m_config_file.c_str() ));
DL((APP," mask = 0x%X\n", m_mask ));
dump ();
DL((APP,"========================================================\n"));
DL((APP,"\n"));
}
ASSA::CmdLineOpts::dump
void dump() const
Write options set to the log file.
Definition CmdLineOpts.cpp:646
ASSA::GenServer::get_cmdline_name
string get_cmdline_name()
Get command-line process name.
Definition GenServer.h:190
ASSA::GenServer::m_pidfile_lock
PidFileLock m_pidfile_lock
PID File lock.
Definition GenServer.h:313
ASSA::GenServer::get_proc_name
string get_proc_name()
Get name of process+instance_number.
Definition GenServer.h:182
ASSA::PidFileLock::get_error_msg
const char * get_error_msg() const
In case of error, return a verbal description of the last error.
Definition PidFileLock.h:141
ASSA::PidFileLock::lock
bool lock(const string &filename_)
Lock the file.
Definition PidFileLock.cpp:62
ASSA::Log::open_log_file
int open_log_file(const char *logfname_, u_long groups_=ALL, u_long maxsize_=10485760)
Open log file.
Definition Logger.h:319
ASSA::Log::set_app_name
void set_app_name(const std::string &appname_)
Set application name.
Definition Logger.h:305
ASSA::Log::open_log_server
int open_log_server(const std::string &logsvraddr_, const char *logfname_, Reactor *reactor_, u_long groups_=ASSA::ALL, u_long maxsize_=10485760)
Open connection with and write log message to the log server.
Definition Logger.h:344
ASSA::Log::open_log_stdout
int open_log_stdout(u_long groups_=ALL)
Write log message to standard output.
Definition Logger.h:327
ASSA::ASSAERR
@ ASSAERR
ASSA and system errors
Definition LogMask.h:34

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

## init_service()

virtual void ASSA::GenServer::init_service | ( |  | ) |

pure virtual

Interface function provided for derived classes as a place to initialize specifics of derived server.

## operator=()

GenServer & ASSA::GenServer::operator= | ( | const GenServer & |  | ) |

private

## process_events()

virtual void ASSA::GenServer::process_events | ( |  | ) |

pure virtual

Interface function provided for derived classes as the main entry for data processing.

This is the place to implement main event loop.

## resume()

virtual int ASSA::GenServer::resume | ( | void |  | ) |

inline virtual

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.

{ return 0; }

## service_is_active()

bool ASSA::GenServer::service_is_active | ( |  | ) |

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.

Returns
  true when active; false if 'graceful quit' flag has been raised;

Definition at line 149 of file GenServer.h.

{ return (!m_graceful_quit); }

References m_graceful_quit.

## set_author()

void ASSA::GenServer::set_author | ( | const string & | author_ | ) |

inline

Set author's name.

Definition at line 358 of file GenServer.h.

{
m_author = author_;
}

References m_author.

## set_exit_value()

void ASSA::GenServer::set_exit_value | ( | int | v_ | ) |

inline protected

Set exit value of the process. This value is returned to the shell.

Definition at line 231 of file GenServer.h.

{ m_exit_value = v_; }

References m_exit_value.

## set_flags()

void ASSA::GenServer::set_flags | ( | LogFlag | logf_ | ) |

inline

New debug information is added to the old log file.

To erase old log file, set flag to RMLOG.

Parameters

  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.

{ m_log_flag = logf_; }

References m_log_flag.

## set_port()

void ASSA::GenServer::set_port | ( | string | port_ | ) |

inline

Set listening port name.

Parameters

  port_ | new listening port name

Definition at line 211 of file GenServer.h.

{ m_port = port_; }

References m_port.

## set_proc_name()

void ASSA::GenServer::set_proc_name | ( | string | proc_name_ | ) |

inline

Change process name.

Parameters

  proc_name_ | new process name

Definition at line 187 of file GenServer.h.

{ m_proc_name = proc_name_; }

References m_proc_name.

## set_version()

void ASSA::GenServer::set_version | ( | const string & | release_,
|  | int | revision_
| ) |  |

inline

Set Version and Revision number.

Parameters

  release_ | Release number.
  revision_ | Patch level.

Definition at line 350 of file GenServer.h.

{
m_version = release_;
m_revision = revision_;
}

References m_revision, and m_version.

## stop_service()

void ASSA::GenServer::stop_service | ( |  | ) |

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.

{
m_graceful_quit = true;
m_reactor.deactivate ();
}

References ASSA::Reactor::deactivate(), m_graceful_quit, and m_reactor.

## suspend()

virtual int ASSA::GenServer::suspend | ( | void |  | ) |

inline virtual

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.

{ return 0; }

## Member Data Documentation

## m_author

string ASSA::GenServer::m_author

protected

Author's name.

Definition at line 289 of file GenServer.h.

Referenced by display_help(), init(), and set_author().

## m_cmdline_name

string ASSA::GenServer::m_cmdline_name

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

## m_config_file

string ASSA::GenServer::m_config_file

protected

alternative configuration file name

Definition at line 247 of file GenServer.h.

Referenced by GenServer(), get_config_file(), init(), and init_internals().

## m_daemon

string ASSA::GenServer::m_daemon

protected

Daemon option flag. If 'yes', become a UNIX daemon process.

Definition at line 301 of file GenServer.h.

Referenced by GenServer(), and init().

## m_default_config_file

string ASSA::GenServer::m_default_config_file

protected

standard configuration file name

Definition at line 244 of file GenServer.h.

Referenced by get_default_config_file(), init(), and init_internals().

## m_exit_value

int ASSA::GenServer::m_exit_value

protected

Exit value of the process.

Definition at line 329 of file GenServer.h.

Referenced by get_exit_value(), and set_exit_value().

## m_graceful_quit

bool ASSA::GenServer::m_graceful_quit

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

## m_help_flag

bool ASSA::GenServer::m_help_flag

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

## m_help_msg

const char* ASSA::GenServer::m_help_msg

protected

Help information.

Definition at line 292 of file GenServer.h.

Referenced by display_help(), and init().

## m_instance

int ASSA::GenServer::m_instance

protected

Process instance.

Definition at line 253 of file GenServer.h.

Referenced by GenServer(), and init().

## m_log_file

string ASSA::GenServer::m_log_file

protected

Full pathname of debug file.

Definition at line 256 of file GenServer.h.

Referenced by GenServer(), init(), and init_internals().

## m_log_flag

LogFlag ASSA::GenServer::m_log_flag

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

## m_log_level

int ASSA::GenServer::m_log_level

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

## m_log_server

string ASSA::GenServer::m_log_server

protected

Log server, assa-logd, address (port@host)

Definition at line 263 of file GenServer.h.

Referenced by GenServer(), and init_internals().

## m_log_size

u_int ASSA::GenServer::m_log_size

protected

Max size of the log file.

Definition at line 250 of file GenServer.h.

Referenced by GenServer(), and init_internals().

## m_log_stdout

string ASSA::GenServer::m_log_stdout

protected

If 'yes', redirects all logging messages to std::cerr.

Definition at line 298 of file GenServer.h.

Referenced by GenServer(), and init_internals().

## m_mask

long ASSA::GenServer::m_mask

protected

Debug file mask to filter debug/error messages.

Definition at line 266 of file GenServer.h.

Referenced by GenServer(), and init_internals().

## m_ommit_pidfile

string ASSA::GenServer::m_ommit_pidfile

protected

If 'yes', skip PID file locking creation/locking step.

Definition at line 304 of file GenServer.h.

Referenced by GenServer(), and init_internals().

## m_pidfile

string ASSA::GenServer::m_pidfile

protected

PID File lock path name.

Definition at line 316 of file GenServer.h.

Referenced by GenServer(), init(), and init_internals().

## m_pidfile_lock

PidFileLock ASSA::GenServer::m_pidfile_lock

protected

PID File lock.

Definition at line 313 of file GenServer.h.

Referenced by init_internals().

## m_port

string ASSA::GenServer::m_port

protected

listening port name

Definition at line 241 of file GenServer.h.

Referenced by GenServer(), get_port(), init(), and set_port().

## m_proc_name

string ASSA::GenServer::m_proc_name

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

## m_reactor

Reactor ASSA::GenServer::m_reactor

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

## m_revision

int ASSA::GenServer::m_revision

protected

Software revision (patch) level.

Definition at line 286 of file GenServer.h.

Referenced by get_version(), and set_version().

## m_sig_dispatcher

SigHandlers ASSA::GenServer::m_sig_dispatcher

protected

Signal handlers dispatcher.

Definition at line 273 of file GenServer.h.

Referenced by get_sig_manager(), and init().

## m_sig_poll

SIGPOLLHandler ASSA::GenServer::m_sig_poll

protected

Function that swallows SIGPOLL calls.

Definition at line 276 of file GenServer.h.

Referenced by init().

## m_version

string ASSA::GenServer::m_version

protected

Software version.

Definition at line 283 of file GenServer.h.

Referenced by get_version(), and set_version().

## m_version_flag

bool ASSA::GenServer::m_version_flag

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

## m_with_log_server

string ASSA::GenServer::m_with_log_server

protected

If 'yes', send log messages to the log server.

Definition at line 259 of file GenServer.h.

Referenced by GenServer(), and init_internals().

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

- GenServer.h
- GenServer.cpp