Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include "pjsua_app.h" |
| 21 | |
| 22 | #define THIS_FILE "main.c" |
| 23 | |
| 24 | static pj_bool_t running = PJ_TRUE; |
| 25 | static pj_status_t receive_end_sig; |
| 26 | static pj_thread_t *sig_thread; |
| 27 | static pjsua_app_cfg_t cfg; |
| 28 | |
| 29 | /* Called when CLI (re)started */ |
| 30 | void on_app_started(pj_status_t status, const char *msg) |
| 31 | { |
| 32 | pj_perror(3, THIS_FILE, status, (msg)?msg:""); |
| 33 | } |
| 34 | |
| 35 | void on_app_stopped(pj_bool_t restart, int argc, char** argv) |
| 36 | { |
| 37 | if (argv) { |
| 38 | cfg.argc = argc; |
| 39 | cfg.argv = argv; |
| 40 | } |
| 41 | |
| 42 | running = restart; |
| 43 | } |
| 44 | |
| 45 | #if defined(PJ_WIN32) && PJ_WIN32!=0 |
| 46 | #include <windows.h> |
| 47 | |
| 48 | static pj_thread_desc handler_desc; |
| 49 | |
| 50 | static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) |
| 51 | { |
| 52 | switch (fdwCtrlType) |
| 53 | { |
| 54 | // Handle the CTRL+C signal. |
| 55 | |
| 56 | case CTRL_C_EVENT: |
| 57 | case CTRL_CLOSE_EVENT: |
| 58 | case CTRL_BREAK_EVENT: |
| 59 | case CTRL_LOGOFF_EVENT: |
| 60 | case CTRL_SHUTDOWN_EVENT: |
| 61 | pj_thread_register("ctrlhandler", handler_desc, &sig_thread); |
| 62 | PJ_LOG(3,(THIS_FILE, "Ctrl-C detected, quitting..")); |
| 63 | receive_end_sig = PJ_TRUE; |
| 64 | pjsua_app_destroy(); |
| 65 | ExitProcess(1); |
| 66 | PJ_UNREACHED(return TRUE;) |
| 67 | |
| 68 | default: |
| 69 | |
| 70 | return FALSE; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static void setup_socket_signal() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | static void setup_signal_handler(void) |
| 79 | { |
| 80 | SetConsoleCtrlHandler(&CtrlHandler, TRUE); |
| 81 | } |
| 82 | |
| 83 | #else |
| 84 | #include <signal.h> |
| 85 | |
| 86 | static void setup_socket_signal() |
| 87 | { |
| 88 | signal(SIGPIPE, SIG_IGN); |
| 89 | } |
| 90 | |
| 91 | static void setup_signal_handler(void) {} |
| 92 | #endif |
| 93 | |
| 94 | int main(int argc, char *argv[]) |
| 95 | { |
| 96 | pj_status_t status = PJ_TRUE; |
| 97 | |
| 98 | pj_bzero(&cfg, sizeof(cfg)); |
| 99 | cfg.on_started = &on_app_started; |
| 100 | cfg.on_stopped = &on_app_stopped; |
| 101 | cfg.argc = argc; |
| 102 | cfg.argv = argv; |
| 103 | |
| 104 | setup_signal_handler(); |
| 105 | setup_socket_signal(); |
| 106 | |
| 107 | while (running) { |
| 108 | status = pjsua_app_init(&cfg); |
| 109 | if (status == PJ_SUCCESS) { |
| 110 | status = pjsua_app_run(PJ_TRUE); |
| 111 | } else { |
| 112 | running = PJ_FALSE; |
| 113 | } |
| 114 | |
| 115 | if (!receive_end_sig) { |
| 116 | pjsua_app_destroy(); |
| 117 | |
| 118 | /* This is on purpose */ |
| 119 | pjsua_app_destroy(); |
| 120 | } else { |
| 121 | pj_thread_join(sig_thread); |
| 122 | } |
| 123 | } |
| 124 | } |