From 1ae039e0e28c50c405801dbab1664efc477a2c53 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 15 Jan 2008 00:44:23 +0000 Subject: [PATCH] add bgapi api command git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@7231 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- .../applications/mod_commands/mod_commands.c | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index 26bfa5fd7b..5a640568c9 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -1629,6 +1629,91 @@ SWITCH_STANDARD_API(sched_api_function) } +struct bg_job { + char *cmd; + char *arg; + char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; + switch_memory_pool_t *pool; +}; + +static void *SWITCH_THREAD_FUNC bgapi_exec(switch_thread_t *thread, void *obj) +{ + + struct bg_job *job = (struct bg_job *) obj; + switch_stream_handle_t stream = { 0 }; + switch_status_t status; + char *reply, *freply = NULL; + switch_event_t *event; + char *arg; + + SWITCH_STANDARD_STREAM(stream); + + if ((arg = strchr(job->cmd, ' '))) { + *arg++ = '\0'; + } + + if ((status = switch_api_execute(job->cmd, arg, NULL, &stream)) == SWITCH_STATUS_SUCCESS) { + reply = stream.data; + } else { + freply = switch_mprintf("%s: Command not found!\n", job->cmd); + reply = freply; + } + + if (!reply) { + reply = "Command returned no output!"; + } + + if (switch_event_create(&event, SWITCH_EVENT_BACKGROUND_JOB) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Job-Command", "%s", job->cmd); + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Job-Command-Arg", "%s", arg); + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Job-UUID", "%s", job->uuid_str); + switch_event_add_body(event, "%s", reply); + switch_event_fire(&event); + } + + switch_safe_free(stream.data); + switch_safe_free(freply); + + if (job) { + switch_memory_pool_t *pool = job->pool; + job = NULL; + switch_core_destroy_memory_pool(&pool); + pool = NULL; + } + return NULL; +} + +SWITCH_STANDARD_API(bgapi_function) +{ + struct bg_job *job; + switch_uuid_t uuid; + switch_memory_pool_t *pool; + switch_thread_t *thread; + switch_threadattr_t *thd_attr = NULL; + + if (!cmd) { + stream->write_function(stream, "-ERR Invalid syntax\n"); + return SWITCH_STATUS_SUCCESS; + } + + switch_core_new_memory_pool(&pool); + job = switch_core_alloc(pool, sizeof(*job)); + job->cmd = switch_core_strdup(pool, cmd); + job->pool = pool; + + switch_uuid_get(&uuid); + switch_uuid_format(job->uuid_str, &uuid); + + switch_threadattr_create(&thd_attr, job->pool); + switch_threadattr_detach_set(thd_attr, 1); + switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); + stream->write_function(stream, "+OK %s\n", job->uuid_str); + switch_thread_create(&thread, thd_attr, bgapi_exec, job, job->pool); + + return SWITCH_STATUS_SUCCESS; +} + + struct holder { switch_stream_handle_t *stream; char *http; @@ -2146,6 +2231,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load) SWITCH_ADD_API(commands_api_interface, "sched_transfer", "Schedule a broadcast event to a running call", sched_transfer_function, SCHED_TRANSFER_SYNTAX); SWITCH_ADD_API(commands_api_interface, "create_uuid", "Create a uuid", uuid_function, UUID_SYNTAX); SWITCH_ADD_API(commands_api_interface, "sched_api", "Schedule an api command", sched_api_function, "[+]