summaryrefslogtreecommitdiff
path: root/res/ari
diff options
context:
space:
mode:
authorGeorge Joseph <gjoseph@digium.com>2017-01-19 08:05:36 -0700
committerGeorge Joseph <gjoseph@digium.com>2017-01-23 10:25:58 -0700
commit66916067236dc2f5cf54fadee33a5203d8716148 (patch)
tree963dec954768e5eb279af597ea3fc62d9b43af59 /res/ari
parente2da0021b99ace55a36ad5d1a497159a2d077025 (diff)
ari: Implement 'debug all' and request/response logging
The 'ari set debug' command has been enhanced to accept 'all' as an application name. This allows dumping of all apps even if an app hasn't registered yet. To accomplish this, a new global_debug global variable was added to res/stasis/app.c and new APIs were added to set and query the value. 'ari set debug' now displays requests and responses as well as events. This required refactoring the existing debug code. * The implementation for 'ari set debug' was moved from stasis/cli.{c,h} to ari/cli.{c,h}, and stasis/cli.{c,h} were deleted. * In order to print the body of incoming requests even if a request failed, the consumption of the body was moved from the ari stubs to ast_ari_callback in res_ari.c and the moustache templates were then regenerated. The body is now passed to ast_ari_invoke and then on to the handlers. This results in code savings since that template was inserted multiple times into all the stubs. An additional change was made to the ao2_str_container implementation to add partial key searching and a sort function. The existing cli code assumed it was already there when it wasn't so the tab completion was never working. Change-Id: Ief936f747ce47f1fb14035fbe61152cf766406bf (cherry picked from commit 1d890874f39a5a81b20da44358143ed9b54ab0fe)
Diffstat (limited to 'res/ari')
-rw-r--r--res/ari/ari_websockets.c14
-rw-r--r--res/ari/cli.c175
-rw-r--r--res/ari/resource_events.c10
3 files changed, 195 insertions, 4 deletions
diff --git a/res/ari/ari_websockets.c b/res/ari/ari_websockets.c
index f1e63d398..4f8d6dffd 100644
--- a/res/ari/ari_websockets.c
+++ b/res/ari/ari_websockets.c
@@ -21,6 +21,7 @@
#include "asterisk/ari.h"
#include "asterisk/astobj2.h"
#include "asterisk/http_websocket.h"
+#include "asterisk/stasis_app.h"
#include "internal.h"
/*! \file
@@ -137,6 +138,7 @@ struct ast_json *ast_ari_websocket_session_read(
ast_log(LOG_WARNING,
"WebSocket input failed to parse\n");
}
+
break;
default:
/* Ignore all other message types */
@@ -172,16 +174,20 @@ int ast_ari_websocket_session_write(struct ast_ari_websocket_session *session,
return -1;
}
-#ifdef AST_DEVMODE
- ast_debug(3, "Examining ARI event (length %u): \n%s\n", (unsigned int) strlen(str), str);
-#endif
if (ast_websocket_write_string(session->ws_session, str)) {
- ast_log(LOG_NOTICE, "Problem occurred during websocket write, websocket closed\n");
+ ast_log(LOG_NOTICE, "Problem occurred during websocket write to %s, websocket closed\n",
+ ast_sockaddr_stringify(ast_ari_websocket_session_get_remote_addr(session)));
return -1;
}
return 0;
}
+struct ast_sockaddr *ast_ari_websocket_session_get_remote_addr(
+ struct ast_ari_websocket_session *session)
+{
+ return ast_websocket_remote_address(session->ws_session);
+}
+
void ari_handle_websocket(struct ast_websocket_server *ws_server,
struct ast_tcptls_session_instance *ser, const char *uri,
enum ast_http_method method, struct ast_variable *get_params,
diff --git a/res/ari/cli.c b/res/ari/cli.c
index 9d156f21e..9d0eb3099 100644
--- a/res/ari/cli.c
+++ b/res/ari/cli.c
@@ -26,6 +26,7 @@
#include "asterisk/astobj2.h"
#include "asterisk/cli.h"
+#include "asterisk/stasis_app.h"
#include "internal.h"
static char *ari_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
@@ -249,11 +250,185 @@ static char *ari_mkpasswd(struct ast_cli_entry *e, int cmd, struct ast_cli_args
return CLI_SUCCESS;
}
+static char *ari_show_apps(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ struct ao2_container *apps;
+ struct ao2_iterator it_apps;
+ char *app;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "ari show apps";
+ e->usage =
+ "Usage: ari show apps\n"
+ " Lists all registered applications.\n"
+ ;
+ return NULL;
+ case CLI_GENERATE:
+ return NULL;
+ default:
+ break;
+ }
+
+ if (a->argc != 3) {
+ return CLI_SHOWUSAGE;
+ }
+
+ apps = stasis_app_get_all();
+ if (!apps) {
+ ast_cli(a->fd, "Unable to retrieve registered applications!\n");
+ return CLI_FAILURE;
+ }
+
+ ast_cli(a->fd, "Application Name \n");
+ ast_cli(a->fd, "=========================\n");
+ it_apps = ao2_iterator_init(apps, 0);
+ while ((app = ao2_iterator_next(&it_apps))) {
+ ast_cli(a->fd, "%-25.25s\n", app);
+ ao2_ref(app, -1);
+ }
+
+ ao2_iterator_destroy(&it_apps);
+ ao2_ref(apps, -1);
+
+ return CLI_SUCCESS;
+}
+
+struct app_complete {
+ /*! Nth app to search for */
+ int state;
+ /*! Which app currently on */
+ int which;
+};
+
+static int complete_ari_app_search(void *obj, void *arg, void *data, int flags)
+{
+ struct app_complete *search = data;
+
+ if (++search->which > search->state) {
+ return CMP_MATCH;
+ }
+ return 0;
+}
+
+static char *complete_ari_app(struct ast_cli_args *a, int include_all)
+{
+ RAII_VAR(struct ao2_container *, apps, stasis_app_get_all(), ao2_cleanup);
+ RAII_VAR(char *, app, NULL, ao2_cleanup);
+
+ struct app_complete search = {
+ .state = a->n,
+ };
+
+ if (a->pos != 3) {
+ return NULL;
+ }
+
+ if (!apps) {
+ ast_cli(a->fd, "Error getting ARI applications\n");
+ return CLI_FAILURE;
+ }
+
+ if (include_all && ast_strlen_zero(a->word)) {
+ ast_str_container_add(apps, " all");
+ }
+
+ app = ao2_callback_data(apps,
+ ast_strlen_zero(a->word) ? 0 : OBJ_SEARCH_PARTIAL_KEY,
+ complete_ari_app_search, (char*)a->word, &search);
+
+ return app ? ast_strdup(app) : NULL;
+}
+
+static char *ari_show_app(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ void *app;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "ari show app";
+ e->usage =
+ "Usage: ari show app <application>\n"
+ " Provide detailed information about a registered application.\n"
+ ;
+ return NULL;
+ case CLI_GENERATE:
+ return complete_ari_app(a, 0);
+ default:
+ break;
+ }
+
+ if (a->argc != 4) {
+ return CLI_SHOWUSAGE;
+ }
+
+ app = stasis_app_get_by_name(a->argv[3]);
+ if (!app) {
+ return CLI_FAILURE;
+ }
+
+ stasis_app_to_cli(app, a);
+
+ ao2_ref(app, -1);
+
+ return CLI_SUCCESS;
+}
+
+static char *ari_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ void *app;
+ int debug;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "ari set debug";
+ e->usage =
+ "Usage: ari set debug <application|all> <on|off>\n"
+ " Enable or disable debugging on a specific application.\n"
+ ;
+ return NULL;
+ case CLI_GENERATE:
+ return complete_ari_app(a, 1);
+ default:
+ break;
+ }
+
+ if (a->argc != 5) {
+ return CLI_SHOWUSAGE;
+ }
+
+ debug = !strcmp(a->argv[4], "on");
+
+ if (!strcmp(a->argv[3], "all")) {
+ stasis_app_set_global_debug(debug);
+ ast_cli(a->fd, "Debugging on all applications %s\n",
+ debug ? "enabled" : "disabled");
+ return CLI_SUCCESS;
+ }
+
+ app = stasis_app_get_by_name(a->argv[3]);
+ if (!app) {
+ return CLI_FAILURE;
+ }
+
+ stasis_app_set_debug(app, debug);
+ ast_cli(a->fd, "Debugging on '%s' %s\n",
+ stasis_app_name(app),
+ debug ? "enabled" : "disabled");
+
+ ao2_ref(app, -1);
+
+ return CLI_SUCCESS;
+}
+
static struct ast_cli_entry cli_ari[] = {
AST_CLI_DEFINE(ari_show, "Show ARI settings"),
AST_CLI_DEFINE(ari_show_users, "List ARI users"),
AST_CLI_DEFINE(ari_show_user, "List single ARI user"),
AST_CLI_DEFINE(ari_mkpasswd, "Encrypts a password"),
+ AST_CLI_DEFINE(ari_show_apps, "List registered ARI applications"),
+ AST_CLI_DEFINE(ari_show_app, "Display details of a registered ARI application"),
+ AST_CLI_DEFINE(ari_set_debug, "Enable/disable debugging of an ARI application"),
};
int ast_ari_cli_register(void) {
diff --git a/res/ari/resource_events.c b/res/ari/resource_events.c
index 597f4dfeb..ed936f732 100644
--- a/res/ari/resource_events.c
+++ b/res/ari/resource_events.c
@@ -27,6 +27,7 @@
#include "resource_events.h"
#include "asterisk/astobj2.h"
+#include "asterisk/http_websocket.h"
#include "asterisk/stasis_app.h"
#include "asterisk/vector.h"
@@ -108,6 +109,15 @@ static void stasis_app_message_handler(
msg_type,
msg_application);
} else {
+ if (stasis_app_get_debug_by_name(app_name)) {
+ char *str = ast_json_dump_string_format(message, ast_ari_json_format());
+
+ ast_verbose("<--- Sending ARI event to %s --->\n%s\n",
+ ast_sockaddr_stringify(ast_ari_websocket_session_get_remote_addr(session->ws_session)),
+ str);
+ ast_json_free(str);
+ }
+
/* We are ready to publish the message */
ast_ari_websocket_session_write(session->ws_session, message);
}