libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 {
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)
214 
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)
272 
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:
333  GenServer (const GenServer&);
334  GenServer& operator=(const GenServer&);
335 
337  void init_internals ();
338 };
339 
340 
341 inline void
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
367 {
368  std::ostringstream v;
369  v << "Version: " << m_version << " Revision: " << m_revision << std::ends;
370  return (v.str ());
371 }
372 
373 inline void
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 */