[asterisk-commits] dlee: branch dlee/stasis-http r380834 - in /team/dlee/stasis-http: apps/ incl...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Mon Feb 4 15:12:14 CST 2013
Author: dlee
Date: Mon Feb 4 15:12:12 2013
New Revision: 380834
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=380834
Log:
Cleaned up
Modified:
team/dlee/stasis-http/apps/app_stasis.c
team/dlee/stasis-http/include/asterisk/stasis_app.h
team/dlee/stasis-http/res/res_stasis_core.c
team/dlee/stasis-http/tests/test_stasis_app.c
Modified: team/dlee/stasis-http/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/apps/app_stasis.c?view=diff&rev=380834&r1=380833&r2=380834
==============================================================================
--- team/dlee/stasis-http/apps/app_stasis.c (original)
+++ team/dlee/stasis-http/apps/app_stasis.c Mon Feb 4 15:12:12 2013
@@ -32,6 +32,7 @@
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/app.h"
+#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/stasis_app.h"
@@ -57,15 +58,68 @@
static const char *stasis = "Stasis";
+static int send_start_msg(
+ const char *app_name, struct ast_channel *chan, int argc, char *argv[])
+{
+ RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+ struct ast_json *json_args;
+ int i;
+
+ ast_assert(chan != NULL);
+
+ msg = ast_json_pack("{s: s, s: s, s: []}",
+ "command", "start",
+ "channel", ast_channel_uniqueid(chan),
+ "args");
+ if (!msg) {
+ ast_log(LOG_ERROR, "Couldn't create message for %s\n", app_name);
+ return -1;
+ }
+
+ /* Append arguments to args array */
+ json_args = ast_json_object_get(msg, "args");
+ ast_assert(json_args != NULL);
+ for (i = 0; i < argc; ++i) {
+ if (!ast_json_array_append(json_args, ast_json_string_create(argv[i]))) {
+ ast_log(LOG_ERROR, "Error appending arg to start message");
+ return -1;
+ }
+ }
+
+ return stasis_app_send(app_name, msg);
+}
+
+static int send_end_msg(const char *app_name, struct ast_channel *chan)
+{
+ RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+
+ ast_assert(chan != NULL);
+
+ msg = ast_json_pack("{s: s, s: s}",
+ "command", "start",
+ "channel", ast_channel_uniqueid(chan),
+ "args");
+ if (!msg) {
+ ast_log(LOG_ERROR, "Couldn't create message for %s\n", app_name);
+ return -1;
+ }
+
+ return stasis_app_send(app_name, msg);
+}
+
/*! /brief Stasis dialplan application callback */
static int stasis_exec(struct ast_channel *chan, const char *data)
{
+ int res = 0;
char *parse = NULL;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(app_name);
AST_APP_ARG(app_argv)[MAX_ARGS];
);
+
+ ast_assert(chan != NULL);
+ ast_assert(data != NULL);
/* parse the arguments */
parse = ast_strdupa(data);
@@ -76,7 +130,19 @@
return -1;
}
- return stasis_app_invoke(args.app_name, chan, args.argc - 1, args.app_argv);
+ res = send_start_msg(args.app_name, chan, args.argc - 1, args.app_argv);
+ if (res != 0) {
+ ast_log(LOG_ERROR, "Error sending start message to %s\n", args.app_name);
+ return res;
+ }
+
+ res = send_end_msg(args.app_name, chan);
+ if (res != 0) {
+ ast_log(LOG_ERROR, "Error sending start message to %s\n", args.app_name);
+ return res;
+ }
+
+ return res;
}
static int load_module(void)
Modified: team/dlee/stasis-http/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/stasis_app.h?view=diff&rev=380834&r1=380833&r2=380834
==============================================================================
--- team/dlee/stasis-http/include/asterisk/stasis_app.h (original)
+++ team/dlee/stasis-http/include/asterisk/stasis_app.h Mon Feb 4 15:12:12 2013
@@ -32,15 +32,13 @@
#include "asterisk/json.h"
/*!
- * \brief Invoke stasis application for a given channel, with the given arguments.
+ * \brief Send a message to the given Stasis application
* \param app_name Name of the application to invoke
- * \param chan Channel to invoke application upon
- * \param argc Number of arguments to pass into the application
- * \param argv Argument array.
+ * \param message Message to send (borrowed reference)
* \return 0 for success.
* \return -1 for error.
*/
-int stasis_app_invoke(const char *app_name, struct ast_channel *chan, int argc, char *argv[]);
+int stasis_app_send(const char *app_name, struct ast_json *message);
/*!
* \brief Callback for Stasis application handler.
Modified: team/dlee/stasis-http/res/res_stasis_core.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/res/res_stasis_core.c?view=diff&rev=380834&r1=380833&r2=380834
==============================================================================
--- team/dlee/stasis-http/res/res_stasis_core.c (original)
+++ team/dlee/stasis-http/res/res_stasis_core.c Mon Feb 4 15:12:12 2013
@@ -89,13 +89,10 @@
ast_free(app->name);
}
-int stasis_app_invoke(const char *app_name, struct ast_channel *chan, int argc, char *argv[])
+int stasis_app_send(const char *app_name, struct ast_json *message)
{
RAII_VAR(struct ao2_container *, apps, stasis_apps(), ao2_cleanup);
RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
- RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
- struct ast_json *json_args;
- int i;
app = ao2_find(apps, app_name, OBJ_KEY);
@@ -107,22 +104,7 @@
return -1;
}
- msg = ast_json_pack("{s: s, s: o, s: []}",
- "command", "invoke",
- "channel", chan ? ast_json_string_create(ast_channel_name(chan)) : ast_json_null(),
- "args");
- if (!msg) {
- ast_log(LOG_ERROR, "Couldn't create message for %s\n", app_name);
- return -1;
- }
- json_args = ast_json_object_get(msg, "args");
- ast_assert(json_args != NULL);
- for (i = 0; i < argc; ++i) {
- ast_json_array_append(json_args, ast_json_string_create(argv[i]));
- }
-
- app->handler(app->data, app_name, msg);
-
+ app->handler(app->data, app_name, message);
return 0;
}
Modified: team/dlee/stasis-http/tests/test_stasis_app.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/tests/test_stasis_app.c?view=diff&rev=380834&r1=380833&r2=380834
==============================================================================
--- team/dlee/stasis-http/tests/test_stasis_app.c (original)
+++ team/dlee/stasis-http/tests/test_stasis_app.c Mon Feb 4 15:12:12 2013
@@ -52,7 +52,7 @@
break;
}
- res = stasis_app_invoke("i-am-not-an-app", NULL, 0, NULL);
+ res = stasis_app_send("i-am-not-an-app", ast_json_null());
ast_test_validate(test, -1 == res);
return AST_TEST_PASS;
@@ -93,6 +93,7 @@
RAII_VAR(struct app_data *, app_data, NULL, app_data_dtor);
RAII_VAR(char *, app_name, "test-handler", stasis_app_unregister);
RAII_VAR(struct ast_json *, expected_message, NULL, ast_json_unref);
+ RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
int res;
@@ -110,12 +111,10 @@
app_data = app_data_create();
stasis_app_register(app_name, test_handler, app_data);
- expected_message = ast_json_pack("[{s: s, s: o, s: []}]",
- "command", "invoke",
- "channel", ast_json_null(),
- "args");
+ message = ast_json_pack("{s: s}", "test", "message");
+ expected_message = ast_json_pack("[o]", ast_json_ref(message));
- res = stasis_app_invoke(app_name, NULL, 0, NULL);
+ res = stasis_app_send(app_name, message);
ast_test_validate(test, 0 == res);
ast_test_validate(test, 1 == app_data->invocations);
ast_test_validate(test, ast_json_equal(expected_message, app_data->messages));
@@ -129,6 +128,7 @@
RAII_VAR(struct app_data *, app_data2, NULL, app_data_dtor);
RAII_VAR(char *, app_name, "test-handler", stasis_app_unregister);
RAII_VAR(struct ast_json *, expected_message1, NULL, ast_json_unref);
+ RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, expected_message2, NULL, ast_json_unref);
int res;
@@ -149,13 +149,11 @@
stasis_app_register(app_name, test_handler, app_data1);
stasis_app_register(app_name, test_handler, app_data2);
- expected_message1 = ast_json_pack("[{s: s}]", "event", "replaced");
- expected_message2 = ast_json_pack("[{s: s, s: o, s: []}]",
- "command", "invoke",
- "channel", ast_json_null(),
- "args");
+ expected_message1 = ast_json_pack("[{s: s}]", "event", "application-replaced");
+ message = ast_json_pack("{s, s}", "test", "message");
+ expected_message2 = ast_json_pack("[o]", ast_json_ref(message));
- res = stasis_app_invoke(app_name, NULL, 0, NULL);
+ res = stasis_app_send(app_name, message);
ast_test_validate(test, 0 == res);
ast_test_validate(test, 1 == app_data1->invocations);
ast_test_validate(test, ast_json_equal(expected_message1, app_data1->messages));
More information about the asterisk-commits
mailing list