FSCORE-297

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12173 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale
2009-02-20 01:10:59 +00:00
parent 32f4635068
commit f125872c73
13 changed files with 221 additions and 93 deletions
@@ -40,7 +40,9 @@
#include <switch_version.h>
SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load);
SWITCH_MODULE_DEFINITION(mod_commands, mod_commands_load, NULL, NULL);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_commands_shutdown);
SWITCH_MODULE_DEFINITION(mod_commands, mod_commands_load, mod_commands_shutdown, NULL);
SWITCH_STANDARD_API(time_test_function)
{
@@ -2289,6 +2291,8 @@ SWITCH_STANDARD_API(sched_api_function)
return SWITCH_STATUS_SUCCESS;
}
static switch_thread_rwlock_t *bgapi_rwlock = NULL;
struct bg_job {
char *cmd;
char *arg;
@@ -2309,6 +2313,8 @@ static void *SWITCH_THREAD_FUNC bgapi_exec(switch_thread_t *thread, void *obj)
if (!job)
return NULL;
switch_thread_rwlock_rdlock(bgapi_rwlock);
pool = job->pool;
SWITCH_STANDARD_STREAM(stream);
@@ -2345,6 +2351,9 @@ static void *SWITCH_THREAD_FUNC bgapi_exec(switch_thread_t *thread, void *obj)
job = NULL;
switch_core_destroy_memory_pool(&pool);
pool = NULL;
switch_thread_rwlock_unlock(bgapi_rwlock);
return NULL;
}
@@ -3183,11 +3192,37 @@ SWITCH_STANDARD_API(hupall_api_function)
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_commands_shutdown)
{
int x;
for (x = 30; x > 0; x--) {
if (switch_thread_rwlock_trywrlock(bgapi_rwlock) == SWITCH_STATUS_SUCCESS) {
switch_thread_rwlock_unlock(bgapi_rwlock);
break;
}
if (x == 30) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Waiting for bgapi threads.\n");
}
switch_yield(1000000);
}
if (!x) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Giving up waiting for bgapi threads.\n");
}
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load)
{
switch_api_interface_t *commands_api_interface;
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
switch_thread_rwlock_create(&bgapi_rwlock, pool);
SWITCH_ADD_API(commands_api_interface, "group_call", "Generate a dial string to call a group", group_call_function, "<group>[@<domain>]");
SWITCH_ADD_API(commands_api_interface, "in_group", "determine if a user is in a group", in_group_function, "<user>[@<domain>] <group_name>");
@@ -116,6 +116,7 @@ static struct {
static void remove_listener(listener_t *listener);
static void kill_all_listeners(void);
static uint32_t next_id(void)
{
@@ -452,32 +453,25 @@ static void close_socket(switch_socket_t **sock)
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_event_socket_shutdown)
{
listener_t *l;
int sanity = 0;
prefs.done = 1;
kill_all_listeners();
switch_log_unbind_logger(socket_logger);
close_socket(&listen_list.sock);
while (prefs.threads) {
switch_yield(10000);
if (++sanity == 1000) {
switch_yield(100000);
kill_all_listeners();
if (++sanity >= 200) {
break;
}
}
switch_event_unbind(&globals.node);
switch_mutex_lock(globals.listener_mutex);
for (l = listen_list.listeners; l; l = l->next) {
close_socket(&l->sock);
}
switch_mutex_unlock(globals.listener_mutex);
switch_yield(1000000);
return SWITCH_STATUS_SUCCESS;
}
@@ -509,6 +503,22 @@ static void remove_listener(listener_t *listener)
}
static void kill_all_listeners(void)
{
listener_t *l;
switch_mutex_lock(globals.listener_mutex);
for (l = listen_list.listeners; l; l = l->next) {
switch_clear_flag(l, LFLAG_RUNNING);
if (l->sock) {
switch_socket_shutdown(l->sock, SWITCH_SHUTDOWN_READWRITE);
switch_socket_close(l->sock);
}
}
switch_mutex_unlock(globals.listener_mutex);
}
static listener_t *find_listener(uint32_t id)
{
listener_t *l, *r = NULL;
@@ -961,12 +971,16 @@ static switch_status_t read_packet(listener_t *listener, switch_event_t **event,
uint32_t max_len = sizeof(mbuf);
switch_channel_t *channel = NULL;
int clen = 0;
*event = NULL;
if (prefs.done) {
return SWITCH_STATUS_FALSE;
}
start = switch_epoch_time_now(NULL);
ptr = mbuf;
if (listener->session) {
channel = switch_core_session_get_channel(listener->session);
}
@@ -976,7 +990,7 @@ static switch_status_t read_packet(listener_t *listener, switch_event_t **event,
mlen = 1;
status = switch_socket_recv(listener->sock, ptr, &mlen);
if (!SWITCH_STATUS_IS_BREAK(status) && status != SWITCH_STATUS_SUCCESS) {
if (prefs.done || (!SWITCH_STATUS_IS_BREAK(status) && status != SWITCH_STATUS_SUCCESS)) {
return SWITCH_STATUS_FALSE;
}
@@ -1044,7 +1058,7 @@ static switch_status_t read_packet(listener_t *listener, switch_event_t **event,
status = switch_socket_recv(listener->sock, p, &mlen);
if (!SWITCH_STATUS_IS_BREAK(status) && status != SWITCH_STATUS_SUCCESS) {
if (prefs.done || (!SWITCH_STATUS_IS_BREAK(status) && status != SWITCH_STATUS_SUCCESS)) {
return SWITCH_STATUS_FALSE;
}
@@ -1211,10 +1225,15 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
switch_stream_handle_t stream = { 0 };
char *reply, *freply = NULL;
switch_status_t status;
switch_mutex_lock(globals.listener_mutex);
prefs.threads++;
switch_mutex_unlock(globals.listener_mutex);
if (!acs) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Internal error.\n");
return NULL;
goto cleanup;
}
if (!acs->listener || !switch_test_flag(acs->listener, LFLAG_RUNNING) ||
@@ -1291,6 +1310,11 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
}
cleanup:
switch_mutex_lock(globals.listener_mutex);
prefs.threads--;
switch_mutex_unlock(globals.listener_mutex);
return NULL;
}
@@ -1934,7 +1958,7 @@ static void *SWITCH_THREAD_FUNC listener_run(switch_thread_t *thread, void *obj)
}
}
while (switch_test_flag(listener, LFLAG_RUNNING) && listen_list.ready) {
while (!prefs.done && switch_test_flag(listener, LFLAG_RUNNING) && listen_list.ready) {
len = sizeof(buf);
memset(buf, 0, len);
status = read_packet(listener, &revent, 0);
@@ -2106,7 +2130,7 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_event_socket_runtime)
config();
for (;;) {
while(!prefs.done) {
rv = switch_sockaddr_info_get(&sa, prefs.ip, SWITCH_INET, prefs.port, 0, pool);
if (rv)
goto fail;
@@ -2132,7 +2156,7 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_event_socket_runtime)
listen_list.ready = 1;
for (;;) {
while(!prefs.done) {
if (switch_core_new_memory_pool(&listener_pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "OH OH no pool\n");
goto fail;
@@ -222,13 +222,13 @@ static void *SWITCH_THREAD_FUNC read_stream_thread(switch_thread_t *thread, void
if (!used && !is_open) {
break;
}
if (!is_open || used >= source->prebuf || (source->total && used > source->samples * 2)) {
used = switch_buffer_read(audio_buffer, dist_buf, source->samples * 2);
if (source->total) {
switch_mutex_lock(source->mutex);
for (cp = source->context_list; cp; cp = cp->next) {
for (cp = source->context_list; cp && RUNNING; cp = cp->next) {
if (switch_test_flag(cp->handle, SWITCH_FILE_CALLBACK)) {
continue;
}