libassa  3.5.1
GenServer.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // GenServer.h
4 //------------------------------------------------------------------------------
5 // Copyright (c) 1999-2005 by Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 #ifndef GENSERVER_H
13 #define GENSERVER_H
14 
15 extern "C" {
16 #include <stdio.h> /* printf, sprintf */
17 #include <unistd.h>
18 #include <stdlib.h> /* getopt() */
19 #include <string.h> /* strlen */
20 #include <errno.h> /* errno */
21 #include <signal.h> /* kill */
22 #include <sys/types.h> /* open */
23 #include <sys/stat.h> /* open */
24 #include <fcntl.h> /* open */
25 #include <limits.h> /* PATH_MAX */
26 #include <assert.h>
27 
28 #if !defined(WIN32)
29 # include <sys/resource.h> /* getrlimit */
30 # include <syslog.h>
31 #endif
32 }
33 
34 #include <iostream>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 
39 using std::string;
40 using std::vector;
41 
42 #include "assa/Assure.h"
43 #include "assa/Handlers.h"
44 #include "assa/SigHandlers.h"
45 #include "assa/Fork.h"
46 #include "assa/Reactor.h"
47 #include "assa/CmdLineOpts.h"
48 #include "assa/PidFileLock.h"
49 
50 namespace ASSA {
51 
59 class GenServer :
60  public virtual EventHandler,
61  public CmdLineOpts
62 {
63 public:
66  enum LogFlag {
67  KEEPLOG,
70  RMLOG
72  };
73 
74 public:
78  GenServer ();
79 
81  virtual ~GenServer ();
82 
101  virtual void init (int* argc, char* argv[], const char* help_info);
102 
107  virtual int fini (void) { return 0; }
108 
112  virtual int suspend (void) { return 0; }
113 
117  virtual int resume (void) { return 0; }
118 
122  virtual void init_service () =0;
123 
128  virtual void process_events () =0;
129 
135  virtual void fatal_signal_hook () { /*--- empty ---*/ }
136 
140  int handle_signal (int signum_);
141 
149  bool service_is_active () { return (!m_graceful_quit); }
150 
154  void stop_service ();
155 
161  void set_version (const string& release_, int revision_);
162 
164  string get_version ();
165 
167  void set_author (const string& author_);
168 
176  void set_flags (LogFlag logf_) { m_log_flag = logf_; }
177 
179  virtual void display_help ();
180 
182  string get_proc_name () { return m_proc_name; }
183 
187  void set_proc_name (string proc_name_) { m_proc_name = proc_name_; }
188 
190  string get_cmdline_name () { return m_cmdline_name; }
191 
199 
203  string get_config_file () { return m_config_file; }
204 
206  string get_port () { return m_port; }
207 
211  void set_port (string port_) { m_port = port_; }
212 
213 #if !defined(WIN32)
217 #endif
218 
221  Reactor* get_reactor () { return &m_reactor; }
222 
224  static bool become_daemon ();
225 
227  int get_exit_value () const { return m_exit_value; }
228 
229 protected:
231  void set_exit_value (int v_) { m_exit_value = v_; }
232 
233 protected:
235  string m_proc_name;
236 
238  string m_cmdline_name;
239 
241  string m_port;
242 
245 
248 
251 
254 
256  string m_log_file;
257 
260 
263  string m_log_server;
264 
266  long m_mask;
267 
270 
271 #if !defined(WIN32)
274 
277 #endif
278 
281 
283  string m_version;
284 
287 
289  string m_author;
290 
292  const char* m_help_msg;
293 
296 
298  string m_log_stdout;
299 
301  string m_daemon;
302 
305 
311 
314 
316  string m_pidfile;
317 
322 
327 
330 
331 private:
335 
337  void init_internals ();
338 };
339 
340 
341 inline void
343 stop_service ()
344 {
345  m_graceful_quit = true;
347 }
348 
349 inline void
351 set_version (const string& release_, int revision_)
352 {
353  m_version = release_;
354  m_revision = revision_;
355 }
356 
357 inline void
359 set_author (const string& author_)
360 {
361  m_author = author_;
362 }
363 
364 inline string
366 get_version ()
367 {
368  std::ostringstream v;
369  v << "Version: " << m_version << " Revision: " << m_revision << std::ends;
370  return (v.str ());
371 }
372 
373 inline void
375 display_help ()
376 {
377  std::cout << m_help_msg << '\n'
378  << "Written by " << m_author << "\n" << std::endl;
379 }
380 
381 } // The end of namespase ASSA
382 
383 
384 #endif /* GENSERVER_H */
A collection of assert function wrappers.
Class to handle processing command-line options.
A simple wrapper around fork() library function call.
A collection of various most popular signal handlers.
unsigned int u_int
Definition: Logger_Impl.h:40
A utility class for creating and managing process PID lock file.
An implementation of Reactor pattern.
SigHandlers is a signal handlers manager.
Class CmdLineOpts.
Definition: CmdLineOpts.h:114
EventHandler class.
Definition: EventHandler.h:103
string m_log_server
Log server, assa-logd, address (port@host)
Definition: GenServer.h:263
virtual int fini(void)
This is an iterface function corresponding to the object moving back into IDLE state.
Definition: GenServer.h:107
void stop_service()
Inform server that it has to stop data processing, clean up and exit.
Definition: GenServer.h:343
int get_exit_value() const
Retrieve exit value of the process.
Definition: GenServer.h:227
string m_author
Author's name.
Definition: GenServer.h:289
static bool become_daemon()
Become a daemon process.
Definition: GenServer.cpp:358
string get_default_config_file()
Get default configuration file name: $HOME/.
Definition: GenServer.h:198
string m_cmdline_name
process name as appeared on command line
Definition: GenServer.h:238
string get_cmdline_name()
Get command-line process name.
Definition: GenServer.h:190
string m_version
Software version.
Definition: GenServer.h:283
virtual int suspend(void)
Temporarily suspend the execution of a service.
Definition: GenServer.h:112
int m_instance
Process instance.
Definition: GenServer.h:253
bool service_is_active()
Normally called by the main loop to find out whether 'graceful quit' flag had been raised,...
Definition: GenServer.h:149
GenServer()
Constructor.
Definition: GenServer.cpp:56
string m_log_file
Full pathname of debug file.
Definition: GenServer.h:256
virtual ~GenServer()
Destructor.
Definition: GenServer.cpp:112
PidFileLock m_pidfile_lock
PID File lock.
Definition: GenServer.h:313
virtual int resume(void)
Resume execution of a service.
Definition: GenServer.h:117
bool m_graceful_quit
Flag that indicates wheather server outgh to stop and exit.
Definition: GenServer.h:269
string m_config_file
alternative configuration file name
Definition: GenServer.h:247
void init_internals()
Initialize internals.
Definition: GenServer.cpp:271
string m_ommit_pidfile
If 'yes', skip PID file locking creation/locking step.
Definition: GenServer.h:304
string m_pidfile
PID File lock path name.
Definition: GenServer.h:316
int m_revision
Software revision (patch) level.
Definition: GenServer.h:286
virtual void display_help()
List options and invocation syntax to stdout.
Definition: GenServer.h:375
string m_with_log_server
If 'yes', send log messages to the log server.
Definition: GenServer.h:259
SigHandlers & get_sig_manager()
Obtain reference to the Signal Manager, class SigHandls.
Definition: GenServer.h:216
bool m_version_flag
Version option flag.
Definition: GenServer.h:326
u_int m_log_size
Max size of the log file.
Definition: GenServer.h:250
void set_flags(LogFlag logf_)
New debug information is added to the old log file.
Definition: GenServer.h:176
long m_mask
Debug file mask to filter debug/error messages.
Definition: GenServer.h:266
string m_log_stdout
If 'yes', redirects all logging messages to std::cerr.
Definition: GenServer.h:298
Reactor m_reactor
GenServer object has its very own personal Reactor object.
Definition: GenServer.h:280
string get_config_file()
Get alternative configuration file name.
Definition: GenServer.h:203
GenServer & operator=(const GenServer &)
string m_default_config_file
standard configuration file name
Definition: GenServer.h:244
SIGPOLLHandler m_sig_poll
Function that swallows SIGPOLL calls.
Definition: GenServer.h:276
@ KEEPLOG
By default, append new log records to the existing log file.
Definition: GenServer.h:67
@ RMLOG
Remove existing log file and start afresh.
Definition: GenServer.h:70
string get_port()
Return assumed name of the listening port.
Definition: GenServer.h:206
void set_proc_name(string proc_name_)
Change process name.
Definition: GenServer.h:187
virtual void init_service()=0
Interface function provided for derived classes as a place to initialize specifics of derived server.
string m_port
listening port name
Definition: GenServer.h:241
LogFlag m_log_flag
Log file initialization flag. If RM_LOG, remove old log file.
Definition: GenServer.h:295
int m_exit_value
Exit value of the process.
Definition: GenServer.h:329
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
void set_version(const string &release_, int revision_)
Set Version and Revision number.
Definition: GenServer.h:351
const char * m_help_msg
Help information.
Definition: GenServer.h:292
string get_proc_name()
Get name of process+instance_number.
Definition: GenServer.h:182
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
string m_proc_name
process name (considering instance_number)
Definition: GenServer.h:235
int m_log_level
Logging level - an integer number that incrementally increases verbosity of the looing messages.
Definition: GenServer.h:310
virtual void process_events()=0
Interface function provided for derived classes as the main entry for data processing.
Reactor * get_reactor()
Obtain reference to the Reactor.
Definition: GenServer.h:221
int handle_signal(int signum_)
Handle fatal signals.
Definition: GenServer.cpp:409
GenServer(const GenServer &)
No cloning.
void set_exit_value(int v_)
Set exit value of the process. This value is returned to the shell.
Definition: GenServer.h:231
SigHandlers m_sig_dispatcher
Signal handlers dispatcher.
Definition: GenServer.h:273
void set_author(const string &author_)
Set author's name.
Definition: GenServer.h:359
string get_version()
Obtain version information.
Definition: GenServer.h:366
bool m_help_flag
Help option flag.
Definition: GenServer.h:321
void set_port(string port_)
Set listening port name.
Definition: GenServer.h:211
string m_daemon
Daemon option flag. If 'yes', become a UNIX daemon process.
Definition: GenServer.h:301
void deactivate(void)
Deactivate Reactor.
Definition: Reactor.h:234
Class SIGPOLLHandler handles SIGPOLL signal.
Definition: Handlers.h:262
Definition: Acceptor.h:40
Socket & endl(Socket &os_)
endl manipulator.
Definition: Socket.h:602
Socket & ends(Socket &os_)
ends manipulator.
Definition: Socket.h:622