-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.h
166 lines (116 loc) · 3.98 KB
/
application.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* application.h
*
* Created on: 2011-12-5
* Author: gxl2007@hotmail.com
*/
#ifndef APPLICATION_H_
#define APPLICATION_H_
#include <signal.h>
#include <stdio.h>
#include "epoll_reactor.h"
#include "timer_manager.h"
#include "time_util.h"
namespace xlnet
{
class application
{
public:
enum
{
STATUS_STOP = 0 ,
STATUS_RUN = 1 ,
STATUS_RELOAD = 2 ,
STATUS_PRE_STOP = 3 ,
};
enum
{
DEFAULT_INTERVAL = 600000,
} ;
public:
application() ;
virtual ~application() ;
public:
int start(int argc,char** argv) ;
void on_timeout(timer_manager* manager) ;
void on_delay_timeout(timer_manager* manager) {m_status = STATUS_STOP;} ;
void set_status(int8_t status) { m_status = status ; } ;
int8_t get_status() const { return m_status ; } ;
int64_t get_run_ms() const { return rdtsc()/tick_ms ; } ;
int set_timer(int interval_ms) ;
int set_delay_stop(int interval_ms);
void send_signal(int signo) ;
void write_self_pid();
protected:
/*
* @brief get version string , implemented derived class
*/
virtual const char* version() { return "1.0" ; } ;
/*
* @brief called when initialize , implemented derived class
* @return 0 on success , if failed ,application will exit
*/
virtual int on_init() {return 0; };
/*
* @brief called when get USR1 signal , implemented derived class
* @return 0 on success , if failed , application will exit
*/
virtual int on_reload() { return 0 ; } ;
/*
* @brief called when quit , implemented derived class
*/
virtual void on_fini() { };
/*
* @brief called when get signal , should return immediately
*/
virtual void on_signal(int signo) ;
/*
* @brief called every minute , implemented derived class
*/
virtual void on_timer() { } ;
virtual void on_delay_stop() { } ;
private:
int parse_option(int argc,char** argv) ;
void change_work_directory(const char* argv0) ;
static void show_help(const char* argv0) ;
void show_version(const char* argv0) ;
application(const application& app) ;
application& operator=(const application& app) ;
public:
epoll_reactor event_engine ;
timer_manager timer_engine ;
const int64_t tick_ms ;
const char* config_file ;
const char* work_path ;
const int32_t max_open_fd ;
private:
int32_t m_interval_ms ;
int32_t m_delay_ms ;
int8_t m_daemon ;
volatile int8_t m_status ;
template_timer<application,&application::on_timeout> m_timer ;
template_timer<application,&application::on_delay_timeout> m_delay_timer ;
};
}
#define DECLARE_APPLICATION_INSTANCE(app_type) \
app_type& get_app() ;
#define IMPLEMENT_APPLICATION_INSTANCE(app_type) \
app_type& get_app(){ static app_type app ; return app ; }
#define IMPLEMENT_MAIN() \
void sig_handler(int signo){ get_app().send_signal(signo);} \
int main(int argc,char** argv){ \
get_app() ; \
signal(SIGINT,sig_handler) ; \
signal(SIGTERM,sig_handler) ; \
signal(SIGQUIT,sig_handler) ; \
signal(SIGUSR2,sig_handler) ; \
signal(SIGUSR1,sig_handler) ; \
signal(SIGPIPE, SIG_IGN); \
signal(SIGALRM,SIG_IGN); \
signal(SIGHUP,SIG_IGN); \
return get_app().start(argc,argv);}
#define LOG_ERR(fmt, args...) error_log_format(get_app().logger, fmt, ##args)
#define LOG_INFO(fmt, args...) info_log_format(get_app().logger, fmt, ##args)
#define LOG_DEBUG(fmt, args...) debug_log_format(get_app().logger, fmt, ##args)
#define LOG_WARN(fmt, args...) warn_log_format(get_app().logger, fmt, ##args)
#endif /* APPLICATION_H_ */