summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/pjsua/main.c
diff options
context:
space:
mode:
authorRiza Sulistyo <riza@teluu.com>2013-04-19 06:05:06 +0000
committerRiza Sulistyo <riza@teluu.com>2013-04-19 06:05:06 +0000
commit5df88d5670b8860937acf820ec4483bf24def352 (patch)
tree73b3281a5499465920fa57cab590bb6478981ec9 /pjsip-apps/src/pjsua/main.c
parent2173d09f2e0a708f02af11f68f834425484d4e91 (diff)
Re #1643: Code restructure + modification to handle non blocking mode
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4476 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjsip-apps/src/pjsua/main.c')
-rw-r--r--pjsip-apps/src/pjsua/main.c104
1 files changed, 97 insertions, 7 deletions
diff --git a/pjsip-apps/src/pjsua/main.c b/pjsip-apps/src/pjsua/main.c
index 715ab349..4df0a56e 100644
--- a/pjsip-apps/src/pjsua/main.c
+++ b/pjsip-apps/src/pjsua/main.c
@@ -17,18 +17,108 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include <pjsua-lib/pjsua.h>
-
+#include "pjsua_app.h"
#define THIS_FILE "main.c"
+static pj_bool_t running = PJ_TRUE;
+static pj_status_t receive_end_sig;
+static pj_thread_t *sig_thread;
+static app_cfg_t cfg;
-/*
- * These are defined in pjsua_app.c.
- */
-int main_func(int argc, char *argv[]);
+/* Called when CLI (re)started */
+void on_app_started(pj_status_t status, const char *msg)
+{
+ pj_perror(3, THIS_FILE, status, (msg)?msg:"");
+}
+
+pj_bool_t on_app_stopped(pj_bool_t restart, int argc, char** argv)
+{
+ cfg.argc = argc;
+ cfg.argv = argv;
+
+ running = restart;
+ return PJ_TRUE;
+}
+
+#if defined(PJ_WIN32) && PJ_WIN32!=0
+#include <windows.h>
+
+static pj_thread_desc handler_desc;
+
+static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
+{
+ switch (fdwCtrlType)
+ {
+ // Handle the CTRL+C signal.
+
+ case CTRL_C_EVENT:
+ case CTRL_CLOSE_EVENT:
+ case CTRL_BREAK_EVENT:
+ case CTRL_LOGOFF_EVENT:
+ case CTRL_SHUTDOWN_EVENT:
+ pj_thread_register("ctrlhandler", handler_desc, &sig_thread);
+ PJ_LOG(3,(THIS_FILE, "Ctrl-C detected, quitting.."));
+ receive_end_sig = PJ_TRUE;
+ app_destroy();
+ ExitProcess(1);
+ PJ_UNREACHED(return TRUE;)
+
+ default:
+
+ return FALSE;
+ }
+}
+
+static void setup_socket_signal()
+{
+}
+
+static void setup_signal_handler(void)
+{
+ SetConsoleCtrlHandler(&CtrlHandler, TRUE);
+}
+
+#else
+#include <signal.h>
+
+static void setup_socket_signal()
+{
+ signal(SIGPIPE, SIG_IGN);
+}
+
+static void setup_signal_handler(void) {}
+#endif
int main(int argc, char *argv[])
{
- return pj_run_app(&main_func, argc, argv, 0);
+ pj_status_t status = PJ_TRUE;
+
+ pj_bzero(&cfg, sizeof(cfg));
+ cfg.on_started = &on_app_started;
+ cfg.on_stopped = &on_app_stopped;
+ cfg.argc = argc;
+ cfg.argv = argv;
+
+ setup_signal_handler();
+ setup_socket_signal();
+
+ while (running) {
+ status = app_init(&cfg);
+ if (status == PJ_SUCCESS) {
+ status = app_run(PJ_TRUE);
+ } else {
+ pj_perror(3, THIS_FILE, status, "Failed init");
+ running = PJ_FALSE;
+ }
+
+ if (!receive_end_sig) {
+ app_destroy();
+
+ /* This is on purpose */
+ app_destroy();
+ } else {
+ pj_thread_join(sig_thread);
+ }
+ }
}