Index: pjproject/trunk/pjsip-apps/src/pjsua/ios/ipjsua/ipjsuaAppDelegate.m
===================================================================
--- pjproject/trunk/pjsip-apps/src/pjsua/ios/ipjsua/ipjsuaAppDelegate.m	(revision 4464)
+++ pjproject/trunk/pjsip-apps/src/pjsua/ios/ipjsua/ipjsuaAppDelegate.m	(revision 4497)
@@ -11,5 +11,7 @@
 #import <pjsua.h>
 #import <pj/log.h>
-#include "../../pjsua_common.h"
+
+#include "../../pjsua_app.h"
+#include "../../pjsua_app_config.h"
 
 #import "ipjsuaViewController.h"
@@ -17,55 +19,108 @@
 @implementation ipjsuaAppDelegate
 
+#define THIS_FILE	"ipjsuaAppDelegate.m"
+
 #define KEEP_ALIVE_INTERVAL 600
 
-static int _argc = 4;
-static char *_argv[] = {"",
-    "--use-cli",
-    "--no-cli-console",
-    "--cli-telnet-port=6378"
-};
-
-ipjsuaAppDelegate              *app;
-extern pj_cli_telnet_on_started on_started_cb;
-extern pj_cli_on_quit           on_quit_cb;
-extern pj_cli_on_destroy        on_destroy_cb;
-extern pj_cli_on_restart_pjsua  on_restart_pjsua_cb;
-static pj_thread_desc           a_thread_desc;
-static pj_thread_t             *a_thread;
-
-int main_func(int argc, char *argv[]);
-
-static void cli_telnet_started(pj_cli_telnet_info *telnet_info)
-{
-    PJ_LOG(3,("ipjsua", "Telnet to %.*s:%d",
-	      telnet_info->ip_address.slen, telnet_info->ip_address.ptr,
-	      telnet_info->port));
-    NSString *str = [NSString stringWithFormat:@"Telnet to %.*s:%d",
-                     (int)telnet_info->ip_address.slen,
-                     telnet_info->ip_address.ptr,
-                     telnet_info->port];
+ipjsuaAppDelegate      *app;
+static pjsua_app_cfg_t  app_cfg;
+static bool             isShuttingDown;
+static char           **restartArgv;
+static int              restartArgc;
+static pj_thread_desc   a_thread_desc;
+static pj_thread_t     *a_thread;
+
+static void displayMsg(const char *msg)
+{
+    NSString *str = [NSString stringWithFormat:@"%s", msg];
     [app performSelectorOnMainThread:@selector(displayMsg:) withObject:str
-         waitUntilDone:NO];
-}
-
-static void cli_on_quit (pj_bool_t is_restarted)
-{
-    PJ_LOG(3,("ipjsua", "CLI quit, restart(%d)", is_restarted));
-    if (!is_restarted) {
-        NSString *str = [NSString stringWithFormat:@"CLI quit, "
-                         "telnet unavailable"];
-        [app performSelectorOnMainThread:@selector(displayMsg:) withObject:str
-             waitUntilDone:NO];
-    }
-}
-
-- (void)displayMsg:(NSString *)str {
+                       waitUntilDone:NO];
+}
+
+static void pjsuaOnStartedCb(pj_status_t status, const char* msg)
+{
+    char errmsg[PJ_ERR_MSG_SIZE];
+    
+    if (status != PJ_SUCCESS && (!msg || !*msg)) {
+	pj_strerror(status, errmsg, sizeof(errmsg));
+	PJ_LOG(3,(THIS_FILE, "Error: %s", errmsg));
+	msg = errmsg;
+    } else {
+	PJ_LOG(3,(THIS_FILE, "Started: %s", msg));
+    }
+
+    displayMsg(msg);
+}
+
+static void pjsuaOnStoppedCb(pj_bool_t restart,
+                             int argc, char** argv)
+{
+    PJ_LOG(3,("ipjsua", "CLI %s request", (restart? "restart" : "shutdown")));
+    if (restart) {
+        displayMsg("Restarting..");
+	pj_thread_sleep(100);
+        app_cfg.argc = argc;
+        app_cfg.argv = argv;
+    } else {
+        displayMsg("Shutting down..");
+	pj_thread_sleep(100);
+        isShuttingDown = true;
+    }
+}
+
+static void pjsuaOnAppConfigCb(pjsua_app_config *cfg)
+{
+    PJ_UNUSED_ARG(cfg);
+}
+
+- (void)displayMsg:(NSString *)str
+{
     app.viewController.textLabel.text = str;
 }
 
-- (void)start_app {
-    on_started_cb = &cli_telnet_started;
-    on_quit_cb = &cli_on_quit;
-    main_func(_argc, _argv);
+- (void)pjsuaStart
+{
+    // TODO: read from config?
+    const char **argv = pjsua_app_def_argv;
+    int argc = PJ_ARRAY_SIZE(pjsua_app_def_argv) -1;
+    pj_status_t status;
+    
+    isShuttingDown = false;
+    displayMsg("Starting..");
+    
+    pj_bzero(&app_cfg, sizeof(app_cfg));
+    if (restartArgc) {
+	app_cfg.argc = restartArgc;
+	app_cfg.argv = restartArgv;
+    } else {
+	app_cfg.argc = argc;
+	app_cfg.argv = (char**)argv;
+    }
+    app_cfg.on_started = &pjsuaOnStartedCb;
+    app_cfg.on_stopped = &pjsuaOnStoppedCb;
+    app_cfg.on_config_init = &pjsuaOnAppConfigCb;
+    
+    while (!isShuttingDown) {
+        status = pjsua_app_init(&app_cfg);
+        if (status != PJ_SUCCESS) {
+            char errmsg[PJ_ERR_MSG_SIZE];
+            pj_strerror(status, errmsg, sizeof(errmsg));
+            displayMsg(errmsg);
+            pjsua_app_destroy();
+            return;
+        }
+    
+        status = pjsua_app_run(PJ_TRUE);
+        if (status != PJ_SUCCESS) {
+            char errmsg[PJ_ERR_MSG_SIZE];
+            pj_strerror(status, errmsg, sizeof(errmsg));
+            displayMsg(errmsg);
+        }
+    
+        pjsua_app_destroy();
+    }
+    
+    restartArgv = NULL;
+    restartArgc = 0;
 }
 
@@ -85,5 +140,5 @@
     
     /* Start pjsua app thread */
-    [NSThread detachNewThreadSelector:@selector(start_app) toTarget:self withObject:nil];
+    [NSThread detachNewThreadSelector:@selector(pjsuaStart) toTarget:self withObject:nil];
 
     return YES;
