From 643ca5c4e746b0e91781987023f7e2c08c155a64 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Fri, 30 Mar 2007 00:10:33 +0000 Subject: [PATCH] add 'show tasks' and sched_api api commands git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@4797 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_types.h | 8 +++ .../applications/mod_commands/mod_commands.c | 62 ++++++++++++++++++- src/switch_core_sqldb.c | 28 ++++++++- src/switch_core_state_machine.c | 7 +++ src/switch_event.c | 4 ++ src/switch_ivr_async.c | 2 + src/switch_scheduler.c | 42 ++++++++++++- 7 files changed, 149 insertions(+), 4 deletions(-) diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 2260ca3174..36f1c02e86 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -797,6 +797,10 @@ typedef enum { SWITCH_EVENT_PRIVATE_COMMAND - A private command event SWITCH_EVENT_HEARTBEAT - Machine is alive SWITCH_EVENT_TRAP - Error Trap + SWITCH_EVENT_ADD_SCHEDULE - Something has been scheduled + SWITCH_EVENT_DEL_SCHEDULE - Something has been unscheduled + SWITCH_EVENT_EXE_SCHEDULE - Something scheduled has been executed + SWITCH_EVENT_RE_SCHEDULE - Something scheduled has been rescheduled SWITCH_EVENT_ALL - All events at once @@ -839,6 +843,10 @@ typedef enum { SWITCH_EVENT_PRIVATE_COMMAND, SWITCH_EVENT_HEARTBEAT, SWITCH_EVENT_TRAP, + SWITCH_EVENT_ADD_SCHEDULE, + SWITCH_EVENT_DEL_SCHEDULE, + SWITCH_EVENT_EXE_SCHEDULE, + SWITCH_EVENT_RE_SCHEDULE, SWITCH_EVENT_ALL } switch_event_types_t; diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index 856d2fa97d..b4049d26c8 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -696,6 +696,56 @@ static switch_status_t originate_function(char *cmd, switch_core_session_t *ises return SWITCH_STATUS_SUCCESS;; } +static void sch_api_callback(switch_scheduler_task_t *task) +{ + char *cmd, *arg = NULL; + switch_stream_handle_t stream = { 0 }; + + assert(task); + + cmd = (char *) task->cmd_arg; + + if ((arg = strchr(cmd, ' '))) { + *arg++ = '\0'; + } + + SWITCH_STANDARD_STREAM(stream); + switch_api_execute(cmd, arg, NULL, &stream); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Command %s(%s):\n%s\n", cmd, arg, switch_str_nil((char *)stream.data)); + switch_safe_free(stream.data); +} + +static switch_status_t sched_api_function(char *cmd, switch_core_session_t *isession, switch_stream_handle_t *stream) +{ + char *tm = NULL, *dcmd; + time_t when; + + assert(cmd != NULL); + tm = strdup(cmd); + assert(tm != NULL); + + if ((dcmd = strchr(tm, ' '))) { + uint32_t id; + + *dcmd++ = '\0'; + + if (*tm == '+') { + when = time(NULL) + atol(tm + 1); + } else { + when = atol(tm); + } + id = switch_scheduler_add_task(when, sch_api_callback, (char *) __SWITCH_FUNC__, dcmd, 0, strdup(dcmd), SSHF_FREE_ARG); + stream->write_function(stream, "Added task %u\n", id); + } else { + stream->write_function(stream, "Invalid syntax\n"); + } + + switch_safe_free(tm); + + return SWITCH_STATUS_SUCCESS; +} + + struct holder { switch_stream_handle_t *stream; char *http; @@ -769,6 +819,8 @@ static switch_status_t show_function(char *cmd, switch_core_session_t *session, return SWITCH_STATUS_SUCCESS; } else if (!strcmp(cmd, "codec") || !strcmp(cmd, "dialplan") || !strcmp(cmd, "file") || !strcmp(cmd, "timer")) { sprintf(sql, "select type, name from interfaces where type = '%s'", cmd); + } else if (!strcmp(cmd, "tasks")) { + sprintf(sql, "select * from %s", cmd); } else if (!strcmp(cmd, "application") || !strcmp(cmd, "api")) { sprintf(sql, "select name, description, syntax from interfaces where type = '%s' and description != ''", cmd); } else if (!strcmp(cmd, "calls")) { @@ -849,12 +901,20 @@ static switch_status_t help_function(char *cmd, switch_core_session_t *session, return SWITCH_STATUS_SUCCESS; } +static switch_api_interface_t sched_api_api_interface = { + /*.interface_name */ "sched_api", + /*.desc */ "Schedule an api command", + /*.function */ sched_api_function, + /*.syntax */ "[+]