[asterisk-commits] kmoore: branch kmoore/stasis-bridging_events r389769 - in /team/kmoore/stasis...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Sat May 25 01:38:44 CDT 2013
Author: kmoore
Date: Sat May 25 01:38:40 2013
New Revision: 389769
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=389769
Log:
Flesh out res_stasis bridging messages
This adds the BridgeMerged stasis message and fleshes out bridge event
support in res_stasis.
Modified:
team/kmoore/stasis-bridging_events/res/res_stasis.c
team/kmoore/stasis-bridging_events/res/res_stasis_json_events.c
team/kmoore/stasis-bridging_events/res/res_stasis_json_events.exports.in
team/kmoore/stasis-bridging_events/res/stasis_json/resource_events.h
team/kmoore/stasis-bridging_events/rest-api/api-docs/events.json
Modified: team/kmoore/stasis-bridging_events/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events/res/res_stasis.c?view=diff&rev=389769&r1=389768&r2=389769
==============================================================================
--- team/kmoore/stasis-bridging_events/res/res_stasis.c (original)
+++ team/kmoore/stasis-bridging_events/res/res_stasis.c Sat May 25 01:38:40 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"
@@ -92,6 +93,9 @@
/*! \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)
{
@@ -170,7 +174,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 +306,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 +317,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 +346,7 @@
return;
}
- watching_apps = get_watching_apps(obj->snapshot->uniqueid);
+ watching_apps = get_apps_watching_channel(obj->snapshot->uniqueid);
if (!watching_apps) {
return;
}
@@ -679,6 +683,182 @@
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 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) {
+ msg = stasis_json_event_bridge_destroyed_create(old_snapshot);
+ } 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 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, ast_str_container_alloc(1), 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) {
+ return;
+ }
+ update_apps_list(watching_apps_all, watching_apps_to);
+
+ watching_apps_from = get_apps_watching_bridge(merge->from->uniqueid);
+ if (!watching_apps_from) {
+ return;
+ }
+ 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, ast_str_container_alloc(1), 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;
+ }
+ update_apps_list(watching_apps_all, watching_apps_bridge);
+
+ watching_apps_channel = get_apps_watching_channel(obj->channel->uniqueid);
+ if (!watching_apps_channel) {
+ return;
+ }
+ 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_channel, NULL, ao2_cleanup);
+ RAII_VAR(struct ao2_container *, watching_apps_bridge, NULL, ao2_cleanup);
+ RAII_VAR(struct ao2_container *, watching_apps_all, ast_str_container_alloc(1), 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;
+ }
+ update_apps_list(watching_apps_all, watching_apps_bridge);
+
+ watching_apps_channel = get_apps_watching_channel(obj->channel->uniqueid);
+ if (!watching_apps_channel) {
+ return;
+ }
+ update_apps_list(watching_apps_all, watching_apps_channel);
+
+ if (!ao2_container_count(watching_apps_all)) {
+ return;
+ }
+
+ msg = stasis_json_event_channel_left_bridge_create(obj->bridge, obj->channel);
+
+ distribute_message(watching_apps_all, msg);
+}
+
static int load_module(void)
{
int r = 0;
@@ -700,7 +880,7 @@
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 +889,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;
}
@@ -718,6 +911,9 @@
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;
Modified: team/kmoore/stasis-bridging_events/res/res_stasis_json_events.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events/res/res_stasis_json_events.c?view=diff&rev=389769&r1=389768&r2=389769
==============================================================================
--- team/kmoore/stasis-bridging_events/res/res_stasis_json_events.c (original)
+++ team/kmoore/stasis-bridging_events/res/res_stasis_json_events.c Sat May 25 01:38:40 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/res/res_stasis_json_events.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events/res/res_stasis_json_events.exports.in?view=diff&rev=389769&r1=389768&r2=389769
==============================================================================
--- team/kmoore/stasis-bridging_events/res/res_stasis_json_events.exports.in (original)
+++ team/kmoore/stasis-bridging_events/res/res_stasis_json_events.exports.in Sat May 25 01:38:40 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/res/stasis_json/resource_events.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events/res/stasis_json/resource_events.h?view=diff&rev=389769&r1=389768&r2=389769
==============================================================================
--- team/kmoore/stasis-bridging_events/res/stasis_json/resource_events.h (original)
+++ team/kmoore/stasis-bridging_events/res/stasis_json/resource_events.h Sat May 25 01:38:40 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
- * - stasis_start: StasisStart
+ * - channel_varset: ChannelVarset
* - channel_created: ChannelCreated
* - channel_destroyed: ChannelDestroyed
* - channel_entered_bridge: ChannelEnteredBridge
* - channel_left_bridge: ChannelLeftBridge
+ * - bridge_merged: BridgeMerged
* - channel_dialplan: ChannelDialplan
- * - channel_varset: ChannelVarset
* - application_replaced: ApplicationReplaced
* - channel_state_change: ChannelStateChange
* - bridge_created: BridgeCreated
* - application: string (required)
* - channel_hangup_request: ChannelHangupRequest
* - channel_userevent: ChannelUserevent
- * - playback_started: PlaybackStarted
+ * - stasis_start: StasisStart
* - channel_snapshot: ChannelSnapshot
* - channel_dtmf_received: ChannelDtmfReceived
* - channel_caller_id: ChannelCallerId
* - bridge_destroyed: BridgeDestroyed
+ * - playback_started: PlaybackStarted
* - playback_finished: PlaybackFinished
* - stasis_end: StasisEnd
* StasisEnd
Modified: team/kmoore/stasis-bridging_events/rest-api/api-docs/events.json
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging_events/rest-api/api-docs/events.json?view=diff&rev=389769&r1=389768&r2=389769
==============================================================================
--- team/kmoore/stasis-bridging_events/rest-api/api-docs/events.json (original)
+++ team/kmoore/stasis-bridging_events/rest-api/api-docs/events.json Sat May 25 01:38:40 2013
@@ -56,6 +56,7 @@
"application_replaced": { "type": "ApplicationReplaced" },
"bridge_created": { "type": "BridgeCreated" },
"bridge_destroyed": { "type": "BridgeDestroyed" },
+ "bridge_merged": { "type": "BridgeMerged" },
"channel_created": { "type": "ChannelCreated" },
"channel_destroyed": { "type": "ChannelDestroyed" },
"channel_snapshot": { "type": "ChannelSnapshot" },
@@ -127,6 +128,20 @@
}
}
},
+ "BridgeMerged": {
+ "id": "BridgeMerged",
+ "description": "Notification that one bridge has merged into another.",
+ "properties": {
+ "bridge": {
+ "required": true,
+ "type": "Bridge"
+ },
+ "bridge_from": {
+ "required": true,
+ "type": "Bridge"
+ }
+ }
+ },
"ChannelCreated": {
"id": "ChannelCreated",
"description": "Notification that a channel has been created.",
More information about the asterisk-commits
mailing list