[asterisk-commits] dlee: branch dlee/json_main r382788 - in /team/dlee/json_main: include/asteri...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Mar 11 10:47:46 CDT 2013


Author: dlee
Date: Mon Mar 11 10:47:42 2013
New Revision: 382788

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=382788
Log:
Introduce channel_blob message type, moved varset over to it.

Added:
    team/dlee/json_main/main/json.c
      - copied unchanged from r382738, team/dlee/json_main/res/res_json.c
Removed:
    team/dlee/json_main/res/res_json.c
    team/dlee/json_main/res/res_json.exports.in
Modified:
    team/dlee/json_main/include/asterisk/channel.h
    team/dlee/json_main/main/channel.c
    team/dlee/json_main/main/manager.c
    team/dlee/json_main/tests/test_json.c

Modified: team/dlee/json_main/include/asterisk/channel.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/json_main/include/asterisk/channel.h?view=diff&rev=382788&r1=382787&r2=382788
==============================================================================
--- team/dlee/json_main/include/asterisk/channel.h (original)
+++ team/dlee/json_main/include/asterisk/channel.h Mon Mar 11 10:47:42 2013
@@ -151,6 +151,7 @@
 #include "asterisk/ccss.h"
 #include "asterisk/framehook.h"
 #include "asterisk/stasis.h"
+#include "asterisk/json.h"
 
 #define DATASTORE_INHERIT_FOREVER	INT_MAX
 
@@ -4187,24 +4188,51 @@
 
 /*!
  * \since 12
- * \brief Variable set event.
- */
-struct ast_channel_varset {
-	/*! Channel variable was set on (or NULL for global variable) */
+ * \brief Blob of data associated with a channel.
+ *
+ * The \c blob is actually a JSON object of structured data. It has a "type" field
+ * which contains the type string describing this blob.
+ */
+struct ast_channel_blob {
+	/*! Channel blob is associated with (or NULL for global/all channels) */
 	struct ast_channel_snapshot *snapshot;
-	/*! Variable name */
-	char *variable;
-	/*! New value */
-	char *value;
+	/*! JSON blob of data */
+	struct ast_json *blob;
 };
 
 /*!
  * \since 12
- * \brief Message type for \ref ast_channel_varset messages.
- *
- * \retval Message type for \ref ast_channel_varset messages.
- */
-struct stasis_message_type *ast_channel_varset(void);
+ * \brief Message type for \ref ast_channel_blob messages.
+ *
+ * \retval Message type for \ref ast_channel_blob messages.
+ */
+struct stasis_message_type *ast_channel_blob(void);
+
+/*!
+ * \since 12
+ * \brief Extracts the type field from a \ref ast_channel_blob.
+ * Returned \c char* is still owned by \a obj
+ * \param obj Channel blob object.
+ * \return Type field value from the blob.
+ * \return \c NULL on error.
+ */
+const char *ast_channel_blob_type(struct ast_channel_blob *obj);
+
+/*!
+ * \since 12
+ * \brief Creates a \ref ast_channel_blob message.
+ *
+ * The \a blob JSON object requires a \c "type" field describing the blob. It
+ * should also be treated as immutable and not modified after it is put into the
+ * message.
+ *
+ * \param chan Channel blob is associated with, or NULL for global/all channels.
+ * \param blob JSON object representing the data.
+ * \return \ref ast_channel_blob message.
+ * \return \c NULL on error
+ */
+struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
+					       struct ast_json *blob);
 
 /*!
  * \since 12

Modified: team/dlee/json_main/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/json_main/main/channel.c?view=diff&rev=382788&r1=382787&r2=382788
==============================================================================
--- team/dlee/json_main/main/channel.c (original)
+++ team/dlee/json_main/main/channel.c Mon Mar 11 10:47:42 2013
@@ -155,7 +155,7 @@
 /*! \brief Message type for channel snapshot events */
 static struct stasis_message_type *__channel_snapshot;
 
-static struct stasis_message_type *__channel_varset;
+static struct stasis_message_type *__channel_blob;
 
 struct stasis_topic *__channel_topic_all;
 
@@ -243,37 +243,79 @@
 	stasis_publish(ast_channel_topic(chan), message);
 }
 
-static void channel_varset_dtor(void *obj)
-{
-	struct ast_channel_varset *event = obj;
+static void channel_blob_dtor(void *obj)
+{
+	struct ast_channel_blob *event = obj;
 	ao2_cleanup(event->snapshot);
-	ast_free(event->variable);
-	ast_free(event->value);
+	ast_json_unref(event->blob);
+}
+
+struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
+					       struct ast_json *blob)
+{
+	RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct ast_json *type;
+
+	ast_assert(obj != NULL);
+
+	type = ast_json_object_get(blob, "type");
+	if (type == NULL) {
+		ast_log(LOG_ERROR, "Invalid ast_channel_blob; missing type field");
+		return NULL;
+	}
+
+	obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
+	if (!obj) {
+		return NULL;
+	}
+
+	if (chan) {
+		obj->snapshot = ast_channel_snapshot_create(chan);
+		if (obj->snapshot == NULL) {
+			return NULL;
+		}
+	}
+
+	obj->blob = ast_json_ref(blob);
+
+	msg = stasis_message_create(ast_channel_blob(), obj);
+	if (!msg) {
+		return NULL;
+	}
+
+	ao2_ref(msg, +1);
+	return msg;
+}
+
+const char *ast_channel_blob_type(struct ast_channel_blob *obj)
+{
+	if (obj == NULL) {
+		return NULL;
+	}
+
+	return ast_json_string_get(ast_json_object_get(obj->blob, "type"));
 }
 
 void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
 {
-	RAII_VAR(struct ast_channel_varset *, event, NULL, ao2_cleanup);
 	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
-
-	event = ao2_alloc(sizeof(*event), channel_varset_dtor);
-	if (!event) {
+	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+
+	ast_assert(name != NULL);
+	ast_assert(value != NULL);
+
+	blob = ast_json_pack("{s: s, s: s, s: s}",
+			     "type", "varset",
+			     "variable", name,
+			     "value", value);
+	if (!blob) {
+		ast_log(LOG_ERROR, "Error creating message\n");
 		return;
 	}
 
-	if (chan) {
-		event->snapshot = ast_channel_snapshot_create(chan);
-		if (event->snapshot == NULL) {
-			return;
-		}
-	}
-	event->variable = ast_strdup(name);
-	event->value = ast_strdup(value);
-	if (event->variable == NULL || event->value == NULL) {
-		return;
-	}
-
-	msg = stasis_message_create(ast_channel_varset(), event);
+	msg = ast_channel_blob_create(chan, ast_json_ref(blob));
+
 	if (!msg) {
 		return;
 	}
@@ -8633,8 +8675,8 @@
 {
 	ao2_cleanup(__channel_snapshot);
 	__channel_snapshot = NULL;
-	ao2_cleanup(__channel_varset);
-	__channel_varset = NULL;
+	ao2_cleanup(__channel_blob);
+	__channel_blob = NULL;
 	ao2_cleanup(__channel_topic_all);
 	__channel_topic_all = NULL;
 	stasis_caching_unsubscribe(__channel_topic_all_cached);
@@ -8667,7 +8709,7 @@
 	}
 
 	__channel_snapshot = stasis_message_type_create("ast_channel_snapshot");
-	__channel_varset = stasis_message_type_create("ast_channel_varset");
+	__channel_blob = stasis_message_type_create("ast_channel_blob");
 
 	__channel_topic_all = stasis_topic_create("ast_channel_topic_all");
 	__channel_topic_all_cached = stasis_caching_topic_create(__channel_topic_all, channel_snapshot_get_id);
@@ -11306,9 +11348,9 @@
 	return snapshot;
 }
 
-struct stasis_message_type *ast_channel_varset(void)
-{
-	return __channel_varset;
+struct stasis_message_type *ast_channel_blob(void)
+{
+	return __channel_blob;
 }
 
 struct stasis_message_type *ast_channel_snapshot(void)

Modified: team/dlee/json_main/main/manager.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/json_main/main/manager.c?view=diff&rev=382788&r1=382787&r2=382788
==============================================================================
--- team/dlee/json_main/main/manager.c (original)
+++ team/dlee/json_main/main/manager.c Mon Mar 11 10:47:42 2013
@@ -7548,7 +7548,8 @@
 		      channel_name, name, value, uniqueid);
 }
 
-static void channel_event_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message)
+static void channel_event_cb(void *data, struct stasis_subscription *sub,
+			     struct stasis_topic *topic, struct stasis_message *message)
 {
 	if (stasis_message_type(message) == stasis_cache_update()) {
 		struct stasis_cache_update *update = stasis_message_data(message);
@@ -7559,11 +7560,25 @@
 				stasis_message_data(update->new_snapshot);
 			channel_snapshot_update(old_snapshot, new_snapshot);
 		}
-	} else if (stasis_message_type(message) == ast_channel_varset()) {
-		struct ast_channel_varset *varset = stasis_message_data(message);
-		const char *name = varset->snapshot ? varset->snapshot->name : "none";
-		const char *uniqueid = varset->snapshot ? varset->snapshot->uniqueid : "none";
-		channel_varset(name, uniqueid, varset->variable, varset->value);
+	} else if (stasis_message_type(message) == ast_channel_blob()) {
+		struct ast_channel_blob *obj = stasis_message_data(message);
+
+		if (strcmp("varset", ast_channel_blob_type(obj)) == 0) {
+			const char *variable = ast_json_string_get(ast_json_object_get(obj->blob, "variable"));
+			const char *value = ast_json_string_get(ast_json_object_get(obj->blob, "value"));
+			const char *name;
+			const char *uniqueid;
+
+			if (obj->snapshot) {
+				name = obj->snapshot->name;
+				uniqueid = obj->snapshot->uniqueid;
+			} else {
+				name = "none";
+				uniqueid = "none";
+			}
+
+			channel_varset(name, uniqueid, variable, value);
+		}
 	}
 }
 

Modified: team/dlee/json_main/tests/test_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/json_main/tests/test_json.c?view=diff&rev=382788&r1=382787&r2=382788
==============================================================================
--- team/dlee/json_main/tests/test_json.c (original)
+++ team/dlee/json_main/tests/test_json.c Mon Mar 11 10:47:42 2013
@@ -31,7 +31,6 @@
 
 /*** MODULEINFO
 	<depend>TEST_FRAMEWORK</depend>
-	<depend>res_json</depend>
 	<support_level>core</support_level>
  ***/
 
@@ -1720,5 +1719,4 @@
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "JSON testing",
 		.load = load_module,
-		.unload = unload_module,
-		.nonoptreq = "res_json");
+		.unload = unload_module);




More information about the asterisk-commits mailing list