Debian Codegraph @

Xpjproject/pjsip-apps/src/pjsua/main.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "pjsua_app.h"
20
21 #define THIS_FILE "main.c"
22
23 static pj_bool_t running = PJ_TRUE;
24 static pj_status_t receive_end_sig;
25 static pj_thread_t *sig_thread;
26 static pjsua_app_cfg_t cfg;
27
28 /* Called when CLI (re)started */
29 void on_app_started(pj_status_t status, const char *msg)
30 {
31 pj_perror(3, THIS_FILE, status, "%s", (msg)?msg:"");
32 }
33
34 void on_app_stopped(pj_bool_t restart, int argc, char** argv)
35 {
36 if (argv) {
37 cfg.argc = argc;
38 cfg.argv = argv;
39 }
40
41 running = restart;
42 }
43
44 #if defined(PJ_WIN32) && PJ_WIN32!=0
45 #include <windows.h>
46
47 static pj_thread_desc handler_desc;
48
49 static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
50 {
51 switch (fdwCtrlType)
52 {
53 // Handle the CTRL+C signal.
54
55 case CTRL_C_EVENT:
56 case CTRL_CLOSE_EVENT:
57 case CTRL_BREAK_EVENT:
58 case CTRL_LOGOFF_EVENT:
59 case CTRL_SHUTDOWN_EVENT:
60 pj_thread_register("ctrlhandler", handler_desc, &sig_thread);
61 PJ_LOG(3,(THIS_FILE, "Ctrl-C detected, quitting.."));
62 receive_end_sig = PJ_TRUE;
63 pjsua_app_destroy();
64 ExitProcess(1);
65 PJ_UNREACHED(return TRUE;)
66
67 default:
68
69 return FALSE;
70 }
71 }
72
73 static void setup_socket_signal()
74 {
75 }
76
77 static void setup_signal_handler(void)
78 {
79 SetConsoleCtrlHandler(&CtrlHandler, TRUE);
80 }
81
82 #elif (PJ_LINUX || PJ_DARWINOS) && defined(PJ_HAS_EXECINFO_H) && PJ_HAS_EXECINFO_H != 0
83
84 #include <execinfo.h>
85 #include <signal.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <unistd.h>
89 static void print_stack(int sig)
90 {
91 void *array[16];
92 size_t size;
93
94 size = backtrace(array, 16);
95 fprintf(stderr, "Error: signal %d:\n", sig);
96 backtrace_symbols_fd(array, size, STDERR_FILENO);
97 exit(1);
98 }
99
100 static void setup_socket_signal()
101 {
102 signal(SIGPIPE, SIG_IGN);
103 }
104
105 static void setup_signal_handler(void)
106 {
107 signal(SIGSEGV, &print_stack);
108 signal(SIGABRT, &print_stack);
109 }
110
111 #else
112
113 #include <signal.h>
114
115 static void setup_socket_signal()
116 {
117 signal(SIGPIPE, SIG_IGN);
118 }
119
120 static void setup_signal_handler(void) {}
121
122 #endif
123
124 int main_func(int argc, char *argv[])
125 {
126 pj_bzero(&cfg, sizeof(cfg));
127 cfg.on_started = &on_app_started;
128 cfg.on_stopped = &on_app_stopped;
129 cfg.argc = argc;
130 cfg.argv = argv;
131
132 setup_signal_handler();
133 setup_socket_signal();
134
135 while (running) {
136 if (pjsua_app_init(&cfg) == PJ_SUCCESS) {
137 pjsua_app_run(PJ_TRUE);
138 } else {
139 running = PJ_FALSE;
140 }
141
142 if (!receive_end_sig) {
143 pjsua_app_destroy();
144
145 /* This is on purpose */
146 pjsua_app_destroy();
147 } else {
148 pj_thread_join(sig_thread);
149 }
150 }
151 return 0;
152 }
153
154 int main(int argc, char *argv[])
155 {
156 pj_log_set_level(1);
157
158 return pj_run_app(&main_func, argc, argv, 0);
159 }