[asterisk-commits] kmoore: branch kmoore/stasis-bridge_events r385972 - /team/kmoore/stasis-brid...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Apr 17 11:19:57 CDT 2013


Author: kmoore
Date: Wed Apr 17 11:19:56 2013
New Revision: 385972

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385972
Log:
Rework app/bridge interest model

This reduces processing on addition of channels to bridges, new interest
in channels already in bridges, and message passing and shifts that
processing to bridge/channel removal and interest removal.

Modified:
    team/kmoore/stasis-bridge_events/res/res_stasis.c

Modified: team/kmoore/stasis-bridge_events/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridge_events/res/res_stasis.c?view=diff&rev=385972&r1=385971&r2=385972
==============================================================================
--- team/kmoore/stasis-bridge_events/res/res_stasis.c (original)
+++ team/kmoore/stasis-bridge_events/res/res_stasis.c Wed Apr 17 11:19:56 2013
@@ -179,10 +179,11 @@
 }
 
 struct bridge_info {
-	/*! List of channel identifiers that stasis apps are interested in for this bridge */
-	struct ao2_container *channels;
+	/*! List of struct app interested in this bridge
+	 * Note: These are necessarily shared with the apps registry container */
+	struct ao2_container *apps;
 	/*! uniqueid of the bridge */
-	char uniqueid[];
+	char *uniqueid;
 };
 
 /*! AO2 destructor for \ref bridge_info */
@@ -190,8 +191,34 @@
 {
 	struct bridge_info *info = obj;
 
-	ao2_cleanup(info->channels);
-	info->channels = NULL;
+	ao2_cleanup(info->apps);
+	info->apps = NULL;
+
+	ast_free(info->uniqueid);
+	info->uniqueid = NULL;
+}
+
+static struct bridge_info *bridge_info_alloc(const char *uniqueid)
+{
+	RAII_VAR(struct bridge_info *, info, NULL, ao2_cleanup);
+	/* bridge not yet in list of bridges of interest */
+	info = ao2_alloc(sizeof(*info), bridge_info_dtor);
+	if (!info) {
+		return NULL;
+	}
+	
+	info->apps = ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
+	if (!info->apps) {
+		return NULL;
+	}
+
+	info->uniqueid = ast_strdup(uniqueid);
+	if (!info->uniqueid) {
+		return NULL;
+	}
+
+	ao2_ref(info, +1);
+	return info;
 }
 
 /*! AO2 hash function for \ref bridge_info */
@@ -217,13 +244,27 @@
 	}
 }
 
-static int check_bridge_snapshot_cb(void *obj, void *arg, int flags)
+static int add_app_to_bridge_info_cb(void *obj, void *arg, int flags)
+{
+	struct app *new_app = obj;
+	struct bridge_info *info = arg;
+	RAII_VAR(struct app *, app_in_bridge, NULL, ao2_cleanup);
+
+	app_in_bridge = ao2_find(info->apps, new_app, OBJ_POINTER);
+	if (!app_in_bridge) {
+		ao2_link(info->apps, new_app);
+	}
+
+	return 0;
+}
+
+static int check_bridge_snapshot_cb(void *obj, void *arg, void *data, int flags)
 {
 	struct ast_bridge_snapshot *snapshot = stasis_message_data(obj);
 	char *chan_uniqueid = arg;
+	struct app *new_app = data;
 	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) {
@@ -233,21 +274,14 @@
 	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);
+		info = bridge_info_alloc(snapshot->uniqueid);
 		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);
+	add_app_to_bridge_info_cb(new_app, info, flags);
 
 	/* Assumes a given channel will only be in one bridge */
 	return CMP_STOP;
@@ -256,7 +290,7 @@
 /*!
  * Handles adding and updating bridge_info structs upon interest
  * in a channel from app_stasis */
-static void add_bridge_for_chan(char *uniqueid)
+static void add_bridge_for_chan(struct app *new_app, char *uniqueid)
 {
 	RAII_VAR(struct ao2_container *, bridge_states, NULL, ao2_cleanup);
 
@@ -267,7 +301,7 @@
 		return;
 	}
 
-	ao2_callback(bridge_states, OBJ_NODATA, check_bridge_snapshot_cb, uniqueid);
+	ao2_callback_data(bridge_states, OBJ_NODATA, check_bridge_snapshot_cb, uniqueid, new_app);
 }
 
 static int app_add_channel(struct app* app, const struct ast_channel *chan)
@@ -281,35 +315,69 @@
 		return -1;
 	}
 
-	add_bridge_for_chan(uniqueid);
+	add_bridge_for_chan(app, uniqueid);
 	return 0;
 }
 
+static int intersect_app_bridge_cb(void *obj, void *arg, int flags)
+{
+	char *chan = obj;
+	struct ao2_container *bridge_chans = arg;
+	RAII_VAR(char *, chan_in_bridge, NULL, ao2_cleanup);
+
+	chan_in_bridge = ao2_find(bridge_chans, chan, OBJ_KEY);
+	if (chan_in_bridge) {
+		return CMP_MATCH;
+	}
+	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) {
+	struct app *app = arg;
+	RAII_VAR(struct app *, app_in_info, NULL, ao2_cleanup);
+	RAII_VAR(char *, common_chan, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct ast_bridge_snapshot *snapshot;
+
+	app_in_info = ao2_find(info->apps, app, OBJ_POINTER);
+	if (!app_in_info) {
 		return 0;
 	}
 
-	if (ao2_container_count(info->channels)) {
-		return CMP_STOP;
-	}
-	/* no channels left in this bridge_info to be interested in,
+	msg = stasis_cache_get(ast_bridge_topic_all_cached(), ast_bridge_snapshot_type(), info->uniqueid);
+	if (!msg) {
+		/* no bridge snapshot found? unlink the bridge_info since the bridge has gone away! */
+		return CMP_MATCH;
+	}
+	snapshot = stasis_message_data(msg);
+	
+	/* check to see whether the app is still interested in any of the bridge's channels */
+	common_chan = ao2_callback(app->channels, 0, intersect_app_bridge_cb, snapshot->channels);
+	if (common_chan) {
+		return 0;
+	}
+
+	/* no more interest from this app in this bridge, unlink the app from the bridge_info */
+	ao2_find(info->apps, app, OBJ_POINTER | OBJ_NODATA | OBJ_UNLINK);
+
+	if (ao2_container_count(info->apps)) {
+		/* there is still interest in this bridge from *SOME* app */
+		return 0;
+	}
+
+	/* no apps 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)
+	return CMP_MATCH;
+}
+
+static void remove_bridge_for_app(struct app *app)
+{
+	ao2_callback(app_bridges, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK, check_bridge_info_removal_cb, app);
+}
+
+static void app_remove_channel(struct app *app, const struct ast_channel *chan)
 {
 	char *uniqueid;
 	ast_assert(chan != NULL);
@@ -318,7 +386,7 @@
 	uniqueid = ast_strdupa(ast_channel_uniqueid(chan));
 	ao2_find(app->channels, uniqueid, OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
 
-	remove_bridge_for_chan(uniqueid);
+	remove_bridge_for_app(app);
 }
 
 /*!
@@ -819,64 +887,31 @@
 {
 	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)) {
+	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);
+		info = bridge_info_alloc(bridge_uniqueid);
 		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);
-	}
+	/* add apps interested in the new channel that may not yet be interested in the bridge */
+	ao2_callback(entering_apps, OBJ_NODATA, add_app_to_bridge_info_cb, info);
 
 	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 ast_bridge_snapshot *bridge)
 {
 	RAII_VAR(struct ao2_container *, out, NULL, ao2_cleanup);
@@ -891,15 +926,8 @@
 		return NULL;
 	}
 
-	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;
+	ao2_ref(info->apps, +1);
+	return info->apps;
 }
 
 static struct ast_json *app_bridge_event_create(
@@ -1257,13 +1285,28 @@
 	return 0;
 }
 
+static int app_unregister_cb(void *obj, void *arg, int flags)
+{
+	struct bridge_info *info = obj;
+	char *app_name = arg;
+
+	ao2_find(info->apps, app_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
+
+	if (!ao2_container_count(info->apps)) {
+		return CMP_MATCH;
+	}
+	return 0;
+}
+
 void stasis_app_unregister(const char *app_name)
 {
-	RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
-
 	if (app_name) {
+		RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
+		char *name = ast_strdupa(app_name);
+
 		apps = apps_registry();
-		ao2_cleanup(ao2_find(apps, app_name, OBJ_KEY | OBJ_UNLINK));
+		ao2_find(apps, app_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
+		ao2_callback(app_bridges, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, app_unregister_cb, name);
 	}
 }
 
@@ -1304,8 +1347,37 @@
 	return app_bridge_event_create("bridge-event-enter", obj->bridge, obj->channel, NULL);
 }
 
+static int bridge_leave_cb(void *obj, void *arg, int flags)
+{
+	struct app *app = obj;
+	struct ao2_container *bridge_channels = arg;
+	RAII_VAR(char *, common_chan, NULL, ao2_cleanup);
+
+	/* check to see whether the app is still interested in any of the bridge's channels */
+	common_chan = ao2_callback(app->channels, 0, intersect_app_bridge_cb, bridge_channels);
+	if (common_chan) {
+		return 0;
+	}
+
+	/* no interest by this app in this bridge */
+	return CMP_MATCH;
+}
+
 static struct ast_json *handle_blob_leave(struct ast_bridge_blob *obj)
 {
+	RAII_VAR(struct bridge_info *, info, NULL, ao2_cleanup);
+	info = ao2_find(app_bridges, obj->bridge->uniqueid, OBJ_KEY);
+	if (!info) {
+		/* no interest */
+		return NULL;
+	}
+
+	ao2_callback(info->apps, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, bridge_leave_cb, obj->bridge->channels);
+	if (!ao2_container_count(info->apps)) {
+		/* no more apps have interest in this bridge, remove it from the list */
+		ao2_find(app_bridges, info, OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
+	}
+
 	return app_bridge_event_create("bridge-event-leave", obj->bridge, obj->channel, NULL);
 }
 




More information about the asterisk-commits mailing list