diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index 32293bb089..3a85ddd9ad 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -950,7 +950,7 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t const void *vvar; int c = 0; int ac = 0; - const char *line = "======================================================================================="; + const char *line = "================================================================================================="; if (argc > 0) { @@ -1045,14 +1045,16 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t if (strcmp(vvar, profile->name)) { ac++; - stream->write_function(stream, "%25s\t%s\t %32s\t%s\n", vvar, " alias", profile->name, "LOADED"); + stream->write_function(stream, "%25s\t%s\t %32s\t%s\n", vvar, " alias", profile->name, "ALIASED"); } else { - stream->write_function(stream, "%25s\t%s\t %32s\t%s\n", profile->name, "profile", profile->url, "LOADED"); + stream->write_function(stream, "%25s\t%s\t %32s\t%s\n", profile->name, "profile", profile->url, + sofia_test_pflag(profile, PFLAG_RUNNING) ? "RUNNING" : "DOWN"); c++; for (gp = profile->gateways; gp; gp = gp->next) { assert(gp->state < REG_STATE_LAST); stream->write_function(stream, "%25s\t%s\t %32s\t%s\n", gp->name, "gateway", gp->register_to, sofia_state_names[gp->state]); + } } @@ -1061,7 +1063,7 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t } switch_mutex_unlock(mod_sofia_globals.hash_mutex); stream->write_function(stream, "%s\n", line); - stream->write_function(stream, "%d profiles %d aliases\n", c, ac); + stream->write_function(stream, "%d profile%s %d alias%s\n", c, c == 1 ? "" : "s", ac, ac == 1 ? "" : "es"); return SWITCH_STATUS_SUCCESS; } @@ -1105,9 +1107,15 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t sofia_clear_pflag_locked(profile, PFLAG_RUNNING); stream->write_function(stream, "stopping: %s", profile->name); } else if (!strcasecmp(argv[1], "restart")) { - sofia_set_pflag_locked(profile, PFLAG_RESPAWN); - sofia_clear_pflag_locked(profile, PFLAG_RUNNING); - stream->write_function(stream, "restarting: %s", profile->name); + int rsec = 30; + + if (time(NULL) - profile->started < rsec) { + stream->write_function(stream, "Profile %s must be up for at least %d seconds to restart\n", rsec, profile->name); + } else { + sofia_set_pflag_locked(profile, PFLAG_RESPAWN); + sofia_clear_pflag_locked(profile, PFLAG_RUNNING); + stream->write_function(stream, "restarting: %s", profile->name); + } } if (profile) { diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index 85879e2ab4..a965957807 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -240,6 +240,7 @@ struct sofia_profile { switch_thread_rwlock_t *rwlock; switch_mutex_t *flag_mutex; uint32_t inuse; + time_t started; #ifdef SWITCH_HAVE_ODBC char *odbc_dsn; char *odbc_user; diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index 5e4c3abeee..69c8d11eab 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -262,6 +262,13 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Creating agent for %s\n", profile->name); + if (!sofia_glue_init_sql(profile)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database [%s]!\n", profile->name); + sofia_glue_del_profile(profile); + goto end; + } + + profile->nua = nua_create(profile->s_root, /* Event loop */ sofia_event_callback, /* Callback for processing events */ profile, /* Additional data to pass to callback */ @@ -269,6 +276,7 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void if (!profile->nua) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Creating SIP UA for profile: %s\n", profile->name); + sofia_glue_del_profile(profile); goto end; } @@ -313,10 +321,6 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void } - if (!sofia_glue_init_sql(profile)) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n"); - goto end; - } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "activated db for %s\n", profile->name); @@ -343,8 +347,11 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Starting thread for %s\n", profile->name); - + profile->started = time(NULL); + switch_yield(1000000); + sofia_set_pflag_locked(profile, PFLAG_RUNNING); + while (mod_sofia_globals.running == 1 && sofia_test_pflag(profile, PFLAG_RUNNING)) { if (++ireg_loops >= IREG_SECONDS) { sofia_reg_check_expire(profile, time(NULL)); @@ -363,8 +370,12 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void switch_yield(500000); } + + //sofia_reg_check_expire(profile, 0); //sofia_reg_check_gateway(profile, 0); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write lock %s\n", profile->name); switch_thread_rwlock_wrlock(profile->rwlock); sofia_reg_unregister(profile); nua_shutdown(profile->nua); @@ -393,6 +404,7 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void sofia_glue_del_profile(profile); switch_thread_rwlock_unlock(profile->rwlock); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write unlock %s\n", profile->name); if (sofia_test_pflag(profile, PFLAG_RESPAWN)) { config_sofia(1, profile->name); @@ -402,6 +414,7 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void end: + switch_mutex_lock(mod_sofia_globals.mutex); mod_sofia_globals.threads--; diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c index 76ca768f59..ea73711285 100644 --- a/src/mod/endpoints/mod_sofia/sofia_glue.c +++ b/src/mod/endpoints/mod_sofia/sofia_glue.c @@ -1426,10 +1426,14 @@ sofia_profile_t *sofia_glue_find_profile(char *key) switch_mutex_lock(mod_sofia_globals.hash_mutex); if ((profile = (sofia_profile_t *) switch_core_hash_find(mod_sofia_globals.profile_hash, key))) { if (!sofia_test_pflag(profile, PFLAG_RUNNING)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is not running\n", profile->name); profile = NULL; } else if (switch_thread_rwlock_tryrdlock(profile->rwlock) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", profile->name); profile = NULL; } + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is not in the hash\n", profile->name); } switch_mutex_unlock(mod_sofia_globals.hash_mutex); @@ -1545,7 +1549,7 @@ int sofia_glue_init_sql(sofia_profile_t *profile) } #endif - return 1; + return profile->master_odbc ? 1 : 0; } void sofia_glue_sql_close(sofia_profile_t *profile)