[asterisk-commits] dlee: branch dlee/ari-event-remodel2 r392315 - in /team/dlee/ari-event-remode...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jun 20 09:45:00 CDT 2013


Author: dlee
Date: Thu Jun 20 09:44:58 2013
New Revision: 392315

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=392315
Log:
Parts are back in the car

Modified:
    team/dlee/ari-event-remodel2/main/stasis_channels.c
    team/dlee/ari-event-remodel2/res/res_stasis.c
    team/dlee/ari-event-remodel2/res/stasis_http/ari_model.c
    team/dlee/ari-event-remodel2/res/stasis_http/ari_model.h
    team/dlee/ari-event-remodel2/rest-api/api-docs/events.json

Modified: team/dlee/ari-event-remodel2/main/stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel2/main/stasis_channels.c?view=diff&rev=392315&r1=392314&r2=392315
==============================================================================
--- team/dlee/ari-event-remodel2/main/stasis_channels.c (original)
+++ team/dlee/ari-event-remodel2/main/stasis_channels.c Thu Jun 20 09:44:58 2013
@@ -32,10 +32,11 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include "asterisk/astobj2.h"
+#include "asterisk/json.h"
+#include "asterisk/pbx.h"
 #include "asterisk/stasis.h"
-#include "asterisk/astobj2.h"
 #include "asterisk/stasis_channels.h"
-#include "asterisk/pbx.h"
 
 /*** DOCUMENTATION
 	<managerEvent language="en_US" name="VarSet">
@@ -667,6 +668,92 @@
 		strcmp(old_snapshot->caller_name, new_snapshot->caller_name) == 0;
 }
 
+static struct ast_json *dtmf_end_to_json(struct stasis_message *message)
+{
+	struct ast_channel_blob *channel_blob = stasis_message_data(message);
+	struct ast_json *blob = channel_blob->blob;
+	struct ast_channel_snapshot *snapshot = channel_blob->snapshot;
+	const char *direction =
+		ast_json_string_get(ast_json_object_get(blob, "direction"));
+	const struct timeval *tv = stasis_message_timestamp(message);
+
+	/* Only present received DTMF end events as JSON */
+	if (strcasecmp("Received", direction) != 0) {
+		return NULL;
+	}
+
+	return ast_json_pack("{s: s, s: o, s: o, s: o, s: o}",
+		"type", "ChannelDtmfReceived",
+		"timestamp", ast_json_timeval(*tv, NULL),
+		"digit", ast_json_ref(ast_json_object_get(blob, "digit")),
+		"duration_ms", ast_json_ref(ast_json_object_get(blob, "duration_ms")),
+		"channel", ast_channel_snapshot_to_json(snapshot));
+}
+
+static struct ast_json *user_event_to_json(struct stasis_message *message)
+{
+	struct ast_channel_blob *channel_blob = stasis_message_data(message);
+	struct ast_json *blob = channel_blob->blob;
+	struct ast_channel_snapshot *snapshot = channel_blob->snapshot;
+	const struct timeval *tv = stasis_message_timestamp(message);
+
+	return ast_json_pack("{s: s, s: o, s: o, s: o, s: o}",
+		"type", "ChannelUserevent",
+		"timestamp", ast_json_timeval(*tv, NULL),
+		"eventname", ast_json_ref(ast_json_object_get(blob, "eventname")),
+		"userevent", ast_json_ref(blob),
+		"channel", ast_channel_snapshot_to_json(snapshot));
+}
+
+static struct ast_json *varset_to_json(struct stasis_message *message)
+{
+	RAII_VAR(struct ast_json *, res, NULL, ast_json_unref);
+	struct ast_channel_blob *channel_blob = stasis_message_data(message);
+	struct ast_json *blob = channel_blob->blob;
+	struct ast_channel_snapshot *snapshot = channel_blob->snapshot;
+	const struct timeval *tv = stasis_message_timestamp(message);
+
+	res = ast_json_pack("{s: s, s: o, s: o, s: o, s: o}",
+		"type", "ChannelVarset",
+		"timestamp", ast_json_timeval(*tv, NULL),
+		"variable", ast_json_ref(ast_json_object_get(blob, "eventname")),
+		"value", ast_json_ref(blob));
+
+	if (snapshot) {
+		ast_json_object_set(res, "channel",
+			ast_channel_snapshot_to_json(snapshot));
+	}
+
+	return ast_json_ref(res);
+}
+
+static struct ast_json *hangup_request_to_json(struct stasis_message *message)
+{
+	RAII_VAR(struct ast_json *, res, NULL, ast_json_unref);
+	struct ast_channel_blob *channel_blob = stasis_message_data(message);
+	struct ast_json *blob = channel_blob->blob;
+	struct ast_channel_snapshot *snapshot = channel_blob->snapshot;
+	struct ast_json *field;
+	const struct timeval *tv = stasis_message_timestamp(message);
+
+	res = ast_json_pack("{s: s, s: o, s: o, s: o, s: o}",
+		"type", "ChannelHangupRequest",
+		"timestamp", ast_json_timeval(*tv, NULL),
+		"channel", ast_channel_snapshot_to_json(snapshot));
+
+	field = ast_json_object_get(blob, "cause");
+	if (field) {
+		ast_json_object_set(res, "cause", ast_json_ref(field));
+	}
+
+	field = ast_json_object_get(blob, "soft");
+	if (field) {
+		ast_json_object_set(res, "soft", ast_json_ref(field));
+	}
+
+	return ast_json_ref(res);
+}
+
 /*!
  * @{ \brief Define channel message types.
  */
@@ -674,13 +761,18 @@
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dial_type);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_varset_type,
 	.to_ami = varset_to_ami,
+	.to_json = varset_to_json,
 	);
-STASIS_MESSAGE_TYPE_DEFN(ast_channel_user_event_type);
-STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_request_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_user_event_type,
+	.to_json = user_event_to_json,
+	);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_request_type,
+	.to_json = hangup_request_to_json,
+	);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_begin_type);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_end_type,
 	.to_json = dtmf_end_to_json,
-);
+	);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_hold_type);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_unhold_type);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_chanspy_start_type);

Modified: team/dlee/ari-event-remodel2/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel2/res/res_stasis.c?view=diff&rev=392315&r1=392314&r2=392315
==============================================================================
--- team/dlee/ari-event-remodel2/res/res_stasis.c (original)
+++ team/dlee/ari-event-remodel2/res/res_stasis.c Thu Jun 20 09:44:58 2013
@@ -232,33 +232,33 @@
 typedef struct ast_json *(*channel_snapshot_monitor)(
 	struct ast_channel_snapshot *old_snapshot,
 	struct ast_channel_snapshot *new_snapshot,
-	struct timeval tv);
+	const struct timeval *tv);
 
 static struct ast_json *simple_channel_event(
 	const char *type,
 	struct ast_channel_snapshot *snapshot,
-	struct timeval tv)
+	const struct timeval *tv)
 {
 	return ast_json_pack("{s: s, s: o, s: o}",
 		"type", type,
-		"timestamp", ast_json_timeval(tv, NULL),
+		"timestamp", ast_json_timeval(*tv, NULL),
 		"channel", ast_channel_snapshot_to_json(snapshot));
 }
 
 static struct ast_json *channel_created_event(
 	struct ast_channel_snapshot *snapshot,
-	struct timeval tv)
+	const struct timeval *tv)
 {
 	return simple_channel_event("ChannelCreated", snapshot, tv);
 }
 
 static struct ast_json *channel_destroyed_event(
 	struct ast_channel_snapshot *snapshot,
-	struct timeval tv)
+	const struct timeval *tv)
 {
 	return ast_json_pack("{s: s, s: o, s: i, s: s, s: o}",
 		"type", "ChannelDestroyed",
-		"timestamp", ast_json_timeval(tv, NULL),
+		"timestamp", ast_json_timeval(*tv, NULL),
 		"cause", snapshot->hangupcause,
 		"cause_txt", ast_cause2str(snapshot->hangupcause),
 		"channel", ast_channel_snapshot_to_json(snapshot));
@@ -266,7 +266,7 @@
 
 static struct ast_json *channel_state_change_event(
 	struct ast_channel_snapshot *snapshot,
-	struct timeval tv)
+	const struct timeval *tv)
 {
 	return simple_channel_event("ChannelStateChange", snapshot, tv);
 }
@@ -275,7 +275,7 @@
 static struct ast_json *channel_state(
 	struct ast_channel_snapshot *old_snapshot,
 	struct ast_channel_snapshot *new_snapshot,
-	struct timeval tv)
+	const struct timeval *tv)
 {
 	struct ast_channel_snapshot *snapshot = new_snapshot ? new_snapshot : old_snapshot;
 
@@ -292,7 +292,7 @@
 
 static struct ast_json *channel_dialplan_event(
 	struct ast_channel_snapshot *snapshot,
-	struct timeval tv)
+	const struct timeval *tv)
 {
 	return simple_channel_event("ChannelDialplan", snapshot, tv);
 }
@@ -300,7 +300,7 @@
 static struct ast_json *channel_dialplan(
 	struct ast_channel_snapshot *old_snapshot,
 	struct ast_channel_snapshot *new_snapshot,
-	struct timeval tv)
+	const struct timeval *tv)
 {
 	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
 
@@ -323,7 +323,7 @@
 
 static struct ast_json *channel_callerid_event(
 	struct ast_channel_snapshot *snapshot,
-	struct timeval tv)
+	const struct timeval *tv)
 {
 	return simple_channel_event("ChannelCallerId", snapshot, tv);
 }
@@ -331,7 +331,7 @@
 static struct ast_json *channel_callerid(
 	struct ast_channel_snapshot *old_snapshot,
 	struct ast_channel_snapshot *new_snapshot,
-	struct timeval tv)
+	const struct timeval *tv)
 {
 	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
 
@@ -384,7 +384,7 @@
 	for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) {
 		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 
-		msg = channel_monitors[i](old_snapshot, new_snapshot, *tv);
+		msg = channel_monitors[i](old_snapshot, new_snapshot, tv);
 		if (msg) {
 			ao2_callback(watching_apps, OBJ_NODATA, app_send_cb, msg);
 		}
@@ -394,28 +394,6 @@
 static void distribute_message(struct ao2_container *apps, struct ast_json *msg)
 {
 	ao2_callback(apps, OBJ_NODATA, app_send_cb, msg);
-}
-
-static void generic_blob_handler(struct ast_channel_blob *obj, channel_blob_handler_cb handler_cb)
-{
-	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-	RAII_VAR(struct ao2_container *, watching_apps, NULL, ao2_cleanup);
-
-	if (!obj->snapshot) {
-		return;
-	}
-
-	watching_apps = get_apps_watching_channel(obj->snapshot->uniqueid);
-	if (!watching_apps) {
-		return;
-	}
-
-	msg = handler_cb(obj);
-	if (!msg) {
-		return;
-	}
-
-	distribute_message(watching_apps, msg);
 }
 
 /*!
@@ -683,67 +661,6 @@
 	}
 }
 
-static struct ast_json *handle_blob_dtmf(struct ast_channel_blob *obj)
-{
-	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-	const char *direction;
-
-	/* To simplify events, we'll only generate on receive */
-	direction = ast_json_string_get(
-		ast_json_object_get(obj->blob, "direction"));
-
-	if (strcmp("Received", direction) != 0) {
-		return NULL;
-	}
-
-	msg = ast_json_pack(
-		"{s: s, s: o}",
-		"digit", ast_json_ref(ast_json_object_get(obj->blob, "digit")));
-	return channel_dtmf_received_event(obj->snapshot, );
-}
-
-static struct ast_json *handle_blob_userevent(struct ast_channel_blob *obj)
-{
-	return stasis_json_event_channel_userevent_create(obj->snapshot, obj->blob);
-}
-
-static void sub_userevent_handler(void *data,
-		struct stasis_subscription *sub,
-		struct stasis_topic *topic,
-		struct stasis_message *message)
-{
-	struct ast_channel_blob *obj = stasis_message_data(message);
-	generic_blob_handler(obj, handle_blob_userevent);
-}
-
-static struct ast_json *handle_blob_hangup_request(struct ast_channel_blob *obj)
-{
-	return stasis_json_event_channel_hangup_request_create(obj->snapshot, obj->blob);
-}
-
-static void sub_hangup_request_handler(void *data,
-		struct stasis_subscription *sub,
-		struct stasis_topic *topic,
-		struct stasis_message *message)
-{
-	struct ast_channel_blob *obj = stasis_message_data(message);
-	generic_blob_handler(obj, handle_blob_hangup_request);
-}
-
-static struct ast_json *handle_blob_varset(struct ast_channel_blob *obj)
-{
-	return stasis_json_event_channel_varset_create(obj->snapshot, obj->blob);
-}
-
-static void sub_varset_handler(void *data,
-		struct stasis_subscription *sub,
-		struct stasis_topic *topic,
-		struct stasis_message *message)
-{
-	struct ast_channel_blob *obj = stasis_message_data(message);
-	generic_blob_handler(obj, handle_blob_varset);
-}
-
 void stasis_app_ref(void)
 {
 	ast_module_ref(ast_module_info->self);
@@ -791,6 +708,30 @@
 	return 0;
 }
 
+static struct ast_json *simple_bridge_event(
+	const char *type,
+	struct ast_bridge_snapshot *snapshot,
+	const struct timeval *tv)
+{
+	return ast_json_pack("{s: s, s: o, s: o}",
+		"type", type,
+		"timestamp", ast_json_timeval(*tv, NULL),
+		"bridge", ast_bridge_snapshot_to_json(snapshot));
+}
+
+static struct ast_json *simple_bridge_channel_event(
+	const char *type,
+	struct ast_bridge_snapshot *bridge_snapshot,
+	struct ast_channel_snapshot *channel_snapshot,
+	const struct timeval *tv)
+{
+	return ast_json_pack("{s: s, s: o, s: o}",
+		"type", type,
+		"timestamp", ast_json_timeval(*tv, NULL),
+		"bridge", ast_bridge_snapshot_to_json(bridge_snapshot),
+		"channel", ast_channel_snapshot_to_json(channel_snapshot));
+}
+
 static void sub_bridge_snapshot_handler(void *data,
 		struct stasis_subscription *sub,
 		struct stasis_topic *topic,
@@ -800,6 +741,8 @@
 	struct stasis_cache_update *update = stasis_message_data(message);
 	struct ast_bridge_snapshot *new_snapshot = stasis_message_data(update->new_snapshot);
 	struct ast_bridge_snapshot *old_snapshot = stasis_message_data(update->old_snapshot);
+	const struct timeval *tv = update->new_snapshot ? stasis_message_timestamp(update->new_snapshot) : stasis_message_timestamp(message);
+
 	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 
 	watching_apps = get_apps_watching_bridge(new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid);
@@ -813,11 +756,11 @@
 		/* The bridge has gone away. Create the message, make sure no apps are
 		 * watching this bridge anymore, and destroy the bridge's control
 		 * structure */
-		msg = stasis_json_event_bridge_destroyed_create(old_snapshot);
+		msg = simple_bridge_event("BridgeDestroyed", old_snapshot, tv);
 		ao2_callback(watching_apps, OBJ_NODATA, remove_bridge_cb, bridge_id);
 		stasis_app_bridge_destroy(old_snapshot->uniqueid);
 	} else if (!old_snapshot) {
-		msg = stasis_json_event_bridge_created_create(old_snapshot);
+		msg = simple_bridge_event("BridgeCreated", old_snapshot, tv);
 	}
 
 	if (!msg) {
@@ -868,6 +811,7 @@
 	struct ast_bridge_merge_message *merge = stasis_message_data(message);
 	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+	const struct timeval *tv = stasis_message_timestamp(message);
 
 	watching_apps_to = get_apps_watching_bridge(merge->to->uniqueid);
 	if (watching_apps_to) {
@@ -884,15 +828,15 @@
 		return;
 	}
 
-	/* The secondary bridge has to be packed into JSON by hand because the auto-generated
-	 * JSON event generator can only handle one instance of a given snapshot type in an
-	 * elegant way */
-	blob = ast_json_pack("{s: o}", "bridge_from", ast_bridge_snapshot_to_json(merge->from));
-	if (!blob) {
+	msg = ast_json_pack("{s: s, s: o, s: o, s: o}",
+		"type", "BridgeMerged",
+		"timestamp", ast_json_timeval(*tv, NULL),
+		"bridge", ast_bridge_snapshot_to_json(merge->to),
+		"bridge_from", ast_bridge_snapshot_to_json(merge->from));
+
+	if (!msg) {
 		return;
 	}
-
-	msg = stasis_json_event_bridge_merged_create(merge->to, blob);
 
 	distribute_message(watching_apps_all, msg);
 }
@@ -923,7 +867,8 @@
 		return;
 	}
 
-	msg = stasis_json_event_channel_entered_bridge_create(obj->bridge, obj->channel);
+	msg = simple_bridge_channel_event("ChannelEnteredBridge", obj->bridge,
+		obj->channel, stasis_message_timestamp(message));
 
 	distribute_message(watching_apps_all, msg);
 }
@@ -942,7 +887,8 @@
 		return;
 	}
 
-	msg = stasis_json_event_channel_left_bridge_create(obj->bridge, obj->channel);
+	msg = simple_bridge_channel_event("ChannelLeftBridge", obj->bridge,
+		obj->channel, stasis_message_timestamp(message));
 
 	distribute_message(watching_apps_bridge, msg);
 }
@@ -975,10 +921,6 @@
 	}
 
 	r |= stasis_message_router_add(channel_router, stasis_cache_update_type(), sub_channel_snapshot_handler, NULL);
-	r |= stasis_message_router_add(channel_router, ast_channel_user_event_type(), sub_userevent_handler, NULL);
-	r |= stasis_message_router_add(channel_router, ast_channel_varset_type(), sub_varset_handler, NULL);
-	r |= stasis_message_router_add(channel_router, ast_channel_dtmf_end_type(), sub_dtmf_handler, NULL);
-	r |= stasis_message_router_add(channel_router, ast_channel_hangup_request_type(), sub_hangup_request_handler, NULL);
 	if (r) {
 		return AST_MODULE_LOAD_FAILURE;
 	}

Modified: team/dlee/ari-event-remodel2/res/stasis_http/ari_model.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel2/res/stasis_http/ari_model.c?view=diff&rev=392315&r1=392314&r2=392315
==============================================================================
--- team/dlee/ari-event-remodel2/res/stasis_http/ari_model.c (original)
+++ team/dlee/ari-event-remodel2/res/stasis_http/ari_model.c Thu Jun 20 09:44:58 2013
@@ -1386,14 +1386,12 @@
 {
 	int res = 1;
 	struct ast_json_iter *iter;
-	int has_channel = 0;
 	int has_value = 0;
 	int has_variable = 0;
 
 	for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
 		if (strcmp("channel", ast_json_object_iter_key(iter)) == 0) {
 			int prop_res;
-			has_channel = 1;
 			prop_res = ari_validate_channel(
 				ast_json_object_iter_value(iter));
 			if (!prop_res) {
@@ -1429,10 +1427,6 @@
 		}
 	}
 
-	if (!has_channel) {
-		ast_log(LOG_ERROR, "ARI ChannelVarset missing required field channel\n");
-		res = 0;
-	}
 	if (!has_value) {
 		ast_log(LOG_ERROR, "ARI ChannelVarset missing required field value\n");
 		res = 0;

Modified: team/dlee/ari-event-remodel2/res/stasis_http/ari_model.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel2/res/stasis_http/ari_model.h?view=diff&rev=392315&r1=392314&r2=392315
==============================================================================
--- team/dlee/ari-event-remodel2/res/stasis_http/ari_model.h (original)
+++ team/dlee/ari-event-remodel2/res/stasis_http/ari_model.h Thu Jun 20 09:44:58 2013
@@ -569,7 +569,7 @@
  * - channel: Channel (required)
  * - eventname: string (required)
  * ChannelVarset
- * - channel: Channel (required)
+ * - channel: Channel
  * - value: string (required)
  * - variable: string (required)
  * Event

Modified: team/dlee/ari-event-remodel2/rest-api/api-docs/events.json
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel2/rest-api/api-docs/events.json?view=diff&rev=392315&r1=392314&r2=392315
==============================================================================
--- team/dlee/ari-event-remodel2/rest-api/api-docs/events.json (original)
+++ team/dlee/ari-event-remodel2/rest-api/api-docs/events.json Thu Jun 20 09:44:58 2013
@@ -306,9 +306,9 @@
 					"description": "The new value of the variable."
 				},
 				"channel": {
-					"required": true,
-					"type": "Channel",
-					"description": "The channel on which the variable was set."
+					"required": false,
+					"type": "Channel",
+					"description": "The channel on which the variable was set.\n\nIf missing, the variable is a global variable."
 				}
 			}
 		},




More information about the asterisk-commits mailing list