[asterisk-commits] kmoore: branch kmoore/stasis-bridging_events-rework r390023 - in /team/kmoore...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed May 29 09:59:08 CDT 2013


Author: kmoore
Date: Wed May 29 09:59:05 2013
New Revision: 390023

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=390023
Log:
Refactored work up to this point, it compiles

Modified:
    team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_app.h
    team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_bridging.h
    team/kmoore/stasis-bridging_events-rework/res/res_stasis.c
    team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.c
    team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.exports.in
    team/kmoore/stasis-bridging_events-rework/res/stasis/app.c
    team/kmoore/stasis-bridging_events-rework/res/stasis/app.h
    team/kmoore/stasis-bridging_events-rework/res/stasis/control.c
    team/kmoore/stasis-bridging_events-rework/res/stasis_http/resource_bridges.c
    team/kmoore/stasis-bridging_events-rework/res/stasis_json/resource_events.h
    team/kmoore/stasis-bridging_events-rework/rest-api/api-docs/events.json

Modified: team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_app.h?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_app.h (original)
+++ team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_app.h Wed May 29 09:59:05 2013
@@ -187,6 +187,63 @@
 	enum ast_control_frame_type frame_type);
 
 /*!
+ * \brief Create a bridge of the specified type.
+ *
+ * \param type The type of bridge to be created
+ *
+ * \return New bridge.
+ * \return \c NULL on error.
+ */
+struct ast_bridge *stasis_app_create_bridge(const char *type);
+
+/*!
+ * \brief Returns the bridge with the given id.
+ * \param bridge_id Uniqueid of the bridge.
+ * \return NULL bridge not in Stasis application, or bridge does not exist.
+ * \return Pointer to bridge.
+ */
+struct ast_bridge *stasis_app_bridge_find_by_id(
+	const char *bridge_id);
+
+/*!
+ * \brief Add a channel to the bridge.
+ *
+ * \param control Control whose channel should be added to the bridge
+ * \param bridge Pointer to the bridge
+ *
+ * \retval non-zero on failure
+ * \retval zero on success
+ */
+int stasis_app_control_add_channel_to_bridge(
+	struct stasis_app_control *control, struct ast_bridge *bridge);
+
+/*!
+ * \brief Remove a channel from the bridge.
+ *
+ * \param control Control whose channel should be removed from the bridge
+ * \param bridge Pointer to the bridge
+ *
+ * \retval non-zero on failure
+ * \retval zero on success
+ */
+int stasis_app_control_remove_channel_from_bridge(
+	struct stasis_app_control *control, struct ast_bridge *bridge);
+
+/*!
+ * \brief Destroy the bridge.
+ *
+ * \param bridge_id Uniqueid of bridge to be destroyed
+ *
+ * \retval non-zero on failure
+ * \retval zero on success
+ */
+int stasis_app_destroy_bridge(
+	const char *bridge_id);
+
+void stasis_app_bridge_remove(struct ast_bridge *bridge);
+void stasis_app_bridge_remove_by_id(const char *bridge_id);
+
+/*!
  * \brief Increment the res_stasis reference count.
  *
  * This ensures graceful shutdown happens in the proper order.

Modified: team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_bridging.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_bridging.h?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_bridging.h (original)
+++ team/kmoore/stasis-bridging_events-rework/include/asterisk/stasis_bridging.h Wed May 29 09:59:05 2013
@@ -220,6 +220,18 @@
 struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot);
 
 /*!
+ * \brief Returns the most recent snapshot for the bridge.
+ *
+ * The returned pointer is AO2 managed, so ao2_cleanup() when you're done.
+ *
+ * \param bridge_id Uniqueid of the bridge for which to get the snapshot.
+ * \return Most recent snapshot. ao2_cleanup() when done.
+ * \return \c NULL if channel isn't in cache.
+ */
+struct ast_bridge_snapshot *ast_bridge_snapshot_get_latest(
+	const char *bridge_id);
+
+/*!
  * \brief Dispose of the stasis bridging topics and message types
  */
 void ast_stasis_bridging_shutdown(void);

Modified: team/kmoore/stasis-bridging_events-rework/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/res/res_stasis.c?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/res/res_stasis.c (original)
+++ team/kmoore/stasis-bridging_events-rework/res/res_stasis.c Wed May 29 09:59:05 2013
@@ -61,6 +61,7 @@
 #include "asterisk/module.h"
 #include "asterisk/stasis_app_impl.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_bridging.h"
 #include "asterisk/stasis_message_router.h"
 #include "asterisk/strings.h"
 #include "stasis/app.h"
@@ -89,8 +90,13 @@
 
 struct ao2_container *app_controls;
 
+struct ao2_container *app_bridges;
+
 /*! \brief Message router for the channel caching topic */
 struct stasis_message_router *channel_router;
+
+/*! \brief Message router for the bridge caching topic */
+struct stasis_message_router *bridge_router;
 
 /*! AO2 hash function for \ref app */
 static int app_hash(const void *obj, const int flags)
@@ -159,6 +165,38 @@
 	return ao2_find(app_controls, channel_id, OBJ_KEY);
 }
 
+/*! AO2 hash function for bridges container  */
+static int bridges_hash(const void *obj, const int flags)
+{
+	const struct ast_bridge *bridge = obj;
+	const char *id = flags & OBJ_KEY ?
+		obj : bridge->uniqueid;
+
+	return ast_str_hash(id);
+}
+
+/*! AO2 comparison function for bridges container */
+static int bridges_compare(void *lhs, void *rhs, int flags)
+{
+	const struct ast_bridge *lhs_bridge = lhs;
+	const struct ast_bridge *rhs_bridge = rhs;
+	const char *lhs_id = lhs_bridge->uniqueid;
+	const char *rhs_id = flags & OBJ_KEY ?
+		rhs : rhs_bridge->uniqueid;
+
+	if (strcmp(lhs_id, rhs_id) == 0) {
+		return CMP_MATCH | CMP_STOP;
+	} else {
+		return 0;
+	}
+}
+
+struct ast_bridge *stasis_app_bridge_find_by_id(
+	const char *bridge_id)
+{
+	return ao2_find(app_bridges, bridge_id, OBJ_KEY);
+}
+
 /*! \brief Typedef for blob handler callbacks */
 typedef struct ast_json *(*channel_blob_handler_cb)(struct ast_channel_blob *);
 
@@ -170,7 +208,7 @@
 	return app_is_watching_channel(app, uniqueid) ? CMP_MATCH : 0;
 }
 
-static struct ao2_container *get_watching_apps(const char *uniqueid)
+static struct ao2_container *get_apps_watching_channel(const char *uniqueid)
 {
 	struct ao2_container *watching_apps;
 	char *uniqueid_dup;
@@ -302,7 +340,7 @@
 	return 0;
 }
 
-static void sub_snapshot_handler(void *data,
+static void sub_channel_snapshot_handler(void *data,
 		struct stasis_subscription *sub,
 		struct stasis_topic *topic,
 		struct stasis_message *message)
@@ -313,7 +351,7 @@
 	struct ast_channel_snapshot *old_snapshot = stasis_message_data(update->old_snapshot);
 	int i;
 
-	watching_apps = get_watching_apps(new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid);
+	watching_apps = get_apps_watching_channel(new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid);
 	if (!watching_apps) {
 		return;
 	}
@@ -342,7 +380,7 @@
 		return;
 	}
 
-	watching_apps = get_watching_apps(obj->snapshot->uniqueid);
+	watching_apps = get_apps_watching_channel(obj->snapshot->uniqueid);
 	if (!watching_apps) {
 		return;
 	}
@@ -357,7 +395,7 @@
 
 /*!
  * \brief In addition to running ao2_cleanup(), this function also removes the
- * object from the app_controls() container.
+ * object from the app_controls container.
  */
 static void control_unlink(struct stasis_app_control *control)
 {
@@ -368,6 +406,53 @@
 	ao2_unlink_flags(app_controls, control,
 		OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
 	ao2_cleanup(control);
+}
+
+struct ast_bridge *stasis_app_create_bridge(const char *type)
+{
+	struct ast_bridge *bridge;
+	int flags;
+	if (ast_strlen_zero(type)) {
+		flags = AST_BRIDGE_CAPABILITY_MULTIMIX;
+	} else if (!strcmp(type, "two-party")) {
+		flags = AST_BRIDGE_CAPABILITY_1TO1MIX;
+	} else if (!strcmp(type, "multi-party")) {
+		flags = AST_BRIDGE_CAPABILITY_MULTIMIX;
+	} else if (!strcmp(type, "holding")) {
+		flags = AST_BRIDGE_CAPABILITY_HOLDING;
+	} else {
+		return NULL;
+	}
+
+	bridge = ast_bridge_base_new(flags, 0);
+	if (bridge) {
+		ao2_link(app_bridges, bridge);
+	}
+	return bridge;
+}
+
+void stasis_app_bridge_remove(struct ast_bridge *bridge)
+{
+	ao2_unlink(app_bridges, bridge);
+}
+
+void stasis_app_bridge_remove_by_id(const char *bridge_id)
+{
+	ao2_find(app_bridges, bridge_id, OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
+}
+
+/*!
+ * \brief In addition to running ao2_cleanup(), this function also removes the
+ * object from the app_bridges container.
+ */
+static void bridge_unlink(struct ast_bridge *bridge)
+{
+	if (!bridge) {
+		return;
+	}
+
+	stasis_app_bridge_remove(bridge);
+	ao2_cleanup(bridge);
 }
 
 int app_send_start_msg(struct app *app, struct ast_channel *chan,
@@ -679,6 +764,192 @@
 	ast_module_unref(ast_module_info->self);
 }
 
+static int app_watching_bridge_cb(void *obj, void *arg, int flags)
+{
+	struct app *app = obj;
+	char *uniqueid = arg;
+
+	return app_is_watching_bridge(app, uniqueid) ? CMP_MATCH : 0;
+}
+
+static struct ao2_container *get_apps_watching_bridge(const char *uniqueid)
+{
+	struct ao2_container *watching_apps;
+	char *uniqueid_dup;
+	RAII_VAR(struct ao2_iterator *,watching_apps_iter, NULL, ao2_iterator_destroy);
+	ast_assert(uniqueid != NULL);
+
+	uniqueid_dup = ast_strdupa(uniqueid);
+
+	watching_apps_iter = ao2_callback(apps_registry, OBJ_MULTIPLE, app_watching_bridge_cb, uniqueid_dup);
+	watching_apps = watching_apps_iter->c;
+
+	if (!ao2_container_count(watching_apps)) {
+		return NULL;
+	}
+
+	ao2_ref(watching_apps, +1);
+	return watching_apps_iter->c;
+}
+
+static int remove_bridge_cb(void *obj, void *arg, int flags)
+{
+	app_remove_bridge(obj, arg);
+	return 0;
+}
+
+static void sub_bridge_snapshot_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(struct ast_json *, msg, NULL, ast_json_unref);
+
+	watching_apps = get_apps_watching_bridge(new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid);
+	if (!watching_apps || !ao2_container_count(watching_apps)) {
+		return;
+	}
+
+	if (!new_snapshot) {
+		RAII_VAR(char *, bridge_id, ast_strdup(old_snapshot->uniqueid), ast_free);
+
+		/* 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);
+		ao2_callback(watching_apps, OBJ_NODATA, remove_bridge_cb, bridge_id);
+		bridge_unlink(stasis_app_bridge_find_by_id(old_snapshot->uniqueid));
+	} else if (!old_snapshot) {
+		msg = stasis_json_event_bridge_created_create(old_snapshot);
+	}
+
+	if (!msg) {
+		return;
+	}
+
+	distribute_message(watching_apps, msg);
+}
+
+static int list_merge_cb(void *obj, void *arg, int flags)
+{
+	/* remove any current entries for this app */
+	ao2_find(arg, obj, OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE);
+	/* relink as the only entry */
+	ao2_link(arg, obj);
+	return 0;
+}
+
+static void update_apps_list(struct ao2_container *dst, struct ao2_container *src)
+{
+	ao2_callback(src, OBJ_NODATA, list_merge_cb, dst);
+}
+
+static int app_add_bridge_cb(void *obj, void *arg, int flags)
+{
+	app_add_bridge(obj, arg);
+	return 0;
+}
+
+static void update_bridge_interest(struct ao2_container *apps, const char *bridge_id)
+{
+	RAII_VAR(char *, bridge_id_dup, ast_strdup(bridge_id), ast_free);
+	ao2_callback(apps, OBJ_NODATA, app_add_bridge_cb, bridge_id_dup);
+}
+
+static void sub_bridge_merge_handler(void *data,
+		struct stasis_subscription *sub,
+		struct stasis_topic *topic,
+		struct stasis_message *message)
+{
+	RAII_VAR(struct ao2_container *, watching_apps_to, NULL, ao2_cleanup);
+	RAII_VAR(struct ao2_container *, watching_apps_from, NULL, ao2_cleanup);
+	RAII_VAR(struct ao2_container *, watching_apps_all, ao2_container_alloc(1, NULL, NULL), ao2_cleanup);
+	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);
+
+	watching_apps_to = get_apps_watching_bridge(merge->to->uniqueid);
+	if (watching_apps_to) {
+		update_apps_list(watching_apps_all, watching_apps_to);
+	}
+
+	watching_apps_from = get_apps_watching_bridge(merge->from->uniqueid);
+	if (watching_apps_from) {
+		update_bridge_interest(watching_apps_from, merge->to->uniqueid);
+		update_apps_list(watching_apps_all, watching_apps_from);
+	}
+
+	if (!ao2_container_count(watching_apps_all)) {
+		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) {
+		return;
+	}
+
+	msg = stasis_json_event_bridge_merged_create(merge->to, blob);
+
+	distribute_message(watching_apps_all, msg);
+}
+
+static void sub_bridge_enter_handler(void *data,
+		struct stasis_subscription *sub,
+		struct stasis_topic *topic,
+		struct stasis_message *message)
+{
+	RAII_VAR(struct ao2_container *, watching_apps_channel, NULL, ao2_cleanup);
+	RAII_VAR(struct ao2_container *, watching_apps_bridge, NULL, ao2_cleanup);
+	RAII_VAR(struct ao2_container *, watching_apps_all, ao2_container_alloc(1, NULL, NULL), ao2_cleanup);
+	struct ast_bridge_blob *obj = stasis_message_data(message);
+	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+
+	watching_apps_bridge = get_apps_watching_bridge(obj->bridge->uniqueid);
+	if (watching_apps_bridge) {
+		update_apps_list(watching_apps_all, watching_apps_bridge);
+	}
+
+	watching_apps_channel = get_apps_watching_channel(obj->channel->uniqueid);
+	if (watching_apps_channel) {
+		update_bridge_interest(watching_apps_channel, obj->bridge->uniqueid);
+		update_apps_list(watching_apps_all, watching_apps_channel);
+	}
+
+	if (!ao2_container_count(watching_apps_all)) {
+		return;
+	}
+
+	msg = stasis_json_event_channel_entered_bridge_create(obj->bridge, obj->channel);
+
+	distribute_message(watching_apps_all, msg);
+}
+
+static void sub_bridge_leave_handler(void *data,
+		struct stasis_subscription *sub,
+		struct stasis_topic *topic,
+		struct stasis_message *message)
+{
+	RAII_VAR(struct ao2_container *, watching_apps_bridge, NULL, ao2_cleanup);
+	struct ast_bridge_blob *obj = stasis_message_data(message);
+	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+
+	watching_apps_bridge = get_apps_watching_bridge(obj->bridge->uniqueid);
+	if (!watching_apps_bridge) {
+		return;
+	}
+
+	msg = stasis_json_event_channel_left_bridge_create(obj->bridge, obj->channel);
+
+	distribute_message(watching_apps_bridge, msg);
+}
+
 static int load_module(void)
 {
 	int r = 0;
@@ -695,12 +966,18 @@
 		return AST_MODULE_LOAD_FAILURE;
 	}
 
+	app_bridges = ao2_container_alloc(CONTROLS_NUM_BUCKETS,
+					     bridges_hash, bridges_compare);
+	if (app_bridges == NULL) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
 	channel_router = stasis_message_router_create(stasis_caching_get_topic(ast_channel_topic_all_cached()));
 	if (!channel_router) {
 		return AST_MODULE_LOAD_FAILURE;
 	}
 
-	r |= stasis_message_router_add(channel_router, stasis_cache_update_type(), sub_snapshot_handler, NULL);
+	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_begin_type(), sub_dtmf_handler, NULL);
@@ -709,6 +986,19 @@
 		return AST_MODULE_LOAD_FAILURE;
 	}
 
+	bridge_router = stasis_message_router_create(stasis_caching_get_topic(ast_bridge_topic_all_cached()));
+	if (!bridge_router) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	r |= stasis_message_router_add(bridge_router, stasis_cache_update_type(), sub_bridge_snapshot_handler, NULL);
+	r |= stasis_message_router_add(bridge_router, ast_bridge_merge_message_type(), sub_bridge_merge_handler, NULL);
+	r |= stasis_message_router_add(bridge_router, ast_channel_entered_bridge_type(), sub_bridge_enter_handler, NULL);
+	r |= stasis_message_router_add(bridge_router, ast_channel_left_bridge_type(), sub_bridge_leave_handler, NULL);
+	if (r) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
@@ -719,11 +1009,17 @@
 	stasis_message_router_unsubscribe_and_join(channel_router);
 	channel_router = NULL;
 
+	stasis_message_router_unsubscribe_and_join(bridge_router);
+	bridge_router = NULL;
+
 	ao2_cleanup(apps_registry);
 	apps_registry = NULL;
 
 	ao2_cleanup(app_controls);
 	app_controls = NULL;
+
+	ao2_cleanup(app_bridges);
+	app_bridges = NULL;
 
 	return r;
 }

Modified: team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.c?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.c (original)
+++ team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.c Wed May 29 09:59:05 2013
@@ -259,6 +259,56 @@
 	return ast_json_ref(message);
 }
 
+struct ast_json *stasis_json_event_channel_varset_create(
+	struct ast_channel_snapshot *channel_snapshot,
+	struct ast_json *blob
+	)
+{
+	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
+	struct ast_json *validator;
+	int ret;
+
+	ast_assert(channel_snapshot != NULL);
+	ast_assert(blob != NULL);
+	ast_assert(ast_json_object_get(blob, "channel") == NULL);
+	ast_assert(ast_json_object_get(blob, "type") == NULL);
+
+	validator = ast_json_object_get(blob, "variable");
+	if (validator) {
+		/* do validation? XXX */
+	} else {
+		/* fail message generation if the required parameter doesn't exist */
+		return NULL;
+	}
+
+	validator = ast_json_object_get(blob, "value");
+	if (validator) {
+		/* do validation? XXX */
+	} else {
+		/* fail message generation if the required parameter doesn't exist */
+		return NULL;
+	}
+
+	event = ast_json_deep_copy(blob);
+	if (!event) {
+		return NULL;
+	}
+
+	ret = ast_json_object_set(event,
+		"channel", ast_channel_snapshot_to_json(channel_snapshot));
+	if (ret) {
+		return NULL;
+	}
+
+	message = ast_json_pack("{s: o}", "channel_varset", ast_json_ref(event));
+	if (!message) {
+		return NULL;
+	}
+
+	return ast_json_ref(message);
+}
+
 struct ast_json *stasis_json_event_bridge_destroyed_create(
 	struct ast_bridge_snapshot *bridge_snapshot
 	)
@@ -370,49 +420,41 @@
 	return ast_json_ref(message);
 }
 
-struct ast_json *stasis_json_event_channel_varset_create(
-	struct ast_channel_snapshot *channel_snapshot,
-	struct ast_json *blob
-	)
-{
-	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
-	RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
-	struct ast_json *validator;
-	int ret;
-
-	ast_assert(channel_snapshot != NULL);
-	ast_assert(blob != NULL);
-	ast_assert(ast_json_object_get(blob, "channel") == NULL);
-	ast_assert(ast_json_object_get(blob, "type") == NULL);
-
-	validator = ast_json_object_get(blob, "variable");
-	if (validator) {
-		/* do validation? XXX */
-	} else {
-		/* fail message generation if the required parameter doesn't exist */
-		return NULL;
-	}
-
-	validator = ast_json_object_get(blob, "value");
-	if (validator) {
-		/* do validation? XXX */
-	} else {
-		/* fail message generation if the required parameter doesn't exist */
-		return NULL;
-	}
-
-	event = ast_json_deep_copy(blob);
-	if (!event) {
-		return NULL;
-	}
-
-	ret = ast_json_object_set(event,
-		"channel", ast_channel_snapshot_to_json(channel_snapshot));
-	if (ret) {
-		return NULL;
-	}
-
-	message = ast_json_pack("{s: o}", "channel_varset", ast_json_ref(event));
+struct ast_json *stasis_json_event_bridge_merged_create(
+	struct ast_bridge_snapshot *bridge_snapshot,
+	struct ast_json *blob
+	)
+{
+	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
+	struct ast_json *validator;
+	int ret;
+
+	ast_assert(bridge_snapshot != NULL);
+	ast_assert(blob != NULL);
+	ast_assert(ast_json_object_get(blob, "bridge") == NULL);
+	ast_assert(ast_json_object_get(blob, "type") == NULL);
+
+	validator = ast_json_object_get(blob, "bridge_from");
+	if (validator) {
+		/* do validation? XXX */
+	} else {
+		/* fail message generation if the required parameter doesn't exist */
+		return NULL;
+	}
+
+	event = ast_json_deep_copy(blob);
+	if (!event) {
+		return NULL;
+	}
+
+	ret = ast_json_object_set(event,
+		"bridge", ast_bridge_snapshot_to_json(bridge_snapshot));
+	if (ret) {
+		return NULL;
+	}
+
+	message = ast_json_pack("{s: o}", "bridge_merged", ast_json_ref(event));
 	if (!message) {
 		return NULL;
 	}

Modified: team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.exports.in?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.exports.in (original)
+++ team/kmoore/stasis-bridging_events-rework/res/res_stasis_json_events.exports.in Wed May 29 09:59:05 2013
@@ -6,10 +6,11 @@
 		LINKER_SYMBOL_PREFIXstasis_json_event_channel_snapshot_create;
 		LINKER_SYMBOL_PREFIXstasis_json_event_channel_caller_id_create;
 		LINKER_SYMBOL_PREFIXstasis_json_event_playback_started_create;
+		LINKER_SYMBOL_PREFIXstasis_json_event_channel_varset_create;
 		LINKER_SYMBOL_PREFIXstasis_json_event_bridge_destroyed_create;
 		LINKER_SYMBOL_PREFIXstasis_json_event_application_replaced_create;
 		LINKER_SYMBOL_PREFIXstasis_json_event_channel_destroyed_create;
-		LINKER_SYMBOL_PREFIXstasis_json_event_channel_varset_create;
+		LINKER_SYMBOL_PREFIXstasis_json_event_bridge_merged_create;
 		LINKER_SYMBOL_PREFIXstasis_json_event_channel_left_bridge_create;
 		LINKER_SYMBOL_PREFIXstasis_json_event_channel_created_create;
 		LINKER_SYMBOL_PREFIXstasis_json_event_stasis_start_create;

Modified: team/kmoore/stasis-bridging_events-rework/res/stasis/app.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/res/stasis/app.c?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/res/stasis/app.c (original)
+++ team/kmoore/stasis-bridging_events-rework/res/stasis/app.c Wed May 29 09:59:05 2013
@@ -38,6 +38,12 @@
  */
 #define APP_CHANNELS_BUCKETS 7
 
+/*!
+ * \brief Number of buckets for the bridges container for app instances.  Remember
+ * to keep it a prime number!
+ */
+#define APP_BRIDGES_BUCKETS 7
+
 struct app {
 	/*! Callback function for this application. */
 	stasis_app_cb handler;
@@ -45,6 +51,8 @@
 	void *data;
 	/*! List of channel identifiers this app instance is interested in */
 	struct ao2_container *channels;
+	/*! List of bridge identifiers this app instance owns */
+	struct ao2_container *bridges;
 	/*! Name of the Stasis application */
 	char name[];
 };
@@ -57,6 +65,8 @@
 	app->data = NULL;
 	ao2_cleanup(app->channels);
 	app->channels = NULL;
+	ao2_cleanup(app->bridges);
+	app->bridges = NULL;
 }
 
 struct app *app_create(const char *name, stasis_app_cb handler, void *data)
@@ -84,6 +94,11 @@
 		return NULL;
 	}
 
+	app->bridges = ast_str_container_alloc(APP_BRIDGES_BUCKETS);
+	if (!app->bridges) {
+		return NULL;
+	}
+
 	ao2_ref(app, +1);
 	return app;
 }
@@ -104,6 +119,22 @@
 	ast_assert(app != NULL);
 
 	ao2_find(app->channels, ast_channel_uniqueid(chan), OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
+}
+
+int app_add_bridge(struct app *app, const char *uniqueid)
+{
+	ast_assert(uniqueid != NULL);
+	ast_assert(app != NULL);
+
+	return ast_str_container_add(app->bridges, uniqueid) ? -1 : 0;
+}
+
+void app_remove_bridge(struct app* app, const char *uniqueid)
+{
+	ast_assert(uniqueid != NULL);
+	ast_assert(app != NULL);
+
+	ao2_find(app->bridges, uniqueid, OBJ_KEY | OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE);
 }
 
 /*!
@@ -137,3 +168,10 @@
 	found = ao2_find(app->channels, uniqueid, OBJ_KEY);
 	return found != NULL;
 }
+
+int app_is_watching_bridge(struct app *app, const char *uniqueid)
+{
+	RAII_VAR(char *, found, NULL, ao2_cleanup);
+	found = ao2_find(app->bridges, uniqueid, OBJ_KEY);
+	return found != NULL;
+}

Modified: team/kmoore/stasis-bridging_events-rework/res/stasis/app.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/res/stasis/app.h?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/res/stasis/app.h (original)
+++ team/kmoore/stasis-bridging_events-rework/res/stasis/app.h Wed May 29 09:59:05 2013
@@ -135,4 +135,32 @@
  */
 void app_remove_channel(struct app *app, const struct ast_channel *chan);
 
+/*!
+ * \brief Add a bridge to an application's watch list by uniqueid.
+ *
+ * \param app Application.
+ * \param bridge Bridge to watch.
+ * \return 0 on success.
+ * \return Non-zero on error.
+ */
+int app_add_bridge(struct app *app, const char *uniqueid);
+
+/*!
+ * \brief Remove a bridge from an application's watch list by uniqueid.
+ *
+ * \param app Application.
+ * \param bridge Bridge to remove.
+ */
+void app_remove_bridge(struct app* app, const char *uniqueid);
+
+/*!
+ * \brief Checks if an application is watching a given bridge.
+ *
+ * \param app Application.
+ * \param uniqueid Uniqueid of the bridge to check.
+ * \return True (non-zero) if \a app is watching bridge with given \a uniqueid
+ * \return False (zero) if \a app isn't.
+ */
+int app_is_watching_bridge(struct app *app, const char *uniqueid);
+
 #endif /* _ASTERISK_RES_STASIS_APP_H */

Modified: team/kmoore/stasis-bridging_events-rework/res/stasis/control.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/res/stasis/control.c?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/res/stasis/control.c (original)
+++ team/kmoore/stasis-bridging_events-rework/res/stasis/control.c Wed May 29 09:59:05 2013
@@ -31,6 +31,8 @@
 
 #include "command.h"
 #include "control.h"
+#include "asterisk/bridging.h"
+#include "asterisk/bridging_features.h"
 
 struct stasis_app_control {
 	/*! Queue of commands to dispatch on the channel */
@@ -200,3 +202,39 @@
 	ao2_iterator_destroy(&i);
 	return count;
 }
+
+static void *app_control_join_bridge(struct stasis_app_control *control,
+	struct ast_channel *chan, void *data)
+{
+	struct ast_bridge_features features;
+	struct ast_bridge *bridge = data;
+	ast_bridge_features_init(&features);
+	ast_bridge_join(bridge,	control->channel, NULL, &features, NULL, 0);
+	ast_bridge_features_cleanup(&features);
+
+	return NULL;
+}
+
+int stasis_app_control_add_channel_to_bridge(struct stasis_app_control *control, struct ast_bridge *bridge)
+{
+	int *retval;
+
+	ast_debug(3, "%s: Sending channel add_to_bridge command\n",
+			ast_channel_uniqueid(control->channel));
+
+	retval = stasis_app_send_command(control, app_control_join_bridge, bridge);
+
+	if (retval == NULL || *retval != 0) {
+		ast_log(LOG_WARNING, "%s: Failed to add channel",
+			ast_channel_uniqueid(control->channel));
+		return -1;
+	}
+
+	return 0;
+}
+
+int stasis_app_control_remove_channel_from_bridge(
+	struct stasis_app_control *control, struct ast_bridge *bridge)
+{
+	return ast_bridge_remove(bridge, control->channel);
+}

Modified: team/kmoore/stasis-bridging_events-rework/res/stasis_http/resource_bridges.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/res/stasis_http/resource_bridges.c?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/res/stasis_http/resource_bridges.c (original)
+++ team/kmoore/stasis-bridging_events-rework/res/stasis_http/resource_bridges.c Wed May 29 09:59:05 2013
@@ -32,32 +32,237 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "resource_bridges.h"
+#include "asterisk/stasis.h"
+#include "asterisk/stasis_bridging.h"
+#include "asterisk/stasis_app.h"
+#include "asterisk/channel.h"
+#include "asterisk/bridging.h"
+
+/*!
+ * \brief Finds the control object for a bridge, filling the response with an
+ * error, if appropriate.
+ * \param[out] response Response to fill with an error if control is not found.
+ * \param bridge_id ID of the bridge to lookup.
+ * \return Bridge control object.
+ * \return \c NULL if control object does not exist.
+ */
+static struct ast_bridge *find_bridge(
+	struct stasis_http_response *response,
+	const char *bridge_id)
+{
+	RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+
+	ast_assert(response != NULL);
+
+	bridge = stasis_app_bridge_find_by_id(bridge_id);
+	if (bridge == NULL) {
+		stasis_http_response_error(response, 409, "Conflict",
+			"Bridge not in Stasis application");
+		return NULL;
+	}
+
+	ao2_ref(bridge, +1);
+	return bridge;
+}
+
+/*!
+ * \brief Finds the control object for a channel, filling the response with an
+ * error, if appropriate.
+ * \param[out] response Response to fill with an error if control is not found.
+ * \param channel_id ID of the channel to lookup.
+ * \return Channel control object.
+ * \return \c NULL if control object does not exist.
+ */
+static struct stasis_app_control *find_channel_control(
+	struct stasis_http_response *response,
+	const char *channel_id)
+{
+	RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
+
+	ast_assert(response != NULL);
+
+	control = stasis_app_control_find_by_channel_id(channel_id);
+	if (control == NULL) {
+		/* Distinguish between 404 and 409 errors */
+		RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
+		chan = ast_channel_get_by_name(channel_id);
+		if (chan == NULL) {
+			stasis_http_response_error(response, 404, "Not Found",
+				"Channel not found");
+			return NULL;
+		}
+
+		stasis_http_response_error(response, 409, "Conflict",
+			"Channel not in Stasis application");
+		return NULL;
+	}
+
+	ao2_ref(control, +1);
+	return control;
+}
 
 void stasis_http_add_channel_to_bridge(struct ast_variable *headers, struct ast_add_channel_to_bridge_args *args, struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_add_channel_to_bridge\n");
-}
+	RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
+	RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
+	struct ast_channel *channel;
+	if (!bridge) {
+		return;
+	}
+
+	control = find_channel_control(response, args->channel);
+	if (!control) {
+		return;
+	}
+
+	/* channel reference is consumed by the add channel call */
+	channel = ast_channel_get_by_name(args->channel);
+	if (!channel) {
+		stasis_http_response_error(response, 404, "Not Found",
+			"Could not acquire channel structure");
+		return;
+	}
+
+	if (stasis_app_control_add_channel_to_bridge(control, bridge)) {
+		stasis_http_response_error(response, 500, "Internal Error",
+			"Could not add channel to bridge");
+	} else {
+		stasis_http_response_no_content(response);
+	}
+}
+
 void stasis_http_remove_channel_from_bridge(struct ast_variable *headers, struct ast_remove_channel_from_bridge_args *args, struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_remove_channel_from_bridge\n");
-}
+	RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
+	RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
+	struct ast_channel *channel;
+	if (!bridge) {
+		return;
+	}
+
+	control = find_channel_control(response, args->channel);
+	if (!control) {
+		return;
+	}
+
+	/* channel reference is consumed by the remove channel call */
+	channel = ast_channel_get_by_name(args->channel);
+	if (!channel) {
+		stasis_http_response_error(response, 404, "Not Found",
+			"Could not acquire channel structure");
+		return;
+	}
+
+	if (stasis_app_control_remove_channel_from_bridge(control, bridge)) {
+		stasis_http_response_error(response, 500, "Internal Error",
+			"Could not remove channel from bridge");
+	} else {
+		stasis_http_response_no_content(response);
+	}
+}
+
 void stasis_http_record_bridge(struct ast_variable *headers, struct ast_record_bridge_args *args, struct stasis_http_response *response)
 {
 	ast_log(LOG_ERROR, "TODO: stasis_http_record_bridge\n");
 }
+
 void stasis_http_get_bridge(struct ast_variable *headers, struct ast_get_bridge_args *args, struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_get_bridge\n");
-}
+	RAII_VAR(struct ast_bridge_snapshot *, snapshot, ast_bridge_snapshot_get_latest(args->bridge_id), ao2_cleanup);
+	if (snapshot) {
+		stasis_http_response_ok(response,
+			ast_bridge_snapshot_to_json(snapshot));
+		return;
+	}
+
+	stasis_http_response_error(
+		response, 404, "Not Found",
+		"Bridge not found");
+}
+
 void stasis_http_delete_bridge(struct ast_variable *headers, struct ast_delete_bridge_args *args, struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_delete_bridge\n");
-}
+	RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
+	if (!bridge) {
+		return;
+	}
+
+	stasis_app_bridge_remove(bridge);
+	stasis_http_response_no_content(response);
+}
+
 void stasis_http_get_bridges(struct ast_variable *headers, struct ast_get_bridges_args *args, struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_get_bridges\n");
-}
+	RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, ao2_cleanup);
+	RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ao2_iterator i;
+	void *obj;
+
+	caching_topic = ast_bridge_topic_all_cached();
+	if (!caching_topic) {
+		stasis_http_response_error(
+			response, 500, "Internal Server Error",
+			"Message bus not initialized");
+		return;
+	}
+	ao2_ref(caching_topic, +1);
+
+	snapshots = stasis_cache_dump(caching_topic, ast_bridge_snapshot_type());
+	if (!snapshots) {
+		stasis_http_response_alloc_failed(response);
+		return;
+	}
+
+	json = ast_json_array_create();
+	if (!json) {
+		stasis_http_response_alloc_failed(response);
+		return;
+	}
+
+	i = ao2_iterator_init(snapshots, 0);
+	while ((obj = ao2_iterator_next(&i))) {
+		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
+		struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);
+		int r = ast_json_array_append(
+			json, ast_bridge_snapshot_to_json(snapshot));
+		if (r != 0) {
+			stasis_http_response_alloc_failed(response);
+			return;
+		}
+	}
+	ao2_iterator_destroy(&i);
+
+	stasis_http_response_ok(response, ast_json_ref(json));
+}
+
 void stasis_http_new_bridge(struct ast_variable *headers, struct ast_new_bridge_args *args, struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_new_bridge\n");
-}
+	RAII_VAR(struct ast_bridge *, bridge, stasis_app_create_bridge(args->type), ao2_cleanup);
+	RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
+
+	if (!bridge) {
+		stasis_http_response_error(
+			response, 500, "Internal Error",
+			"Unable to create bridge");
+		return;
+	}
+
+	snapshot = ast_bridge_snapshot_get_latest(bridge->uniqueid);
+	if (snapshot) {
+		stasis_http_response_ok(response,
+			ast_bridge_snapshot_to_json(snapshot));
+		return;
+	}
+
+	snapshot = ast_bridge_snapshot_create(bridge);
+	if (snapshot) {
+		stasis_http_response_ok(response,
+			ast_bridge_snapshot_to_json(snapshot));
+		return;
+	}
+
+	stasis_http_response_error(
+		response, 404, "Not Found",
+		"Bridge not found");
+}

Modified: team/kmoore/stasis-bridging_events-rework/res/stasis_json/resource_events.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events-rework/res/stasis_json/resource_events.h?view=diff&rev=390023&r1=390022&r2=390023
==============================================================================
--- team/kmoore/stasis-bridging_events-rework/res/stasis_json/resource_events.h (original)
+++ team/kmoore/stasis-bridging_events-rework/res/stasis_json/resource_events.h Wed May 29 09:59:05 2013
@@ -122,6 +122,22 @@
 	);
 
 /*!
+ * \brief Channel variable changed.
+ *
+ * \param channel The channel on which the variable was set.
+ * \param blob JSON blob containing the following parameters:
+ * - variable: string - The variable that changed. (required)
+ * - value: string - The new value of the variable. (required)
+ *
+ * \retval NULL on error
+ * \retval JSON (ast_json) describing the event
+ */
+struct ast_json *stasis_json_event_channel_varset_create(
+	struct ast_channel_snapshot *channel_snapshot,
+	struct ast_json *blob
+	);
+
+/*!
  * \brief Notification that a bridge has been destroyed.
  *
  * \param bridge The bridge to be used to generate this event
@@ -163,18 +179,17 @@
 	);
 
 /*!
- * \brief Channel variable changed.
- *
- * \param channel The channel on which the variable was set.
- * \param blob JSON blob containing the following parameters:
- * - variable: string - The variable that changed. (required)
- * - value: string - The new value of the variable. (required)
- *
- * \retval NULL on error
- * \retval JSON (ast_json) describing the event
- */
-struct ast_json *stasis_json_event_channel_varset_create(
-	struct ast_channel_snapshot *channel_snapshot,
+ * \brief Notification that one bridge has merged into another.
+ *
+ * \param bridge The bridge to be used to generate this event
+ * \param blob JSON blob containing the following parameters:
+ * - bridge_from: Bridge  (required)
+ *
+ * \retval NULL on error
+ * \retval JSON (ast_json) describing the event
+ */
+struct ast_json *stasis_json_event_bridge_merged_create(
+	struct ast_bridge_snapshot *bridge_snapshot,
 	struct ast_json *blob
 	);
 
@@ -318,15 +333,17 @@
  * - caller_presentation: integer (required)
  * PlaybackStarted
  * - playback: Playback (required)
+ * ChannelVarset
+ * - variable: string (required)
+ * - value: string (required)
  * BridgeDestroyed
  * ApplicationReplaced
  * - application: string (required)
  * ChannelDestroyed
  * - cause: integer (required)
  * - cause_txt: string (required)
- * ChannelVarset
- * - variable: string (required)
- * - value: string (required)
+ * BridgeMerged
+ * - bridge_from: Bridge (required)
  * ChannelLeftBridge
  * ChannelCreated
  * StasisStart
@@ -342,24 +359,25 @@
  * ChannelDtmfReceived
  * - digit: string (required)
  * Event

[... 61 lines stripped ...]



More information about the asterisk-commits mailing list