[svn-commits] dlee: branch dlee/stasis-app r381763 - in /team/dlee/stasis-app: apps/ includ...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Feb 19 12:37:54 CST 2013


Author: dlee
Date: Tue Feb 19 12:37:50 2013
New Revision: 381763

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=381763
Log:
Back to JSON messages; compiles and passed unit test

Modified:
    team/dlee/stasis-app/apps/app_stasis.c
    team/dlee/stasis-app/apps/stasis_json.c
    team/dlee/stasis-app/include/asterisk/app_stasis.h
    team/dlee/stasis-app/include/asterisk/json.h
    team/dlee/stasis-app/res/res_json.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=381763&r1=381762&r2=381763
==============================================================================
--- team/dlee/stasis-app/apps/app_stasis.c (original)
+++ team/dlee/stasis-app/apps/app_stasis.c Tue Feb 19 12:37:50 2013
@@ -57,13 +57,6 @@
 	</application>
  ***/
 
-static struct stasis_message_type *__ast_app_event;
-
-struct stasis_message_type *ast_app_event(void) {
-	ast_assert(__ast_app_event != NULL);
-	return __ast_app_event;
-}
-
 /*! Maximum number of arguments for the Stasis dialplan application */
 #define MAX_ARGS 128
 
@@ -102,15 +95,6 @@
 	ast_free(app->name);
 }
 
-static void stasis_app_event_dtor(void *obj) {
-	struct stasis_app_event *app_event = obj;
-
-	ast_free(app_event->event_name);
-	app_event->event_name = NULL;
-	ast_json_unref(app_event->event_details);
-	app_event->event_details = NULL;
-}
-
 /*! Constructor for \ref stasis_app. */
 static struct stasis_app *app_create(const char *name, stasis_app_handler handler, void *data)
 {
@@ -183,51 +167,49 @@
  * \param app App to send the message to.
  * \param message Message to send.
  */
-static void app_send(struct stasis_app *app, struct stasis_message *message)
+static void app_send(struct stasis_app *app, struct ast_json *message)
 {
 	app->handler(app->data, app->name, message);
 }
 
-static struct stasis_message *stasis_app_event_create(const char *event_name, const struct ast_channel_snapshot *channel_info, const struct ast_json *extra_info) {
-	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
-	RAII_VAR(struct stasis_app_event *, app_event, NULL, ao2_cleanup);
-	RAII_VAR(struct ast_json *, event_details, NULL, ast_json_unref);
+struct ast_json *stasis_app_event_create(const char *event_name, const struct ast_channel_snapshot *channel_info, const struct ast_json *extra_info) {
+	RAII_VAR(struct ast_json *, message, NULL, ao2_cleanup);
+	int r;
 
 	if (event_name == NULL) {
 		return NULL;
 	}
 
-	app_event = ao2_alloc(sizeof(*app_event), stasis_app_event_dtor);
-
-	app_event->event_name = ast_strdup(event_name);
-	if (!app_event->event_name) {
+	if (extra_info) {
+		message = ast_json_deep_copy(extra_info);
+	} else {
+		message = ast_json_object_create();
+	}
+	if (!message) {
 		ast_log(LOG_ERROR, "Allocation failed\n");
 		return NULL;
 	}
 
-	if (extra_info) {
-		event_details = ast_json_deep_copy(extra_info);
-	} else {
-		event_details = ast_json_object_create();
-	}
+	r = ast_json_object_set(message, "event_name", ast_json_string_create(event_name));
+	if (r != 0) {
+		ast_log(LOG_ERROR, "Allocation failed\n");
+		return NULL;
+	}
+
 	if (channel_info) {
-		ast_json_object_add(event_details, "channel_info", channel_info);
-	}
-	app_event->event_details = ast_json_ref(event_details);
-
-	message = stasis_message_create(stasis_app_event(), app_event);
-	if (!message) {
-		return NULL;
-	}
-
-	ao2_ref(message, +1);
-	return message;
+		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");
+			return NULL;
+		}
+	}
+
+	return ast_json_ref(message);
 }
 
 static int send_start_msg(struct stasis_app *app, struct ast_channel *chan, int argc, char *argv[])
 {
 	RAII_VAR(struct ast_json *, msg_info, NULL, ast_json_unref);
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
 
 	struct ast_json *json_args;
@@ -236,13 +218,13 @@
 	ast_assert(chan != NULL);
 
 	msg_info = ast_json_pack("{s: []}", "args");
-	if (!msg) {
+	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, "args");
+	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) {
@@ -252,9 +234,8 @@
 	}
 
 	/* Set channel info */
-	snapshot = ast_channel_snapshot_create(chan);
-	if (ast_json_object_set(msg, "channel-info", ast_channel_snapshot_to_json(snapshot)) != 0) {
-		ast_log(LOG_ERROR, "Couldn't attach channel-info info to message\n");
+	msg = stasis_app_event_create("stasis-start", snapshot, msg_info);
+	if (!msg) {
 		return -1;
 	}
 
@@ -269,21 +250,12 @@
 
 	ast_assert(chan != NULL);
 
-	msg = ast_json_pack("{s: s, s: s}",
-			    "event", "app-end",
-			    "app_name", app->name,
-			    "channel_id", ast_channel_uniqueid(chan));
-	if (!msg) {
-		ast_log(LOG_ERROR, "Couldn't create message for %s\n", app->name);
-		return -1;
-	}
-
 	/* Set channel info */
 	snapshot = ast_channel_snapshot_create(chan);
-	if (ast_json_object_set(msg, "channel-info", ast_channel_snapshot_to_json(snapshot)) != 0) {
-		ast_log(LOG_ERROR, "Couldn't attach channel-info info to message\n");
-		return -1;
-	}
+	if (snapshot == NULL) {
+		return -1;
+	}
+	msg = stasis_app_event_create("stasis-end", snapshot, NULL);
 
 	app_send(app, msg);
 	return 0;
@@ -405,7 +377,7 @@
 		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 		SCOPED_LOCK(app_lock, app, ao2_lock, ao2_unlock);
 
-		msg = ast_json_pack("{s: s}", "event", "application-replaced");
+		msg = stasis_app_event_create("application-replaced", NULL, NULL);
 		app->handler(app->data, app_name, msg);
 
 		app->handler = handler;
@@ -413,7 +385,7 @@
 	} else {
 		app = app_create(app_name, handler, data);
 		if (app) {
-			ao2_link(apps, app);
+			ao2_link_flags(apps, app, OBJ_NOLOCK);
 		} else {
 			ast_log(LOG_ERROR, "Failed to allocate stasis_app\n");
 			return -1;
@@ -434,11 +406,6 @@
 {
 	int r = 0;
 
-	__ast_app_event = stasis_message_type_create("ast_app_event");
-	if (__ast_app_event == NULL) {
-		return AST_MODULE_LOAD_FAILURE;
-	}
-
 	__stasis_apps = ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
 	if (__stasis_apps == NULL) {
 		return AST_MODULE_LOAD_FAILURE;
@@ -451,9 +418,6 @@
 static int unload_module(void)
 {
 	int r = 0;
-
-	ao2_cleanup(__ast_app_event);
-	__ast_app_event = NULL;
 
 	ao2_cleanup(__stasis_apps);
 	__stasis_apps = NULL;

Modified: team/dlee/stasis-app/apps/stasis_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/apps/stasis_json.c?view=diff&rev=381763&r1=381762&r2=381763
==============================================================================
--- team/dlee/stasis-app/apps/stasis_json.c (original)
+++ team/dlee/stasis-app/apps/stasis_json.c Tue Feb 19 12:37:50 2013
@@ -29,12 +29,18 @@
 
 #include "asterisk/app_stasis.h"
 
-struct ast_json *ast_channel_snapshot_to_json(struct ast_channel_snapshot *snapshot)
+struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
 {
-	RAII_VAR(struct ast_json *, json_chan, ast_json_object_create(), ast_json_unref);
+	RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
 	int r = 0;
 
+	if (snapshot == NULL) {
+		return NULL;
+	}
+
+	json_chan = ast_json_object_create();
 	if (!json_chan) { ast_log(LOG_ERROR, "Error creating channel json object\n"); return NULL; }
+
 	r = ast_json_object_set(json_chan, "name", ast_json_string_create(snapshot->name));
 	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
 	r = ast_json_object_set(json_chan, "state", ast_json_string_create(ast_state2str(snapshot->state)));

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=381763&r1=381762&r2=381763
==============================================================================
--- team/dlee/stasis-app/include/asterisk/app_stasis.h (original)
+++ team/dlee/stasis-app/include/asterisk/app_stasis.h Tue Feb 19 12:37:50 2013
@@ -54,8 +54,12 @@
 void stasis_app_handler_unref(struct stasis_app_handler *handler);
 
 /*!
- * \brief Send a message to the given Stasis application
- * \param app_name Name of the application to invoke
+ * \brief Send a message to the given Stasis application.
+ *
+ * The message given to the handler is a borrowed copy. If you want to keep a
+ * reference to it, you should use \c ast_ref() to keep it around.
+ *
+ * \param app_name Name of the application to invoke.
  * \param message Message to send (borrowed reference)
  * \return 0 for success.
  * \return -1 for error.
@@ -72,7 +76,7 @@
  * \param app_name Name of the application being dispatched to.
  * \param message Message to handle. (borrowed copy)
  */
-typedef void (*stasis_app_handler)(void *data, const char *app_name, struct stasis_message *message);
+typedef void (*stasis_app_handler)(void *data, const char *app_name, struct ast_json *message);
 
 /*!
  * \brief Register a new Stasis application.
@@ -97,19 +101,16 @@
  * \return JSON object representing channel snapshot.
  * \return \c NULL on error
  */
-struct ast_json *ast_channel_snapshot_to_json(struct ast_channel_snapshot *snapshot);
+struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot);
 
 /*!
- * \brief This wrapper around \ref ast_json to make it an AO2 object.
+ * \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 stasis_app_event {
-	char *event_name;
-	struct ast_json *event_details;
-};
-
-/*!
- * \brief Message type for \ref stasis_app_event.
- */
-struct stasis_message_type *stasis_app_event(void);
+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/include/asterisk/json.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/include/asterisk/json.h?view=diff&rev=381763&r1=381762&r2=381763
==============================================================================
--- team/dlee/stasis-app/include/asterisk/json.h (original)
+++ team/dlee/stasis-app/include/asterisk/json.h Tue Feb 19 12:37:50 2013
@@ -786,7 +786,7 @@
  * \return JSON string with ISO 8601 formatted date/time.
  * \return \c NULL on error.
  */
-struct ast_json *ast_json_timeval(struct timeval *tv, const char *zone);
+struct ast_json *ast_json_timeval(const struct timeval *tv, const char *zone);
 
 /*!
  * \brief Construct a context/exten/priority as JSON.

Modified: team/dlee/stasis-app/res/res_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/res/res_json.c?view=diff&rev=381763&r1=381762&r2=381763
==============================================================================
--- team/dlee/stasis-app/res/res_json.c (original)
+++ team/dlee/stasis-app/res/res_json.c Tue Feb 19 12:37:50 2013
@@ -519,7 +519,7 @@
 			     "priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null());
 }
 
-struct ast_json *ast_json_timeval(struct timeval *tv, const char *zone)
+struct ast_json *ast_json_timeval(const struct timeval *tv, const char *zone)
 {
 	char buf[AST_ISO8601_LEN];
 	struct ast_tm tm = {};




More information about the svn-commits mailing list