[asterisk-commits] dlee: branch dlee/stasis-http r384319 - in /team/dlee/stasis-http: ./ apps/ i...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Mar 28 22:34:24 CDT 2013


Author: dlee
Date: Thu Mar 28 22:34:20 2013
New Revision: 384319

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=384319
Log:
Restructured messages to better fit a Swagger model.

This will allow us to document the events using Swagger without too
much hackery.
........

Merged revisions 384318 from http://svn.asterisk.org/svn/asterisk/team/dlee/stasis-app

Modified:
    team/dlee/stasis-http/   (props changed)
    team/dlee/stasis-http/apps/app_stasis.c
    team/dlee/stasis-http/include/asterisk/app_stasis.h
    team/dlee/stasis-http/tests/test_app_stasis.c

Propchange: team/dlee/stasis-http/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Thu Mar 28 22:34:20 2013
@@ -1,1 +1,1 @@
-/team/dlee/stasis-app:1-384293 /trunk:1-384219
+/team/dlee/stasis-app:1-384318 /trunk:1-384219

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=384319&r1=384318&r2=384319
==============================================================================
--- team/dlee/stasis-http/apps/app_stasis.c (original)
+++ team/dlee/stasis-http/apps/app_stasis.c Thu Mar 28 22:34:20 2013
@@ -360,51 +360,42 @@
 	return *retval;
 }
 
-struct ast_json *stasis_app_event_create(
+static struct ast_json *app_event_create(
 	const char *event_name,
-	const struct ast_channel_snapshot *channel_info,
+	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) {
-		r = ast_json_object_set(
-			message,
-			"channel_info",
-			ast_channel_snapshot_to_json(channel_info));
-		if (r != 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);
 
@@ -413,35 +404,33 @@
 
 	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);
+	/* Set channel info */
+	snapshot = ast_channel_snapshot_create(chan);
+	if (!snapshot) {
 		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(msg_info, "args");
+	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 arg to start message\n");
+			ast_log(LOG_ERROR, "Error appending 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);
-	if (!msg) {
-		return -1;
 	}
 
 	app_send(app, msg);
@@ -460,7 +449,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;
 	}
@@ -470,14 +459,16 @@
 }
 
 static void sub_handler(void *data, struct stasis_subscription *sub,
-			struct stasis_topic *topic, struct stasis_message *message)
+			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;
 		}
@@ -683,7 +674,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-http/include/asterisk/app_stasis.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/app_stasis.h?view=diff&rev=384319&r1=384318&r2=384319
==============================================================================
--- team/dlee/stasis-http/include/asterisk/app_stasis.h (original)
+++ team/dlee/stasis-http/include/asterisk/app_stasis.h Thu Mar 28 22:34:20 2013
@@ -147,16 +147,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-http/tests/test_app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/tests/test_app_stasis.c?view=diff&rev=384319&r1=384318&r2=384319
==============================================================================
--- team/dlee/stasis-http/tests/test_app_stasis.c (original)
+++ team/dlee/stasis-http/tests/test_app_stasis.c Thu Mar 28 22:34:20 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