00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef _terimber_daemon_h_
00028 #define _terimber_daemon_h_
00029
00030 #include "allinc.h"
00031 #include "base/string.h"
00032 #include "base/primitives.h"
00033 #include "threadpool/timer.h"
00034 #include "log.h"
00035
00036 BEGIN_TERIMBER_NAMESPACE
00037 #pragma pack(4)
00038
00039 #if OS_TYPE == OS_WIN32
00040 #define STATIC_FUNCTION_DECL WINAPI
00041 #define DAEMON_HANDLE_FUNCTION_ARG DWORD
00042 #else
00043 #define STATIC_FUNCTION_DECL
00044 #define DAEMON_HANDLE_FUNCTION_ARG int
00045 #endif
00046
00049 class daemon : public terimber_log,
00050 public timer_callback
00051 {
00052 protected:
00054 daemon();
00056 virtual ~daemon();
00057
00059 virtual
00060 bool
00061 v_is_logging( size_t module,
00062 const char* file,
00063 size_t line,
00064 terimber_log_severity severity
00065 ) const;
00067 virtual
00068 void
00069 v_do_logging( size_t module,
00070 const char* file,
00071 size_t line,
00072 terimber_log_severity severity,
00073 const char* msg
00074 ) const;
00075
00076
00077 protected:
00078 string_t _daemonName;
00079 string_t _daemonDesc;
00080
00081
00082 public:
00084 static
00085 int
00086 s_daemon_main( unsigned long argc,
00087 char* argv[]
00088 );
00090 int
00091 daemon_main( unsigned long argc,
00092 char* argv[]
00093 );
00094
00095
00096 protected:
00100 virtual
00101 bool
00102 v_find_cfg_file(string_t& cfg_file
00103 );
00106 virtual
00107 bool
00108 v_on_init( unsigned long argc,
00109 char* argv[],
00110 const char* cfg_file,
00111 TERIMBER::string_t& err
00112 ) = 0;
00114 virtual
00115 void
00116 v_on_uninit() = 0;
00118 virtual
00119 void
00120 v_on_handler( DAEMON_HANDLE_FUNCTION_ARG code
00121 )
00122 {
00123 }
00124
00126 virtual
00127 void
00128 notify( size_t ident,
00129 size_t interval,
00130 size_t multiplier
00131 );
00132
00134 bool
00135 on_startup( unsigned long argc,
00136 char* argv[]
00137 );
00139 void
00140 on_shutdown();
00141 void
00142 on_stop();
00143
00144 private:
00146 void
00147 on_run();
00149 bool
00150 is_installed();
00152 bool
00153 install();
00155 bool
00156 uninstall();
00158 void
00159 daemon_start( unsigned long argc,
00160 char* argv[]
00161 );
00163 void
00164 daemon_handler(DAEMON_HANDLE_FUNCTION_ARG cmd
00165 );
00167 void
00168 log_message( bool err,
00169 const char* format,
00170 ...
00171 ) const;
00172
00173 #if OS_TYPE == OS_WIN32
00175 bool
00176 set_state( DWORD nState
00177 );
00178 #endif
00179 private:
00181 static
00182 void
00183 STATIC_FUNCTION_DECL
00184 s_daemon_start( unsigned long argc,
00185 char* argv[]
00186 );
00188 static
00189 void
00190 STATIC_FUNCTION_DECL
00191 s_daemon_handler(DAEMON_HANDLE_FUNCTION_ARG cmd
00192 );
00193
00195 static
00196 void
00197 find_full_path( char* path,
00198 char*& start,
00199 size_t& len
00200 );
00201
00202 protected:
00203 size_t _severity;
00204
00205 private:
00206 static daemon* g_daemon;
00207 bool _isDaemon;
00208 event _hShutdown;
00209
00210 timer _log_timer;
00211
00212 #if OS_TYPE == OS_WIN32
00213 SERVICE_STATUS_HANDLE _hStatus;
00214 SERVICE_STATUS _pStatus;
00215 #endif
00216 };
00217
00218
00219 #pragma pack()
00220 END_TERIMBER_NAMESPACE
00221
00222 #endif