[asterisk-commits] kmoore: branch kmoore/stasis-channel_events r383745 - in /team/kmoore/stasis-...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Mar 25 13:21:04 CDT 2013


Author: kmoore
Date: Mon Mar 25 13:21:00 2013
New Revision: 383745

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=383745
Log:
Initial bit of work to make custom userevent fields consumable in JSON form

Modified:
    team/kmoore/stasis-channel_events/apps/app_userevent.c
    team/kmoore/stasis-channel_events/main/manager_channels.c

Modified: team/kmoore/stasis-channel_events/apps/app_userevent.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-channel_events/apps/app_userevent.c?view=diff&rev=383745&r1=383744&r2=383745
==============================================================================
--- team/kmoore/stasis-channel_events/apps/app_userevent.c (original)
+++ team/kmoore/stasis-channel_events/apps/app_userevent.c Mon Mar 25 13:21:00 2013
@@ -69,7 +69,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);
 
@@ -78,26 +77,37 @@
 		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];
+		RAII_VAR(struct ast_json *, json_value, NULL, ast_json_unref);
+
+		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;
+		}
+
+		if (ast_json_object_set(blob, key, json_value)) {
+			return -1;
+		}
 	}
 
 	msg = ast_channel_blob_create(chan, blob);

Modified: team/kmoore/stasis-channel_events/main/manager_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-channel_events/main/manager_channels.c?view=diff&rev=383745&r1=383744&r2=383745
==============================================================================
--- team/kmoore/stasis-channel_events/main/manager_channels.c (original)
+++ team/kmoore/stasis-channel_events/main/manager_channels.c Mon Mar 25 13:21:00 2013
@@ -471,11 +471,53 @@
 	}
 }
 
+/*!
+ * \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 int default_exclusion_cb(const char *key)
+{
+	if (!strcmp("type", key)) {
+		return 1;
+	}
+	return 0;
+}
+
+#define manager_str_from_blob(blob) \
+	manager_str_from_blob_exclusion(blob, default_exclusion_cb)
+
+static struct ast_str *manager_str_from_blob_exclusion(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 void channel_varset(struct ast_channel_blob *obj)
 {
 	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
-	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"));
+	RAII_VAR(struct ast_str *, blob_str, manager_str_from_blob(obj->blob), ast_free);
 
 	if (obj->snapshot) {
 		channel_event_string = manager_build_channel_state_string(obj->snapshot);
@@ -506,23 +548,33 @@
 	***/
 	manager_event(EVENT_FLAG_DIALPLAN, "VarSet",
 		      "%s"
-		      "Variable: %s\r\n"
-		      "Value: %s\r\n",
+		      "%s",
 		      ast_str_buffer(channel_event_string),
-		      variable, value);
+		      ast_str_buffer(blob_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, manager_str_from_blob_exclusion(obj->blob, userevent_exclusion_cb), 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"));
 	channel_event_string = manager_build_channel_state_string(obj->snapshot);
 
-	if (!channel_event_string) {
+	if (!channel_event_string || !body) {
 		return;
 	}
 
@@ -544,7 +596,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)




More information about the asterisk-commits mailing list