[asterisk-commits] kmoore: branch kmoore/stasis-bridge_events r385045 - in /team/kmoore/stasis-b...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Apr 8 16:43:19 CDT 2013


Author: kmoore
Date: Mon Apr  8 16:43:14 2013
New Revision: 385045

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385045
Log:
Commit work thus far for bridge events for app_stasis

Interest in events for a given bridge by a stasis application is
conveyed via interest in channels that are currently in that bridge.
I.e. if channel A is watched by stasis application Z and channel A
enters bridge B, stasis application Z will get bridge entrance and exit
events for channel A in bridge B as well as all intervening events that
occur on bridge B.  Current bridge events are on par with events
produced by AMI with the addition of a bridge snapshot event in case
further processing is required.

Testing has not yet commenced, but it compiles.

Modified:
    team/kmoore/stasis-bridge_events/apps/app_stasis.c
    team/kmoore/stasis-bridge_events/apps/stasis_json.c
    team/kmoore/stasis-bridge_events/include/asterisk/app_stasis.h

Modified: team/kmoore/stasis-bridge_events/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridge_events/apps/app_stasis.c?view=diff&rev=385045&r1=385044&r2=385045
==============================================================================
--- team/kmoore/stasis-bridge_events/apps/app_stasis.c (original)
+++ team/kmoore/stasis-bridge_events/apps/app_stasis.c Mon Apr  8 16:43:14 2013
@@ -40,6 +40,7 @@
 #include "asterisk/stasis_message_router.h"
 #include "asterisk/strings.h"
 #include "asterisk/callerid.h"
+#include "asterisk/stasis_bridging.h"
 
 /*** DOCUMENTATION
 	<application name="Stasis" language="en_US">
@@ -66,6 +67,9 @@
 /*! \brief Number of buckets for the channels container for app instances */
 #define APP_CHANNELS_BUCKETS 7
 
+/*! \brief Number of buckets for the global bridges container */
+#define APP_BRIDGES_BUCKETS 57
+
 /*! \brief Dialplan application name */
 static const char *stasis = "Stasis";
 
@@ -90,6 +94,10 @@
 struct ao2_container *__app_controls;
 
 struct stasis_message_router *app_channel_router;
+
+struct stasis_message_router *app_bridge_router;
+
+struct ao2_container *app_bridges;
 
 /*! Ref-counting accessor for the stasis applications container */
 static struct ao2_container *apps_registry(void)
@@ -178,14 +186,107 @@
 	}
 }
 
+struct bridge_info {
+	/*! List of channel identifiers that stasis apps are interested in for this bridge */
+	struct ao2_container *channels;
+	/*! uniqueid of the bridge */
+	char uniqueid[];
+};
+
+/*! AO2 destructor for \ref bridge_info */
+static void bridge_info_dtor(void *obj)
+{
+	struct bridge_info *info = obj;
+
+	ao2_cleanup(info->channels);
+	info->channels = NULL;
+}
+
+/*! AO2 hash function for \ref bridge_info */
+static int bridge_info_hash(const void *obj, const int flags)
+{
+	const struct bridge_info *info = obj;
+	const char *uniqueid = flags & OBJ_KEY ? obj : info->uniqueid;
+
+	return ast_str_hash(uniqueid);
+}
+
+/*! AO2 comparison function for \ref bridge_info */
+static int bridge_info_cmp(void *lhs, void *rhs, int flags)
+{
+	const struct bridge_info *lhs_info = lhs;
+	const struct bridge_info *rhs_info = rhs;
+	const char *rhs_uniqueid = flags & OBJ_KEY ? rhs : rhs_info->uniqueid;
+
+	if (strcmp(lhs_info->uniqueid, rhs_uniqueid) == 0) {
+		return CMP_MATCH;
+	} else {
+		return 0;
+	}
+}
+
+static int check_bridge_snapshot_cb(void *obj, void *arg, int flags)
+{
+	struct ast_bridge_snapshot *snapshot = stasis_message_data(obj);
+	char *chan_uniqueid = arg;
+	RAII_VAR(struct bridge_info *, info, NULL, ao2_cleanup);
+	RAII_VAR(char *, channel_in_snapshot, NULL, ao2_cleanup);
+	RAII_VAR(char *, channel_in_info, NULL, ao2_cleanup);
+
+	channel_in_snapshot = ao2_find(snapshot->channels, chan_uniqueid, OBJ_KEY);
+	if (!channel_in_snapshot) {
+		return 0;
+	}
+
+	info = ao2_find(app_bridges, snapshot->uniqueid, OBJ_KEY);
+	if (!info) {
+		/* bridge not yet in list of bridges of interest */
+		info = ao2_alloc(sizeof(*info) + strlen(snapshot->uniqueid) + 1, bridge_info_dtor);
+		if (!info) {
+			return CMP_STOP;
+		}
+		ao2_link(app_bridges, info);
+	}
+
+	channel_in_info = ao2_find(info->channels, chan_uniqueid, OBJ_KEY);
+	if (channel_in_info) {
+		return CMP_STOP;
+	}
+
+	/* since there is already an ao2 copy of the channel uniqueid
+	 * available, just link that in */
+	ao2_link(info->channels, channel_in_snapshot);
+
+	/* Assumes a given channel will only be in one bridge */
+	return CMP_STOP;
+}
+
+/*!
+ * Handles adding and updating bridge_info structs upon interest
+ * in a channel from app_stasis */
+static void add_bridge_for_chan(char *uniqueid)
+{
+	RAII_VAR(struct ao2_container *, bridge_states, NULL, ao2_cleanup);
+
+	bridge_states = stasis_cache_dump(ast_bridge_topic_all_cached(),
+		ast_bridge_snapshot_type());
+
+	if (!bridge_states) {
+		return;
+	}
+
+	ao2_callback(bridge_states, OBJ_NODATA, check_bridge_snapshot_cb, uniqueid);
+}
+
 static int app_add_channel(struct app* app, const struct ast_channel *chan)
 {
 	RAII_VAR(char *, ao2_channel_id, NULL, ao2_cleanup);
-	const char *uniqueid;
+	RAII_VAR(char *, ao2_bridge_id, NULL, ao2_cleanup);
+	char *uniqueid;
 	ast_assert(chan != NULL);
 	ast_assert(app != NULL);
 
-	uniqueid = ast_channel_uniqueid(chan);
+	uniqueid = ast_strdupa(ast_channel_uniqueid(chan));
 
 	ao2_channel_id = ao2_alloc(strlen(uniqueid) + 1, NULL);
 	if (!ao2_channel_id) {
@@ -195,15 +296,45 @@
 	/* safe strcpy */
 	strcpy(ao2_channel_id, uniqueid);
 	ao2_link(app->channels, ao2_channel_id);
+
+	add_bridge_for_chan(uniqueid);
 	return 0;
 }
 
+static int check_bridge_info_removal_cb(void *obj, void *arg, int flags)
+{
+	struct bridge_info *info = obj;
+	char *chan_uniqueid = arg;
+	RAII_VAR(char *, chan_in_info, NULL, ao2_cleanup);
+
+	chan_in_info = ao2_find(info->channels, chan_uniqueid, OBJ_KEY | OBJ_UNLINK);
+	if (!chan_in_info) {
+		return 0;
+	}
+
+	if (ao2_container_count(info->channels)) {
+		return CMP_STOP;
+	}
+	/* no channels left in this bridge_info to be interested in,
+	 * so have it removed from the container */
+	return CMP_MATCH | CMP_STOP;
+}
+
+static void remove_bridge_for_chan(char *uniqueid)
+{
+	ao2_callback(app_bridges, OBJ_NODATA | OBJ_UNLINK, check_bridge_info_removal_cb, uniqueid);
+}
+
 static void app_remove_channel(struct app* app, const struct ast_channel *chan)
 {
+	char *uniqueid;
 	ast_assert(chan != NULL);
 	ast_assert(app != NULL);
 
-	ao2_find(app->channels, ast_channel_uniqueid(chan), OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
+	uniqueid = ast_strdupa(ast_channel_uniqueid(chan));
+	ao2_find(app->channels, uniqueid, OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
+
+	remove_bridge_for_chan(uniqueid);
 }
 
 /*!
@@ -278,7 +409,7 @@
 	control->continue_to_dialplan = 1;
 }
 
-static struct ast_json *app_event_create(
+static struct ast_json *app_channel_event_create(
 	const char *event_name,
 	const struct ast_channel_snapshot *snapshot,
 	const struct ast_json *extra_info)
@@ -367,7 +498,7 @@
 	if (snapshot == NULL) {
 		return -1;
 	}
-	msg = app_event_create("stasis-end", snapshot, NULL);
+	msg = app_channel_event_create("stasis-end", snapshot, NULL);
 	if (!msg) {
 		return -1;
 	}
@@ -380,21 +511,24 @@
 {
 	RAII_VAR(char *, uniqueid, NULL, ao2_cleanup);
 	struct app *app = obj;
-	struct ast_channel_snapshot *snapshot = arg;
-
-	uniqueid = ao2_find(app->channels, snapshot->uniqueid, OBJ_KEY);
+	char *chan_uniqueid = arg;
+
+	uniqueid = ao2_find(app->channels, chan_uniqueid, OBJ_KEY);
 	return uniqueid ? CMP_MATCH : 0;
 }
 
-static struct ao2_container *get_watching_apps(struct ast_channel_snapshot *snapshot)
+static struct ao2_container *get_watching_apps(const char *uniqueid)
 {
 	RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
 	struct ao2_container *watching_apps;
+	char *uniqueid_dup;
 	RAII_VAR(struct ao2_iterator *,watching_apps_iter, NULL, ao2_iterator_destroy);
-	ast_assert(snapshot != NULL);
+	ast_assert(uniqueid != NULL);
 	ast_assert(apps != NULL);
 
-	watching_apps_iter = ao2_callback(apps, OBJ_MULTIPLE, app_watching_channel_cb, snapshot);
+	uniqueid_dup = ast_strdupa(uniqueid);
+
+	watching_apps_iter = ao2_callback(apps, OBJ_MULTIPLE, app_watching_channel_cb, uniqueid_dup);
 	watching_apps = watching_apps_iter->c;
 
 	if (!ao2_container_count(watching_apps)) {
@@ -406,7 +540,7 @@
 }
 
 /*! \brief Typedef for callbacks that get called on channel snapshot updates */
-typedef struct ast_json *(*snapshot_monitor)(
+typedef struct ast_json *(*channel_snapshot_monitor)(
 	struct ast_channel_snapshot *old_snapshot,
 	struct ast_channel_snapshot *new_snapshot);
 
@@ -436,7 +570,7 @@
 		return NULL;
 	}
 
-	return app_event_create(event_name, new_snapshot ? new_snapshot : old_snapshot, json);
+	return app_channel_event_create(event_name, new_snapshot ? new_snapshot : old_snapshot, json);
 }
 
 /*!
@@ -494,7 +628,7 @@
 		return NULL;
 	}
 
-	return app_event_create("channel-event-dialplan", new_snapshot, json);
+	return app_channel_event_create("channel-event-dialplan", new_snapshot, json);
 }
 
 /*!
@@ -536,7 +670,7 @@
 		return NULL;
 	}
 
-	return app_event_create("channel-event-callerid", new_snapshot, json);
+	return app_channel_event_create("channel-event-callerid", new_snapshot, json);
 }
 
 static struct ast_json *channel_snapshot(
@@ -547,10 +681,10 @@
 		return NULL;
 	}
 
-	return app_event_create("channel-snapshot", new_snapshot, NULL);
-}
-
-snapshot_monitor monitors[] = {
+	return app_channel_event_create("channel-snapshot", new_snapshot, NULL);
+}
+
+channel_snapshot_monitor channel_monitors[] = {
 	channel_snapshot,
 	channel_state,
 	channel_dialplan,
@@ -577,15 +711,15 @@
 	struct ast_channel_snapshot *old_snapshot = stasis_message_data(update->old_snapshot);
 	int i;
 
-	watching_apps = get_watching_apps(new_snapshot ? new_snapshot : old_snapshot);
+	watching_apps = get_watching_apps(new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid);
 	if (!watching_apps) {
 		return;
 	}
 
-	for (i = 0; i < ARRAY_LEN(monitors); ++i) {
+	for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) {
 		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 
-		msg = monitors[i](old_snapshot, new_snapshot);
+		msg = channel_monitors[i](old_snapshot, new_snapshot);
 		if (msg) {
 			ao2_callback(watching_apps, OBJ_NODATA, app_send_cb, msg);
 		}
@@ -606,19 +740,328 @@
 		return;
 	}
 
-	watching_apps = get_watching_apps(obj->snapshot);
+	watching_apps = get_watching_apps(obj->snapshot->uniqueid);
 	if (!watching_apps) {
 		return;
 	}
 
 	ast_str_set(&event_name, 0, "channel-event-%s", ast_channel_blob_json_type(obj));
 
-	msg = app_event_create(ast_str_buffer(event_name), obj->snapshot, obj->blob);
+	msg = app_channel_event_create(ast_str_buffer(event_name), obj->snapshot, obj->blob);
 	if (!msg) {
 		return;
 	}
 
 	ao2_callback(watching_apps, OBJ_NODATA, app_send_cb, msg);
+}
+
+static int find_diff(void *uniqueid, void *arg, int flags)
+{
+	struct ao2_container *secondary = arg;
+	RAII_VAR(char *, ao2_uniqueid, NULL, ao2_cleanup);
+
+	ao2_uniqueid = ao2_find(secondary, uniqueid, OBJ_KEY);
+	if (!ao2_uniqueid) {
+		return CMP_MATCH | CMP_STOP;
+	}
+
+	return 0;
+}
+static struct bridge_info *bridge_info_create_or_update(char *bridge_uniqueid, char *entering_chan)
+{
+	RAII_VAR(struct ao2_container *, entering_apps, NULL, ao2_cleanup);
+	struct bridge_info *info = ao2_find(app_bridges, bridge_uniqueid, OBJ_KEY);
+	RAII_VAR(char *, channel_in_info, NULL, ao2_cleanup);
+
+	if (!entering_chan) {
+		return info;
+	}
+
+	entering_apps = get_watching_apps(entering_chan);
+	if (!entering_apps || ao2_container_count(entering_apps)) {
+		return info;
+	}
+
+	if (!info) {
+		/* bridge not yet in list of bridges of interest */
+		info = ao2_alloc(sizeof(*info) + strlen(bridge_uniqueid) + 1, bridge_info_dtor);
+		if (!info) {
+			return NULL;
+		}
+		ao2_link(app_bridges, info);
+	}
+
+	channel_in_info = ao2_find(info->channels, entering_chan, OBJ_KEY);
+	if (!channel_in_info) {
+		/* since there is already an ao2 copy of the channel uniqueid
+		 * available, just link that in */
+		ao2_link(info->channels, entering_chan);
+	}
+
+	return info;
+}
+
+static int chan_list_intersect_cb(void *obj, void *arg, int flags)
+{
+	struct ao2_container *bridge_channels = arg;
+	RAII_VAR(char *, bridge_chan, ao2_find(bridge_channels, obj, OBJ_KEY), ao2_cleanup);
+
+	if (bridge_chan) {
+		return CMP_MATCH;
+	}
+	return 0;
+}
+
+static int check_app_intersect_cb(void *obj, void *arg, int flags)
+{
+	struct app *app = obj;
+	struct ao2_container *bridge_channels = arg;
+	RAII_VAR(char *, match, NULL, ao2_cleanup);
+
+	match = ao2_callback(app->channels, 0, chan_list_intersect_cb, bridge_channels);
+	/* if intersection, match app */
+	if (match) {
+		return CMP_MATCH;
+	}
+
+	return 0;
+}
+
+/* find all apps where the intersection of the app channels of interest
+ * set and the bridge stasis channels set results in a non-empty set */
+static struct ao2_container *get_bridge_watching_apps(struct bridge_info *info)
+{
+	RAII_VAR(struct ao2_container *, out, NULL, ao2_cleanup);
+	RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
+	RAII_VAR(struct ao2_iterator *,callback_iter, NULL, ao2_iterator_destroy);
+
+	callback_iter = ao2_callback(apps, OBJ_MULTIPLE, check_app_intersect_cb, info->channels);
+	out = callback_iter->c;
+
+	if (!ao2_container_count(out)) {
+		return NULL;
+	}
+
+	ao2_ref(out, +1);
+	return out;
+}
+
+static struct ast_json *app_bridge_event_create(
+	const char *event_name,
+	const struct ast_bridge_snapshot *snapshot,
+	const struct ast_json *extra_info)
+{
+	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
+
+	if (extra_info) {
+		event = ast_json_deep_copy(extra_info);
+	} else {
+		event = ast_json_object_create();
+	}
+
+	if (snapshot) {
+		int ret;
+
+		/* Mustn't already have a bridge field */
+		ast_assert(ast_json_object_get(event, "bridge") == NULL);
+
+		ret = ast_json_object_set(
+			event,
+			"bridge", ast_bridge_snapshot_to_json(snapshot));
+		if (ret != 0) {
+			return NULL;
+		}
+	}
+
+	message = ast_json_pack("{s: o}", event_name, ast_json_ref(event));
+
+	return ast_json_ref(message);
+}
+
+/*! \brief Typedef for callbacks that get called on channel snapshot updates */
+typedef struct ast_json *(*bridge_snapshot_monitor)(
+	struct ast_bridge_snapshot *old_snapshot,
+	struct ast_bridge_snapshot *new_snapshot);
+
+static struct ast_json *bridge_snapshot(
+	struct ast_bridge_snapshot *old_snapshot,
+	struct ast_bridge_snapshot *new_snapshot)
+{
+	if (!new_snapshot) {
+		return NULL;
+	}
+
+	return app_bridge_event_create("bridge-snapshot", new_snapshot, NULL);
+}
+
+/*! \brief Handle bridge creation */
+static struct ast_json *bridge_create(
+	struct ast_bridge_snapshot *old_snapshot,
+	struct ast_bridge_snapshot *new_snapshot)
+{
+	if (!new_snapshot || old_snapshot) {
+		return NULL;
+	}
+
+	return app_bridge_event_create("bridge-event-create", new_snapshot, NULL);
+}
+
+/*! \brief Handle bridge destruction */
+static struct ast_json *bridge_destroy(
+	struct ast_bridge_snapshot *old_snapshot,
+	struct ast_bridge_snapshot *new_snapshot)
+{
+	if (new_snapshot || !old_snapshot) {
+		return NULL;
+	}
+
+	return app_bridge_event_create("bridge-event-destroy", old_snapshot, NULL);
+}
+
+/*! \brief Handle channels entering and leaving */
+static struct ast_json *bridge_channel_diff(
+	struct ast_bridge_snapshot *old_snapshot,
+	struct ast_bridge_snapshot *new_snapshot)
+{
+	RAII_VAR(char *, new_diff, NULL, ao2_cleanup);
+	RAII_VAR(char *, old_diff, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_json *, uniqueid_json, NULL, ast_json_unref);
+	char *event;
+
+	if (!new_snapshot || !old_snapshot
+		|| ao2_container_count(new_snapshot->channels)
+			== ao2_container_count(old_snapshot->channels)) {
+		return NULL;
+	}
+
+	/* get the diff channel for new and old snapshot */
+	new_diff = ao2_callback(new_snapshot->channels, OBJ_NODATA, find_diff, old_snapshot->channels);
+	old_diff = ao2_callback(old_snapshot->channels, OBJ_NODATA, find_diff, new_snapshot->channels);
+	if (new_diff) {
+		event = "bridge-event-enter";
+		uniqueid_json = ast_json_pack("s: s", "uniqueid", new_diff);
+	} else if (old_diff) {
+		event = "bridge-event-leave";
+		uniqueid_json = ast_json_pack("s: s", "uniqueid", old_diff);
+	} else {
+		return NULL;
+	}
+
+	return app_bridge_event_create(event, old_snapshot, uniqueid_json);
+}
+
+/*! \brief Handle bridge video source change */
+static struct ast_json *bridge_video_source(
+	struct ast_bridge_snapshot *old_snapshot,
+	struct ast_bridge_snapshot *new_snapshot)
+{
+	RAII_VAR(struct ast_json *, uniqueid_json, NULL, ast_json_unref);
+
+	if (!new_snapshot || !old_snapshot) {
+		return NULL;
+	}
+
+	if (!strcmp(new_snapshot->video_source, old_snapshot->video_source)) {
+		return NULL;
+	}
+
+	uniqueid_json = ast_json_pack("s: s", "uniqueid", new_snapshot->video_source);
+	if (!uniqueid_json) {
+		return NULL;
+	}
+
+	return app_bridge_event_create("bridge-event-video-source-change", new_snapshot, uniqueid_json);
+}
+
+static const char *video_mode_to_str(enum ast_bridge_video_mode_type video_mode)
+{
+	switch (video_mode) {
+	case AST_BRIDGE_VIDEO_MODE_NONE:
+		return "none";
+	case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
+		return "single";
+	case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
+		return "talker";
+	}
+	return NULL;
+}
+
+/*! \brief Handle bridge video mode change */
+static struct ast_json *bridge_video_mode(
+	struct ast_bridge_snapshot *old_snapshot,
+	struct ast_bridge_snapshot *new_snapshot)
+{
+	RAII_VAR(struct ast_json *, mode_json, NULL, ast_json_unref);
+
+	if (!new_snapshot || !old_snapshot) {
+		return NULL;
+	}
+
+	if (new_snapshot->video_mode == old_snapshot->video_mode) {
+		return NULL;
+	}
+
+	mode_json = ast_json_pack("s: s", "bridge-video-mode", video_mode_to_str(new_snapshot->video_mode));
+	if (!mode_json) {
+		return NULL;
+	}
+
+	return app_bridge_event_create("bridge-event-video-mode-change", new_snapshot, mode_json);
+}
+
+
+bridge_snapshot_monitor bridge_monitors[] = {
+	bridge_snapshot,
+	bridge_create,
+	bridge_destroy,
+	bridge_channel_diff,
+	bridge_video_mode,
+	bridge_video_source,
+};
+
+static void sub_bridge_handler(void *data,
+		struct stasis_subscription *sub,
+		struct stasis_topic *topic,
+		struct stasis_message *message)
+{
+	RAII_VAR(struct ao2_container *, watching_apps, NULL, ao2_cleanup);
+	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);
+	RAII_VAR(char *, entering_chan, NULL, ao2_cleanup);
+	RAII_VAR(char *, leaving_chan, NULL, ao2_cleanup);
+	RAII_VAR(struct bridge_info *, info, NULL, ao2_cleanup);
+	char *bridge_uniqueid = ast_strdupa(new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid);
+	int i;
+
+	/* update bridge_info for added channels if there is interest */ 
+	entering_chan = ao2_callback(new_snapshot->channels, 0, find_diff, old_snapshot->channels);
+	info = bridge_info_create_or_update(bridge_uniqueid, entering_chan);
+	if (!info) {
+		/* no interest in this bridge */
+		return;
+	}
+
+	/* get list of apps that are interested in this bridge event */
+	watching_apps = get_bridge_watching_apps(info);
+	if (!watching_apps) {
+		goto bridge_handler_cleanup;
+	}
+
+	for (i = 0; i < ARRAY_LEN(bridge_monitors); ++i) {
+		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+
+		msg = bridge_monitors[i](old_snapshot, new_snapshot);
+		if (msg) {
+			ao2_callback(watching_apps, OBJ_NODATA, app_send_cb, msg);
+		}
+	}
+
+bridge_handler_cleanup:
+	/* update bridge_info for removed channels if there is interest */ 
+	leaving_chan = ao2_callback(old_snapshot->channels, 0, find_diff, new_snapshot->channels);
+	ao2_find(info->channels, leaving_chan, OBJ_UNLINK | OBJ_NODATA);
 }
 
 /*!
@@ -756,7 +1199,7 @@
 		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 		SCOPED_LOCK(app_lock, app, ao2_lock, ao2_unlock);
 
-		msg = app_event_create("application-replaced", NULL, NULL);
+		msg = app_channel_event_create("application-replaced", NULL, NULL);
 		app->handler(app->data, app_name, msg);
 
 		app->handler = handler;
@@ -807,6 +1250,19 @@
 	r |= stasis_message_router_add(app_channel_router, stasis_cache_update_type(), sub_snapshot_handler, NULL);
 	r |= stasis_message_router_add(app_channel_router, ast_channel_blob_type(), sub_blob_handler, NULL);
 
+
+	app_bridges = ao2_container_alloc(APP_BRIDGES_BUCKETS, bridge_info_hash, bridge_info_cmp);
+	if (!app_bridges) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	app_bridge_router = stasis_message_router_create(stasis_caching_get_topic(ast_bridge_topic_all_cached()));
+	if (!app_bridge_router) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	r |= stasis_message_router_add(app_channel_router, stasis_cache_update_type(), sub_bridge_handler, NULL);
+
 	r |= ast_register_application_xml(stasis, app_stasis_exec);
 	return r;
 }
@@ -818,11 +1274,17 @@
 	stasis_message_router_unsubscribe(app_channel_router);
 	app_channel_router = NULL;
 
+	stasis_message_router_unsubscribe(app_bridge_router);
+	app_bridge_router = NULL;
+
 	ao2_cleanup(__apps_registry);
 	__apps_registry = NULL;
 
 	ao2_cleanup(__app_controls);
 	__app_controls = NULL;
+
+	ao2_cleanup(app_bridges);
+	app_bridges = NULL;
 
 	r |= ast_unregister_application(stasis);
 	return r;

Modified: team/kmoore/stasis-bridge_events/apps/stasis_json.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridge_events/apps/stasis_json.c?view=diff&rev=385045&r1=385044&r2=385045
==============================================================================
--- team/kmoore/stasis-bridge_events/apps/stasis_json.c (original)
+++ team/kmoore/stasis-bridge_events/apps/stasis_json.c Mon Apr  8 16:43:14 2013
@@ -75,3 +75,23 @@
 	return ast_json_ref(json_chan);
 }
 
+struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot)
+{
+	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, "bridge-uniqueid", ast_json_string_create(snapshot->uniqueid));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "bridge-technology", ast_json_string_create(snapshot->technology));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+
+	return ast_json_ref(json_chan);
+}
+

Modified: team/kmoore/stasis-bridge_events/include/asterisk/app_stasis.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridge_events/include/asterisk/app_stasis.h?view=diff&rev=385045&r1=385044&r2=385045
==============================================================================
--- team/kmoore/stasis-bridge_events/include/asterisk/app_stasis.h (original)
+++ team/kmoore/stasis-bridge_events/include/asterisk/app_stasis.h Mon Apr  8 16:43:14 2013
@@ -48,6 +48,7 @@
 
 #include "asterisk/channel.h"
 #include "asterisk/json.h"
+#include "asterisk/stasis_bridging.h"
 
 /*! @{ */
 
@@ -133,6 +134,14 @@
  */
 struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot);
 
+/*!
+ * \brief Build a JSON object from a \ref ast_bridge_snapshot.
+ * \return JSON object representing bridge snapshot.
+ * \return \c NULL on error
+ */
+struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot);
+
+
 /*! @} */
 
 #endif /* _ASTERISK_APP_STASIS_H */




More information about the asterisk-commits mailing list