[asterisk-commits] dlee: branch dlee/ASTERISK-22451-ari-subscribe r399218 - in /team/dlee/ASTERI...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Sep 16 14:58:16 CDT 2013


Author: dlee
Date: Mon Sep 16 14:58:14 2013
New Revision: 399218

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=399218
Log:
Endpoint subscriptions

Modified:
    team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/endpoints.h
    team/dlee/ASTERISK-22451-ari-subscribe/main/asterisk.c
    team/dlee/ASTERISK-22451-ari-subscribe/main/endpoints.c
    team/dlee/ASTERISK-22451-ari-subscribe/res/res_stasis.c
    team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.c
    team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.h

Modified: team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/endpoints.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/endpoints.h?view=diff&rev=399218&r1=399217&r2=399218
==============================================================================
--- team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/endpoints.h (original)
+++ team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/endpoints.h Mon Sep 16 14:58:14 2013
@@ -45,6 +45,13 @@
 #include "asterisk/json.h"
 
 /*!
+ * \brief Endpoint support initialization.
+ * \return 0 on success.
+ * \return Non-zer on error.
+ */
+int ast_endpoint_init(void);
+
+/*!
  * \brief Valid states for an endpoint.
  * \since 12
  */
@@ -75,6 +82,8 @@
  * \since 12
  */
 struct ast_endpoint;
+
+struct ast_endpoint *ast_endpoint_find_by_id(const char *id);
 
 /*!
  * \brief Create an endpoint struct.
@@ -125,6 +134,8 @@
  */
 const char *ast_endpoint_get_resource(const struct ast_endpoint *endpoint);
 
+const char *ast_endpoint_get_id(const struct ast_endpoint *endpoint);
+
 /*!
  * \brief Updates the state of the given endpoint.
  *

Modified: team/dlee/ASTERISK-22451-ari-subscribe/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22451-ari-subscribe/main/asterisk.c?view=diff&rev=399218&r1=399217&r2=399218
==============================================================================
--- team/dlee/ASTERISK-22451-ari-subscribe/main/asterisk.c (original)
+++ team/dlee/ASTERISK-22451-ari-subscribe/main/asterisk.c Mon Sep 16 14:58:14 2013
@@ -247,6 +247,7 @@
 #include "asterisk/stasis_endpoints.h"
 #include "asterisk/stasis_system.h"
 #include "asterisk/security_events.h"
+#include "asterisk/endpoints.h"
 
 #include "../defaults.h"
 
@@ -4340,6 +4341,11 @@
 
 	ast_channels_init();
 
+	if (ast_endpoint_init()) {
+		printf ("%s", term_quit());
+		exit(1);
+	}
+
 	if ((moduleresult = load_modules(1))) {		/* Load modules, pre-load only */
 		printf("%s", term_quit());
 		exit(moduleresult == -2 ? 2 : 1);

Modified: team/dlee/ASTERISK-22451-ari-subscribe/main/endpoints.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22451-ari-subscribe/main/endpoints.c?view=diff&rev=399218&r1=399217&r2=399218
==============================================================================
--- team/dlee/ASTERISK-22451-ari-subscribe/main/endpoints.c (original)
+++ team/dlee/ASTERISK-22451-ari-subscribe/main/endpoints.c Mon Sep 16 14:58:14 2013
@@ -40,7 +40,12 @@
 #include "asterisk/stringfields.h"
 
 /*! Buckets for endpoint->channel mappings. Keep it prime! */
+#define ENDPOINT_CHANNEL_BUCKETS 127
+
+/*! Buckets for endpoint hash. Keep it prime! */
 #define ENDPOINT_BUCKETS 127
+
+static struct ao2_container *endpoints;
 
 struct ast_endpoint {
 	AST_DECLARE_STRING_FIELDS(
@@ -65,6 +70,39 @@
 	struct ao2_container *channel_ids;
 };
 
+static int endpoint_hash(const void *obj, int flags)
+{
+	const struct ast_endpoint *endpoint;
+	const char *key;
+
+	switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
+	case OBJ_KEY:
+		key = obj;
+		return ast_str_hash(key);
+	case OBJ_POINTER:
+		endpoint = obj;
+		return ast_str_hash(endpoint->id);
+	default:
+		/* Hash can only work on something with a full key. */
+		ast_assert(0);
+		return 0;
+	}
+}
+
+static int endpoint_cmp(void *obj, void *arg, int flags)
+{
+	const struct ast_endpoint *left = obj;
+	const struct ast_endpoint *right = arg;
+
+	ast_assert(!(flags & OBJ_KEY));
+
+	if (strcmp(left->id, right->id) == 0) {
+		return CMP_MATCH;
+	}
+
+	return 0;
+}
+
 struct stasis_topic *ast_endpoint_topic(struct ast_endpoint *endpoint)
 {
 	if (!endpoint) {
@@ -218,7 +256,7 @@
 	/* All access to channel_ids should be covered by the endpoint's
 	 * lock; no extra lock needed. */
 	endpoint->channel_ids = ast_str_container_alloc_options(
-		AO2_ALLOC_OPT_LOCK_NOLOCK, ENDPOINT_BUCKETS);
+		AO2_ALLOC_OPT_LOCK_NOLOCK, ENDPOINT_CHANNEL_BUCKETS);
 	if (!endpoint->channel_ids) {
 		return NULL;
 	}
@@ -241,6 +279,8 @@
 
 	endpoint_publish_snapshot(endpoint);
 
+	ao2_link(endpoints, endpoint);
+
 	ao2_ref(endpoint, +1);
 	return endpoint;
 }
@@ -269,6 +309,8 @@
 	if (endpoint == NULL) {
 		return;
 	}
+
+	ao2_unlink(endpoints, endpoint);
 
 	clear_msg = create_endpoint_snapshot_message(endpoint);
 	if (clear_msg) {
@@ -354,3 +396,23 @@
 	ao2_ref(snapshot, +1);
 	return snapshot;
 }
+
+static void endpoint_cleanup(void)
+{
+	ao2_cleanup(endpoints);
+	endpoints = NULL;
+}
+
+int ast_endpoint_init(void)
+{
+	ast_register_cleanup(endpoint_cleanup);
+
+	endpoints = ao2_container_alloc(ENDPOINT_BUCKETS, endpoint_hash,
+		endpoint_cmp);
+
+	if (!endpoints) {
+		return -1;
+	}
+
+	return 0;
+}

Modified: team/dlee/ASTERISK-22451-ari-subscribe/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22451-ari-subscribe/res/res_stasis.c?view=diff&rev=399218&r1=399217&r2=399218
==============================================================================
--- team/dlee/ASTERISK-22451-ari-subscribe/res/res_stasis.c (original)
+++ team/dlee/ASTERISK-22451-ari-subscribe/res/res_stasis.c Mon Sep 16 14:58:14 2013
@@ -823,6 +823,7 @@
 
 #define CHANNEL_SCHEME "channel:"
 #define BRIDGE_SCHEME "bridge:"
+#define ENDPOINT_SCHEME "endpoint:"
 
 struct event_source {
 	enum {
@@ -880,6 +881,15 @@
 				ast_debug(1, "  Bridge not found\n");
 				res = STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
 			}
+		} else if (ast_begins_with(event_source_names[i], ENDPOINT_SCHEME)) {
+			event_sources[i].event_source_type =
+				EVENT_SOURCE_ENDPOINT;
+			event_sources[i].endpoint = ast_endpoint_find_by_id(
+				event_source_names[i] + strlen(ENDPOINT_SCHEME));
+			if (!event_sources[i].endpoint) {
+				ast_debug(1, "  Endpoint not found\n");
+				res = STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
+			}
 		} else {
 			ast_debug(1, "  Invalid scheme\n");
 			res = STASIS_ASR_EVENT_SOURCE_BAD_SCHEME;
@@ -893,12 +903,16 @@
 
 		switch (event_sources[i].event_source_type) {
 		case EVENT_SOURCE_CHANNEL:
-			sub_res = app_subscribe_channel(app, event_sources[i].channel);
+			sub_res = app_subscribe_channel(app,
+				event_sources[i].channel);
 			break;
 		case EVENT_SOURCE_BRIDGE:
-			sub_res = app_subscribe_bridge(app, event_sources[i].bridge);
+			sub_res = app_subscribe_bridge(app,
+				event_sources[i].bridge);
 			break;
 		case EVENT_SOURCE_ENDPOINT:
+			sub_res = app_subscribe_endpoint(app,
+				event_sources[i].endpoint);
 			break;
 		}
 
@@ -963,6 +977,12 @@
 			if (!app_is_subscribed_bridge_id(app, bridge_id)) {
 				res = STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
 			}
+		} else if (ast_begins_with(event_source_names[i], ENDPOINT_SCHEME)) {
+			const char *endpoint_id = event_source_names[i] +
+				strlen(ENDPOINT_SCHEME);
+			if (!app_is_subscribed_endpoint_id(app, endpoint_id)) {
+				res = STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
+			}
 		} else {
 			res = STASIS_ASR_EVENT_SOURCE_BAD_SCHEME;
 		}
@@ -977,6 +997,10 @@
 			const char *bridge_id = event_source_names[i] +
 				strlen(BRIDGE_SCHEME);
 			app_unsubscribe_bridge_id(app, bridge_id);
+		} else if (ast_begins_with(event_source_names[i], ENDPOINT_SCHEME)) {
+			const char *endpoint_id = event_source_names[i] +
+				strlen(ENDPOINT_SCHEME);
+			app_unsubscribe_endpoint_id(app, endpoint_id);
 		}
 	}
 

Modified: team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.c?view=diff&rev=399218&r1=399217&r2=399218
==============================================================================
--- team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.c (original)
+++ team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.c Mon Sep 16 14:58:14 2013
@@ -33,6 +33,7 @@
 #include "asterisk/stasis_app.h"
 #include "asterisk/stasis_bridges.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_endpoints.h"
 #include "asterisk/stasis_message_router.h"
 
 struct app {
@@ -179,6 +180,42 @@
 	ao2_ref(forwards, +1);
 	return forwards;
 }
+
+/*! Forward a endpoint's topics to an app */
+static struct app_forwards *forwards_create_endpoint(struct app *app,
+	struct ast_endpoint *endpoint)
+{
+	RAII_VAR(struct app_forwards *, forwards, NULL, ao2_cleanup);
+
+	if (!app || !endpoint) {
+		return NULL;
+	}
+
+	forwards = forwards_create(app, ast_endpoint_get_id(endpoint));
+	if (!forwards) {
+		return NULL;
+	}
+
+	forwards->topic_forward = stasis_forward_all(ast_endpoint_topic(endpoint),
+		app->topic);
+	if (!forwards->topic_forward) {
+		return NULL;
+	}
+
+	forwards->topic_cached_forward = stasis_forward_all(
+		ast_endpoint_topic_cached(endpoint), app->topic);
+	if (!forwards->topic_cached_forward) {
+		/* Half-subscribed is a bad thing */
+		stasis_unsubscribe(forwards->topic_forward);
+		forwards->topic_forward = NULL;
+		return NULL;
+	}
+
+	ao2_ref(forwards, +1);
+	return forwards;
+}
+
+
 
 static int forwards_sort(const void *obj_left, const void *obj_right, int flags)
 {
@@ -829,3 +866,28 @@
 	forwards = ao2_find(app->forwards, bridge_id, OBJ_SEARCH_KEY);
 	return forwards != NULL;
 }
+
+int app_subscribe_endpoint(struct app *app, struct ast_endpoint *endpoint)
+{
+	if (!app || !endpoint) {
+		return -1;
+	} else {
+		RAII_VAR(struct app_forwards *, forwards, NULL, ao2_cleanup);
+		SCOPED_AO2LOCK(lock, app->forwards);
+
+		forwards = ao2_find(app->forwards, ast_endpoint_get_id(endpoint),
+			OBJ_SEARCH_KEY | OBJ_NOLOCK);
+
+		if (!forwards) {
+			/* Forwards not found, create one */
+			forwards = forwards_create_endpoint(app, endpoint);
+			if (!forwards) {
+				return -1;
+			}
+			ao2_link_flags(app->forwards, forwards, OBJ_NOLOCK);
+		}
+
+		++forwards->interested;
+		return 0;
+	}
+}

Modified: team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.h?view=diff&rev=399218&r1=399217&r2=399218
==============================================================================
--- team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.h (original)
+++ team/dlee/ASTERISK-22451-ari-subscribe/res/stasis/app.h Mon Sep 16 14:58:14 2013
@@ -162,4 +162,10 @@
 
 int app_is_subscribed_bridge_id(struct app *app, const char *bridge_id);
 
+int app_subscribe_endpoint(struct app *app, struct ast_endpoint *endpoint);
+
+int app_unsubscribe_endpoint_id(struct app *app, const char *endpoint_id);
+
+int app_is_subscribed_endpoint_id(struct app *app, const char *endpoint_id);
+
 #endif /* _ASTERISK_RES_STASIS_APP_H */




More information about the asterisk-commits mailing list