Loading...
Searching...
No Matches
GenServer.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// GenServer.h
//------------------------------------------------------------------------------
// Copyright (c) 1999-2005 by Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#ifndef GENSERVER_H
#define GENSERVER_H
extern "C" {
#include <stdio.h> /* printf, sprintf */
#include <unistd.h>
#include <stdlib.h> /* getopt() */
#include <string.h> /* strlen */
#include <errno.h> /* errno */
#include <signal.h> /* kill */
#include <sys/types.h> /* open */
#include <sys/stat.h> /* open */
#include <fcntl.h> /* open */
#include <limits.h> /* PATH_MAX */
#include <assert.h>
#if !defined(WIN32)
# include <sys/resource.h> /* getrlimit */
# include <syslog.h>
#endif
}
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::string;
using std::vector;
#include "assa/Assure.h"
#include "assa/Handlers.h"
#include "assa/SigHandlers.h"
#include "assa/Fork.h"
#include "assa/Reactor.h"
#include "assa/CmdLineOpts.h"
#include "assa/PidFileLock.h"
namespace ASSA {
class GenServer :
public virtual EventHandler,
public CmdLineOpts
{
public:
enum LogFlag {
KEEPLOG,
RMLOG
};
public:
GenServer ();
virtual ~GenServer ();
virtual void init (int* argc, char* argv[], const char* help_info);
virtual int fini (void) { return 0; }
virtual int suspend (void) { return 0; }
virtual int resume (void) { return 0; }
virtual void init_service () =0;
virtual void process_events () =0;
virtual void fatal_signal_hook () { /*--- empty ---*/ }
int handle_signal (int signum_);
bool service_is_active () { return (!m_graceful_quit); }
void stop_service ();
void set_version (const string& release_, int revision_);
string get_version ();
void set_author (const string& author_);
void set_flags (LogFlag logf_) { m_log_flag = logf_; }
virtual void display_help ();
string get_proc_name () { return m_proc_name; }
void set_proc_name (string proc_name_) { m_proc_name = proc_name_; }
string get_cmdline_name () { return m_cmdline_name; }
string get_default_config_file () { return m_default_config_file; }
string get_config_file () { return m_config_file; }
string get_port () { return m_port; }
void set_port (string port_) { m_port = port_; }
#if !defined(WIN32)
SigHandlers& get_sig_manager () { return m_sig_dispatcher; }
#endif
Reactor* get_reactor () { return &m_reactor; }
static bool become_daemon ();
int get_exit_value () const { return m_exit_value; }
protected:
void set_exit_value (int v_) { m_exit_value = v_; }
protected:
string m_proc_name;
string m_cmdline_name;
string m_port;
string m_default_config_file;
string m_config_file;
u_int m_log_size;
int m_instance;
string m_log_file;
string m_with_log_server;
string m_log_server;
long m_mask;
bool m_graceful_quit;
#if !defined(WIN32)
SigHandlers m_sig_dispatcher;
SIGPOLLHandler m_sig_poll;
#endif
Reactor m_reactor;
string m_version;
int m_revision;
string m_author;
const char* m_help_msg;
LogFlag m_log_flag;
string m_log_stdout;
string m_daemon;
string m_ommit_pidfile;
int m_log_level;
PidFileLock m_pidfile_lock;
string m_pidfile;
bool m_help_flag;
bool m_version_flag;
int m_exit_value;
private:
GenServer (const GenServer&);
GenServer& operator=(const GenServer&);
void init_internals ();
};
inline void
GenServer::
stop_service ()
{
m_graceful_quit = true;
m_reactor.deactivate ();
}
inline void
GenServer::
set_version (const string& release_, int revision_)
{
m_version = release_;
m_revision = revision_;
}
inline void
GenServer::
set_author (const string& author_)
{
m_author = author_;
}
inline string
GenServer::
get_version ()
{
std::ostringstream v;
v << "Version: " << m_version << " Revision: " << m_revision << std::ends;
return (v.str ());
}
inline void
GenServer::
display_help ()
{
std::cout << m_help_msg << '\n'
<< "Written by " << m_author << "\n" << std::endl;
}
} // The end of namespase ASSA
#endif /* GENSERVER_H */
Assure.h
A collection of assert function wrappers.
CmdLineOpts.h
Class to handle processing command-line options.
Fork.h
A simple wrapper around fork() library function call.
Handlers.h
A collection of various most popular signal handlers.
u_int
unsigned int u_int
Definition Logger_Impl.h:40
PidFileLock.h
A utility class for creating and managing process PID lock file.
Reactor.h
An implementation of Reactor pattern.
SigHandlers.h
SigHandlers is a signal handlers manager.
ASSA::CmdLineOpts::CmdLineOpts
CmdLineOpts()
Default constructor.
Definition CmdLineOpts.h:295
ASSA::EventHandler::EventHandler
EventHandler()
Constructor.
Definition EventHandler.h:165
ASSA::GenServer::m_log_server
string m_log_server
Log server, assa-logd, address (port@host).
Definition GenServer.h:263
ASSA::GenServer::get_reactor
Reactor * get_reactor()
Obtain reference to the Reactor.
Definition GenServer.h:221
ASSA::GenServer::fini
virtual int fini(void)
This is an iterface function corresponding to the object moving back into IDLE state.
Definition GenServer.h:107
ASSA::GenServer::stop_service
void stop_service()
Inform server that it has to stop data processing, clean up and exit.
Definition GenServer.h:343
ASSA::GenServer::get_exit_value
int get_exit_value() const
Retrieve exit value of the process.
Definition GenServer.h:227
ASSA::GenServer::m_author
string m_author
Author's name.
Definition GenServer.h:289
ASSA::GenServer::become_daemon
static bool become_daemon()
Become a daemon process.
Definition GenServer.cpp:358
ASSA::GenServer::get_default_config_file
string get_default_config_file()
Get default configuration file name: $HOME/.
Definition GenServer.h:198
ASSA::GenServer::m_cmdline_name
string m_cmdline_name
process name as appeared on command line
Definition GenServer.h:238
ASSA::GenServer::get_cmdline_name
string get_cmdline_name()
Get command-line process name.
Definition GenServer.h:190
ASSA::GenServer::m_version
string m_version
Software version.
Definition GenServer.h:283
ASSA::GenServer::suspend
virtual int suspend(void)
Temporarily suspend the execution of a service.
Definition GenServer.h:112
ASSA::GenServer::m_instance
int m_instance
Process instance.
Definition GenServer.h:253
ASSA::GenServer::service_is_active
bool service_is_active()
Normally called by the main loop to find out whether 'graceful quit' flag had been raised,...
Definition GenServer.h:149
ASSA::GenServer::GenServer
GenServer()
Constructor.
Definition GenServer.cpp:56
ASSA::GenServer::m_log_file
string m_log_file
Full pathname of debug file.
Definition GenServer.h:256
ASSA::GenServer::~GenServer
virtual ~GenServer()
Destructor.
Definition GenServer.cpp:112
ASSA::GenServer::m_pidfile_lock
PidFileLock m_pidfile_lock
PID File lock.
Definition GenServer.h:313
ASSA::GenServer::resume
virtual int resume(void)
Resume execution of a service.
Definition GenServer.h:117
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::init_internals
void init_internals()
Initialize internals.
Definition GenServer.cpp:271
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::display_help
virtual void display_help()
List options and invocation syntax to stdout.
Definition GenServer.h:375
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::set_flags
void set_flags(LogFlag logf_)
New debug information is added to the old log file.
Definition GenServer.h:176
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_reactor
Reactor m_reactor
GenServer object has its very own personal Reactor object.
Definition GenServer.h:280
ASSA::GenServer::get_config_file
string get_config_file()
Get alternative configuration file name.
Definition GenServer.h:203
ASSA::GenServer::m_default_config_file
string m_default_config_file
standard configuration file name
Definition GenServer.h:244
ASSA::GenServer::m_sig_poll
SIGPOLLHandler m_sig_poll
Function that swallows SIGPOLL calls.
Definition GenServer.h:276
ASSA::GenServer::LogFlag
LogFlag
Definition GenServer.h:66
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
ASSA::GenServer::get_port
string get_port()
Return assumed name of the listening port.
Definition GenServer.h:206
ASSA::GenServer::set_proc_name
void set_proc_name(string proc_name_)
Change process name.
Definition GenServer.h:187
ASSA::GenServer::init_service
virtual void init_service()=0
Interface function provided for derived classes as a place to initialize specifics of derived server.
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::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::GenServer::set_version
void set_version(const string &release_, int revision_)
Set Version and Revision number.
Definition GenServer.h:351
ASSA::GenServer::m_help_msg
const char * m_help_msg
Help information.
Definition GenServer.h:292
ASSA::GenServer::get_proc_name
string get_proc_name()
Get name of process+instance_number.
Definition GenServer.h:182
ASSA::GenServer::init
virtual void init(int *argc, char *argv[], const char *help_info)
Provide an entry point into the service and perfom initialization of the service.
Definition GenServer.cpp:124
ASSA::GenServer::m_proc_name
string m_proc_name
process name (considering instance_number)
Definition GenServer.h:235
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::process_events
virtual void process_events()=0
Interface function provided for derived classes as the main entry for data processing.
ASSA::GenServer::handle_signal
int handle_signal(int signum_)
Handle fatal signals.
Definition GenServer.cpp:409
ASSA::GenServer::GenServer
GenServer(const GenServer &)
No cloning.
ASSA::GenServer::get_sig_manager
SigHandlers & get_sig_manager()
Obtain reference to the Signal Manager, class SigHandls.
Definition GenServer.h:216
ASSA::GenServer::set_exit_value
void set_exit_value(int v_)
Set exit value of the process. This value is returned to the shell.
Definition GenServer.h:231
ASSA::GenServer::m_sig_dispatcher
SigHandlers m_sig_dispatcher
Signal handlers dispatcher.
Definition GenServer.h:273
ASSA::GenServer::operator=
GenServer & operator=(const GenServer &)
ASSA::GenServer::set_author
void set_author(const string &author_)
Set author's name.
Definition GenServer.h:359
ASSA::GenServer::get_version
string get_version()
Obtain version information.
Definition GenServer.h:366
ASSA::GenServer::m_help_flag
bool m_help_flag
Help option flag.
Definition GenServer.h:321
ASSA::GenServer::set_port
void set_port(string port_)
Set listening port name.
Definition GenServer.h:211
ASSA::GenServer::m_daemon
string m_daemon
Daemon option flag. If 'yes', become a UNIX daemon process.
Definition GenServer.h:301
ASSA::PidFileLock
Definition PidFileLock.h:44
ASSA::Reactor
Definition Reactor.h:58
ASSA::SIGPOLLHandler
Class SIGPOLLHandler handles SIGPOLL signal.
Definition Handlers.h:262
ASSA::SigHandlers
Definition SigHandlers.h:56
ASSA
Definition Acceptor.h:40