[asterisk-commits] kmoore: branch kmoore/stasis-bridging-channel_events r387800 - in /team/kmoor...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue May 7 09:44:23 CDT 2013


Author: kmoore
Date: Tue May  7 09:44:20 2013
New Revision: 387800

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=387800
Log:
Correct a description of an event and convert UserEvents to use JSON blobs instead of raw manager text

Modified:
    team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c
    team/kmoore/stasis-bridging-channel_events/main/manager_channels.c
    team/kmoore/stasis-bridging-channel_events/rest-api/api-docs/events.json

Modified: team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c?view=diff&rev=387800&r1=387799&r2=387800
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c (original)
+++ team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c Tue May  7 09:44:20 2013
@@ -70,7 +70,6 @@
 		AST_APP_ARG(eventname);
 		AST_APP_ARG(extra)[100];
 	);
-	RAII_VAR(struct ast_str *, body, ast_str_create(16), ast_free);
 	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
 	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
 
@@ -79,26 +78,38 @@
 		return -1;
 	}
 
-	if (!body) {
-		ast_log(LOG_WARNING, "Unable to allocate buffer\n");
-		return -1;
-	}
-
 	parse = ast_strdupa(data);
 
 	AST_STANDARD_APP_ARGS(args, parse);
 
-	for (x = 0; x < args.argc - 1; x++) {
-		ast_str_append(&body, 0, "%s\r\n", args.extra[x]);
+	blob = ast_json_pack("{s: s, s: s}",
+			     "type", "userevent",
+			     "eventname", args.eventname);
+
+	if (!blob) {
+		return -1;
 	}
 
-	blob = ast_json_pack("{s: s, s: s, s: s}",
-			     "type", "userevent",
-			     "eventname", args.eventname,
-			     "body", ast_str_buffer(body));
-	if (!blob) {
-		ast_log(LOG_WARNING, "Unable to create message buffer\n");
-		return -1;
+	for (x = 0; x < args.argc - 1; x++) {
+		char *key, *value = args.extra[x];
+		struct ast_json *json_value;
+
+		key = strsep(&value, ":");
+		if (!value) {
+			/* no ':' in string? */
+			continue;
+		}
+
+		value = ast_strip(value);
+		json_value = ast_json_string_create(value);
+		if (!json_value) {
+			return -1;
+		}
+
+		/* ref stolen by ast_json_object_set */
+		if (ast_json_object_set(blob, key, json_value)) {
+			return -1;
+		}
 	}
 
 	msg = ast_channel_blob_create(chan, blob);

Modified: team/kmoore/stasis-bridging-channel_events/main/manager_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/main/manager_channels.c?view=diff&rev=387800&r1=387799&r2=387800
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/main/manager_channels.c (original)
+++ team/kmoore/stasis-bridging-channel_events/main/manager_channels.c Tue May  7 09:44:20 2013
@@ -475,17 +475,60 @@
 		      variable, value);
 }
 
+/*!
+ * \brief Callback used to determine whether a key should be skipped when converting a JSON object to a manager blob
+ * \param key Key from JSON blob to be evaluated
+ * \retval non-zero if the key should be excluded
+ * \retval zero if the key should not be excluded
+ */
+typedef int (*key_exclusion_cb)(const char *key);
+
+static struct ast_str *manager_str_from_json_object(struct ast_json *blob, key_exclusion_cb exclusion_cb)
+{
+	struct ast_str *output_str = ast_str_create(32);
+	struct ast_json_iter *blob_iter = ast_json_object_iter(blob);
+	if (!output_str || !blob_iter) {
+		return NULL;
+	}
+
+	do {
+		const char *key = ast_json_object_iter_key(blob_iter);
+		const char *value = ast_json_string_get(ast_json_object_iter_value(blob_iter));
+		if (exclusion_cb && exclusion_cb(key)) {
+			continue;
+		}
+
+		ast_str_append(&output_str, 0, "%s: %s\r\n", key, value);
+		if (!output_str) {
+			return NULL;
+		}
+	} while ((blob_iter = ast_json_object_iter_next(blob, blob_iter)));
+
+	return output_str;
+}
+
+static int userevent_exclusion_cb(const char *key)
+{
+	if (!strcmp("type", key)) {
+		return 1;
+	}
+	if (!strcmp("eventname", key)) {
+		return 1;
+	}
+	return 0;
+}
+
 static void channel_userevent(struct ast_channel_blob *obj)
 {
 	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
+	RAII_VAR(struct ast_str *, body, NULL, ast_free);
 	const char *eventname;
-	const char *body;
 
 	eventname = ast_json_string_get(ast_json_object_get(obj->blob, "eventname"));
-	body = ast_json_string_get(ast_json_object_get(obj->blob, "body"));
+	body = manager_str_from_json_object(obj->blob, userevent_exclusion_cb);
 	channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
 
-	if (!channel_event_string) {
+	if (!channel_event_string || !body) {
 		return;
 	}
 
@@ -507,7 +550,7 @@
 		      "%s"
 		      "UserEvent: %s\r\n"
 		      "%s",
-		      ast_str_buffer(channel_event_string), eventname, body);
+		      ast_str_buffer(channel_event_string), eventname, ast_str_buffer(body));
 }
 
 static void channel_hangup_request(struct ast_channel_blob *obj)

Modified: team/kmoore/stasis-bridging-channel_events/rest-api/api-docs/events.json
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/rest-api/api-docs/events.json?view=diff&rev=387800&r1=387799&r2=387800
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/rest-api/api-docs/events.json (original)
+++ team/kmoore/stasis-bridging-channel_events/rest-api/api-docs/events.json Tue May  7 09:44:20 2013
@@ -241,17 +241,12 @@
 		},
 		"ChannelUserevent": {
 			"id": "ChannelUserevent",
-			"description": "Channel changed Caller ID.",
+			"description": "User-generated event with additional user-defined fields in the object.",
 			"properties": {
 				"eventname": {
 					"required": true,
 					"type": "string",
 					"description": "The name of the user event."
-				},
-				"body": {
-					"required": true,
-					"type": "string",
-					"description": "The additional body parameters for the user event."
 				},
 				"channel": {
 					"required": true,
@@ -262,7 +257,7 @@
 		},
 		"ChannelHangupRequest": {
 			"id": "ChannelHangupRequest",
-			"description": "Channel changed Caller ID.",
+			"description": "A hangup was requested on the channel.",
 			"properties": {
 				"cause": {
 					"type": "integer",




More information about the asterisk-commits mailing list