[asterisk-commits] dlee: branch dlee/stasis-app r384318 - in /team/dlee/stasis-app: apps/ includ...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Thu Mar 28 22:04:48 CDT 2013
Author: dlee
Date: Thu Mar 28 22:04:44 2013
New Revision: 384318
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=384318
Log:
Restructured messages to better fit a Swagger model.
This will allow us to document the events using Swagger without too
much hackery.
Modified:
team/dlee/stasis-app/apps/app_stasis.c
team/dlee/stasis-app/include/asterisk/app_stasis.h
team/dlee/stasis-app/tests/test_app_stasis.c
Modified: team/dlee/stasis-app/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/apps/app_stasis.c?view=diff&rev=384318&r1=384317&r2=384318
==============================================================================
--- team/dlee/stasis-app/apps/app_stasis.c (original)
+++ team/dlee/stasis-app/apps/app_stasis.c Thu Mar 28 22:04:44 2013
@@ -187,7 +187,8 @@
return control;
}
-struct stasis_app_control *stasis_app_control_find_by_channel(const struct ast_channel *chan)
+struct stasis_app_control *stasis_app_control_find_by_channel(
+ const struct ast_channel *chan)
{
RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
if (chan == NULL) {
@@ -223,43 +224,42 @@
control->continue_to_dialplan = 1;
}
-struct ast_json *stasis_app_event_create(const char *event_name,
- const struct ast_channel_snapshot *channel_info,
- const struct ast_json *extra_info) {
+static struct ast_json *app_event_create(
+ const char *event_name,
+ const struct ast_channel_snapshot *snapshot,
+ const struct ast_json *extra_info)
+{
RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
- int r;
-
- ast_assert(event_name != NULL);
+ RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
if (extra_info) {
- message = ast_json_deep_copy(extra_info);
+ event = ast_json_deep_copy(extra_info);
} else {
- message = ast_json_object_create();
- }
- if (!message) {
- ast_log(LOG_ERROR, "Allocation failed\n");
- return NULL;
- }
-
- r = ast_json_object_set(message, "event", ast_json_string_create(event_name));
- if (r != 0) {
- ast_log(LOG_ERROR, "Failed to add event_name to message\n");
- return NULL;
- }
-
- if (channel_info) {
- if (ast_json_object_set(message, "channel_info", ast_channel_snapshot_to_json(channel_info)) != 0) {
- ast_log(LOG_ERROR, "Failed to add channel info to event\n");
+ event = ast_json_object_create();
+ }
+
+ if (snapshot) {
+ int ret;
+
+ /* Mustn't already have a channel field */
+ ast_assert(ast_json_object_get(event, "channel") == NULL);
+
+ ret = ast_json_object_set(
+ event,
+ "channel", ast_channel_snapshot_to_json(snapshot));
+ if (ret != 0) {
return NULL;
}
}
+ message = ast_json_pack("{s: o}", event_name, event);
+
return ast_json_ref(message);
}
-static int send_start_msg(struct app *app, struct ast_channel *chan, int argc, char *argv[])
-{
- RAII_VAR(struct ast_json *, msg_info, NULL, ast_json_unref);
+static int send_start_msg(struct app *app, struct ast_channel *chan,
+ int argc, char *argv[])
+{
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
@@ -267,32 +267,34 @@
int i;
ast_assert(chan != NULL);
-
- msg_info = ast_json_pack("{s: []}", "args");
- if (!msg_info) {
- 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_info, "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])) != 0) {
- ast_log(LOG_ERROR, "Error appending arg to start message\n");
- return -1;
- }
- }
/* Set channel info */
snapshot = ast_channel_snapshot_create(chan);
if (!snapshot) {
- ast_log(LOG_ERROR, "Allocation failed\n");
- return -1;
- }
- msg = stasis_app_event_create("stasis-start", snapshot, msg_info);
+ return -1;
+ }
+
+ msg = ast_json_pack("{s: {s: [], s: o}}",
+ "stasis-start",
+ "args",
+ "channel", ast_channel_snapshot_to_json(snapshot));
+
if (!msg) {
return -1;
+ }
+
+ /* Append arguments to args array */
+ json_args = ast_json_object_get(
+ ast_json_object_get(msg, "stasis-start"),
+ "args");
+ ast_assert(json_args != NULL);
+ for (i = 0; i < argc; ++i) {
+ int r = ast_json_array_append(json_args,
+ ast_json_string_create(argv[i]));
+ if (r != 0) {
+ ast_log(LOG_ERROR, "Error appending start message\n");
+ return -1;
+ }
}
app_send(app, msg);
@@ -311,7 +313,7 @@
if (snapshot == NULL) {
return -1;
}
- msg = stasis_app_event_create("stasis-end", snapshot, NULL);
+ msg = app_event_create("stasis-end", snapshot, NULL);
if (!msg) {
return -1;
}
@@ -320,14 +322,17 @@
return 0;
}
-static void sub_handler(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message)
+static void sub_handler(void *data, struct stasis_subscription *sub,
+ struct stasis_topic *topic,
+ struct stasis_message *message)
{
struct app *app = data;
if (ast_channel_snapshot_type() == stasis_message_type(message)) {
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
- struct ast_channel_snapshot *snapshot = stasis_message_data(message);
-
- msg = stasis_app_event_create("channel-state-change", snapshot, NULL);
+ struct ast_channel_snapshot *snapshot =
+ stasis_message_data(message);
+
+ msg = app_event_create("channel-state-change", snapshot, NULL);
if (!msg) {
return;
}
@@ -475,7 +480,7 @@
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
SCOPED_LOCK(app_lock, app, ao2_lock, ao2_unlock);
- msg = stasis_app_event_create("application-replaced", NULL, NULL);
+ msg = app_event_create("application-replaced", NULL, NULL);
app->handler(app->data, app_name, msg);
app->handler = handler;
Modified: team/dlee/stasis-app/include/asterisk/app_stasis.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/include/asterisk/app_stasis.h?view=diff&rev=384318&r1=384317&r2=384318
==============================================================================
--- team/dlee/stasis-app/include/asterisk/app_stasis.h (original)
+++ team/dlee/stasis-app/include/asterisk/app_stasis.h Thu Mar 28 22:04:44 2013
@@ -129,16 +129,6 @@
*/
struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot);
-/*!
- * \brief Create an event message for a Stasis application.
- * \param event_name Name of the event. Cannot be \c NULL.
- * \param channel_info Optional channel information to include in the event.
- * \param extra_info Optional extra information to include in the event.
- * \return New event message.
- * \return \c NULL on error.
- */
-struct ast_json *stasis_app_event_create(const char *event_name, const struct ast_channel_snapshot *channel_info, const struct ast_json *extra_info);
-
/*! @} */
#endif /* _ASTERISK_APP_STASIS_H */
Modified: team/dlee/stasis-app/tests/test_app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/tests/test_app_stasis.c?view=diff&rev=384318&r1=384317&r2=384318
==============================================================================
--- team/dlee/stasis-app/tests/test_app_stasis.c (original)
+++ team/dlee/stasis-app/tests/test_app_stasis.c Thu Mar 28 22:04:44 2013
@@ -114,7 +114,7 @@
app_data = app_data_create();
stasis_app_register(app_name, test_handler, app_data);
- message = stasis_app_event_create("test-message", NULL, NULL);
+ message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
expected_message = ast_json_pack("[o]", ast_json_ref(message));
res = stasis_app_send(app_name, message);
@@ -154,7 +154,7 @@
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", "application-replaced");
- message = stasis_app_event_create("test-message", NULL, NULL);
+ message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
expected_message2 = ast_json_pack("[o]", ast_json_ref(message));
res = stasis_app_send(app_name, message);
More information about the asterisk-commits
mailing list