-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.cpp
165 lines (125 loc) · 2.8 KB
/
app.cpp
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
#include "app.h"
#include "ace/OS.h"
#include "ace/ACE.h"
#include "ace/Get_Opt.h"
#include "ace/Thread_Manager.h"
#include "log.h"
#include "cmd_task.h"
#include <iostream>
using namespace std;
static void term_handler(int sig)
{
LOG_INFO_OS("app term ...");
get_app()->shutdown();
}
static void do_init_sig()
{
ACE_OS::signal (SIGTERM, term_handler);
ACE_OS::signal (SIGQUIT, term_handler);
ACE_OS::signal (SIGINT, term_handler);
}
static int daemonize ()
{
ACE_TRACE ("ACE::daemonize");
#if !defined (ACE_LACKS_FORK)
pid_t pid = ACE_OS::fork ();
if (pid == -1)
return -1;
else if (pid != 0)
ACE_OS::exit (0); // Parent exits.
// 1st child continues.
ACE_OS::setsid (); // Become session leader.
ACE_OS::signal (SIGHUP, SIG_IGN);
pid = ACE_OS::fork ();
if (pid != 0)
ACE_OS::exit (0); // First child terminates.
// Second child continues.
ACE_OS::umask (0); // clear our file mode creation mask.
// Close down the I/O handles.
/* if (close_all_handles)
{
for (int i = ACE::max_handles () - 1; i >= 0; i--)
ACE_OS::close (i);
int fd = ACE_OS::open ("/dev/null", O_RDWR, 0);
if (fd != -1)
{
ACE_OS::dup2 (fd, ACE_STDIN);
ACE_OS::dup2 (fd, ACE_STDOUT);
ACE_OS::dup2 (fd, ACE_STDERR);
if (fd > ACE_STDERR)
ACE_OS::close (fd);
}
}*/
return 0;
#else
//ACE_UNUSED_ARG (pathname);
// ACE_UNUSED_ARG (close_all_handles);
//ACE_UNUSED_ARG (program_name);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_LACKS_FORK */
}
App::App()
: daemon_(false),
shutdown_ (false)
{
}
App::~App()
{
}
const std::string App::config() const
{
return config_;
}
int App::main_i(int argc, char ** argv)
{
LOG_INFO_OS("app start ...");
int c;
ACE_Get_Opt getopt (argc, argv, ACE_TEXT("dp:c:"));
while ((c = getopt ()) != -1)
switch (c)
{
case 'p':
this->port_ = getopt.opt_arg ();
break;
case 'c':
config_ = getopt.opt_arg ();
break;
case 'd':
daemon_ = true;
break;
}
if (port_.empty())
{
cerr << " App's port is invalid" << endl;
return -1;
}
if (daemon_)
{
daemonize();
}
do_init_sig();
ACE::init();
cmd_task_ = new CmdTask();
if ( cmd_task_->open(port_.c_str() )!= 0)
{
cerr << " open cmd port " << port_ << " failed!" << endl;
return -1;
}
init();
while (!shutdown_)
{
ACE_OS::sleep(1);
}
fini();
cmd_task_->shutdown();
ACE_Thread_Manager::instance()->wait();
ACE::fini();
LOG_INFO_OS("app exit ...");
delete cmd_task_;
cmd_task_ =0;
return 0;
}
void App::shutdown()
{
shutdown_ = true;
}