-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
269 lines (228 loc) · 7.24 KB
/
Copy pathmain.cpp
File metadata and controls
269 lines (228 loc) · 7.24 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Copyright (C) 2014 Dialogic Corp.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
* Alternatively see <http://www.gnu.org/licenses/>.
* Or see the LICENSE file included within the source tree.
*
*/
/*------------------------------- Dependencies -------------------------------*/
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <pthread.h>
#include <iostream>
#include "logger.h"
#include "getoption.h"
#include "appframework.h"
/*----------------------------------------------------------------------------*/
/* PACKAGE_VERSION is defined by the autotools.
*/
#ifdef PACKAGE_VERSION
static const char *APP_VERSION = PACKAGE_VERSION;
#else
static const char *APP_VERSION = __DATE__ " " __TIME__;
#endif
static const char *APP_NAME = "restconfdemo";
static const char *APP_DESCRIPTION = "PowerMedia XMS - REST Conferencing C++ Demo Application";
static const char *PID_FILE = "/var/run/restconfdemo.pid";
// Event queue and locks
std::queue<std::string> eventQueue;
pthread_mutex_t queueLock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t queueNotEmpty = PTHREAD_COND_INITIALIZER;
pthread_t evHandlerThread;
// REST service address/port
std::string xmsAddr;
static std::string eventHandlerId;
/*!
* \var exit_pipe
* Pipe used by signal handlers to tell the application to exit cleanly.
*/
static int exit_pipe[2];
int term = false;
int log_restart = false;
/*!
* Function prototype for signal handlers.
*/
typedef void (signal_handler_t) (int);
/*!
* Helper to install signal handler functions.
*/
signal_handler_t *
setSignalHandler (int signum, signal_handler_t * handler)
{
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset (&sa.sa_mask);
sa.sa_flags = 0;
struct sigaction old_sa;
sigaction (signum, &sa, &old_sa);
return old_sa.sa_handler;
}
/*!
* Signal handler for termination signal.
* Write the signal's id to the exit_pipe.
*/
void
sig_terminate (int signo)
{
LOGDEBUG("SIGTERM received. Shutting down application");
// Send a signal to main thread to break it loose from waiting on
// the event queue
pthread_cond_signal(&queueNotEmpty);
char sig = (char) signo;
write (exit_pipe[1], &sig, 1);
term = true;
}
/*!
* Signal handler for SIGHUP.
*/
void
sig_reload (int signo)
{
(void) signo;
log_restart = true;
}
/*!
* Signal handler for SIGCHLD. Call wait() to remove zombie.
*/
void
sig_child_exit (int signo)
{
(void) signo;
wait (NULL);
}
/*!
* Entry point. Do the standard initialisation and pass control to the
* application manager.
*/
int
main (int argc, char *argv[])
{
/* Parse command line options.
*/
GetOptions opts;
opts.addOptionNoArg ('h', "help", "Display this information.");
opts.addOptionNoArg ('\0', "version", "Display this applications's version.");
opts.addOptionNoArg ('v', "verbose", "Run with maximum logging.");
opts.addOptionNoArg ('c', "console", "Run as a console application.");
opts.addOptionRequiredArg ('\0', "log-dir", "Directory used to store log files.");
opts.addOptionRequiredArg ('d', "dtmf-mode", "DTMF type - rfc2833 or sipinfo");
opts.addOptionRequiredArg ('a', "ip-address", "XMS server IP address");
opts.addOptionRequiredArg ('p', "port", "XMS server REST messaging port");
opts.parseOptions (argc, argv);
if (opts.isFound ("help"))
{
std::cout << "Command line options:" << std::endl << opts << std::endl;
exit (0);
}
if (opts.isFound ("version"))
{
std::cout << APP_DESCRIPTION << ". Version: " << APP_VERSION << std::endl << std::endl;
std::cout << "Copyright (C) 2014 Dialogic Corp. " << std::endl << std::endl;
exit (0);
}
/* Setup logging
*/
if (opts.isFound ("verbose"))
{
Logger::instance ().setLevel (Logger::LOGLEVEL_DEBUG);
}
else
{
Logger::instance ().setLevel (Logger::LOGLEVEL_NOTICE);
}
std::string opt_log_dir = opts.getValue ("log-dir");
if (opt_log_dir.empty ())
{
opt_log_dir = ".";
}
Logger::instance ().attachAppender (new FileAppender (opt_log_dir, APP_NAME, 1024 * 1024 * 4));
Logger::instance ().attachAppender (new SyslogAppender (APP_NAME));
if (opts.isFound ("console"))
{
Logger::instance ().attachAppender (new ConsoleAppender);
}
else
{
if (daemon (1, 0) == -1) /* don't chdir, redirect stdio to /dev/null */
{
LOGCRIT ("main() daemon() failed " << errno);
exit (1);
}
/* Create a file containing the pid of this process. This is used
* by the system when managing this process. It also locks the file
* ensure that only one instance of this program can be run.
*/
int fd = open (PID_FILE, O_RDWR | O_CREAT, 0640);
if (fd < 0)
{
LOGCRIT ("main() failed to open lock file: " << PID_FILE);
exit (EXIT_FAILURE);
}
if (lockf (fd, F_TLOCK, 0) < 0)
{
/* already running */
exit (0);
}
std::stringstream pid;
pid << getpid () << std::endl;
write (fd, pid.str ().c_str (), pid.str ().length ());
}
/* Create 'selfpipe' for sig handlers
*/
pipe (exit_pipe);
fcntl (exit_pipe[0], F_SETFL, O_NONBLOCK | fcntl (exit_pipe[0], F_GETFL));
fcntl (exit_pipe[1], F_SETFL, O_NONBLOCK | fcntl (exit_pipe[1], F_GETFL));
/* Signal handlers
*/
setSignalHandler (SIGPIPE, SIG_IGN);
setSignalHandler (SIGINT, sig_terminate);
setSignalHandler (SIGQUIT, sig_terminate);
setSignalHandler (SIGTERM, sig_terminate);
setSignalHandler (SIGHUP, sig_reload);
setSignalHandler (SIGCHLD, sig_child_exit);
/* Log version and the command line options.
*/
LOGNOTICE ("Starting " << APP_NAME << " " << APP_VERSION << " Built: " << __DATE__ << " " << __TIME__);
std::map < std::string, std::string > opt_values;
opts.getAllValues (opt_values);
std::map < std::string, std::string >::const_iterator i;
for (i = opt_values.begin (); i != opt_values.end (); ++i)
{
LOGNOTICE ("Option: " << (*i).first << ((*i).second.empty ()? "" : "=" + (*i).second));
}
/* Pass control to the application
*/
AppFramework *app = new AppFramework;
bool exit_status = app->run (opts, exit_pipe[0]);
delete app;
close (exit_pipe[0]);
close (exit_pipe[1]);
if (!exit_status)
{
LOGWARN ("Abnormal exit");
return 1;
}
LOGNOTICE ("Normal exit");
return 0;
}
/* vim:ts=4:set nu:
* EOF
*/