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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Sep 11 00:07:07 CDT 2013


Author: dlee
Date: Wed Sep 11 00:07:04 2013
New Revision: 398803

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=398803
Log:
Subscribe

Modified:
    team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/stasis_app.h
    team/dlee/ASTERISK-22451-ari-subscribe/res/ari/resource_applications.c
    team/dlee/ASTERISK-22451-ari-subscribe/res/res_stasis.c

Modified: team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/stasis_app.h?view=diff&rev=398803&r1=398802&r2=398803
==============================================================================
--- team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/stasis_app.h (original)
+++ team/dlee/ASTERISK-22451-ari-subscribe/include/asterisk/stasis_app.h Wed Sep 11 00:07:04 2013
@@ -101,6 +101,18 @@
  */
 int stasis_app_send(const char *app_name, struct ast_json *message);
 
+enum stasis_app_subscribe_res {
+	STASIS_ASR_OK,
+	STASIS_ASR_APP_NOT_FOUND,
+	STASIS_ASR_EVENT_SOURCE_NOT_FOUND,
+	STASIS_ASR_EVENT_SOURCE_BAD_SCHEME,
+	STASIS_ASR_INTERNAL_ERROR,
+};
+
+enum stasis_app_subscribe_res stasis_app_subscribe(const char *app_name,
+	const char **event_sources, int event_sources_count,
+	struct ast_json **json);
+
 /*! @} */
 
 /*! @{ */

Modified: team/dlee/ASTERISK-22451-ari-subscribe/res/ari/resource_applications.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22451-ari-subscribe/res/ari/resource_applications.c?view=diff&rev=398803&r1=398802&r2=398803
==============================================================================
--- team/dlee/ASTERISK-22451-ari-subscribe/res/ari/resource_applications.c (original)
+++ team/dlee/ASTERISK-22451-ari-subscribe/res/ari/resource_applications.c Wed Sep 11 00:07:04 2013
@@ -28,6 +28,7 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include "asterisk/stasis_app.h"
 #include "resource_applications.h"
 
 void ast_ari_get_applications(struct ast_variable *headers,
@@ -42,12 +43,46 @@
 {
 	ast_log(LOG_ERROR, "TODO: ast_ari_get_application\n");
 }
+
 void ast_ari_application_subscribe(struct ast_variable *headers,
 	struct ast_application_subscribe_args *args,
 	struct ast_ari_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: ast_ari_application_subscribe\n");
+	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	enum stasis_app_subscribe_res res;
+
+	if (args->event_source_count == 0) {
+		ast_ari_response_error(response, 400, "Bad Request",
+			"Missing parameter eventSource");
+		return;
+	}
+
+	res = stasis_app_subscribe(args->application_name, args->event_source,
+		args->event_source_count, &json);
+
+	switch (res) {
+	case STASIS_ASR_OK:
+		ast_ari_response_ok(response, json);
+		break;
+	case STASIS_ASR_APP_NOT_FOUND:
+		ast_ari_response_error(response, 404, "Not Found",
+			"Application not found");
+		break;
+	case STASIS_ASR_EVENT_SOURCE_NOT_FOUND:
+		ast_ari_response_error(response, 422, "Unprocessable Entity",
+			"Event source does not exist");
+		break;
+	case STASIS_ASR_EVENT_SOURCE_BAD_SCHEME:
+		ast_ari_response_error(response, 400, "Bad Request",
+			"Invalid event source URI scheme");
+		break;
+	case STASIS_ASR_INTERNAL_ERROR:
+		ast_ari_response_error(response, 500, "Internal Server Error",
+			"Error processing request");
+	}
+
 }
+
 void ast_ari_application_unsubscribe(struct ast_variable *headers,
 	struct ast_application_unsubscribe_args *args,
 	struct ast_ari_response *response)

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=398803&r1=398802&r2=398803
==============================================================================
--- team/dlee/ASTERISK-22451-ari-subscribe/res/res_stasis.c (original)
+++ team/dlee/ASTERISK-22451-ari-subscribe/res/res_stasis.c Wed Sep 11 00:07:04 2013
@@ -783,6 +783,59 @@
 	cleanup();
 }
 
+#define CHANNEL_SCHEME "channel:"
+
+enum stasis_app_subscribe_res stasis_app_subscribe(const char *app_name,
+	const char **event_sources, int event_sources_count,
+	struct ast_json **json)
+{
+	RAII_VAR(struct app *, app, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_channel **, channels, NULL, ast_free);
+	enum stasis_app_subscribe_res res = STASIS_ASR_OK;
+	int i;
+
+	if (app_name) {
+		app = ao2_find(apps_registry, app_name, OBJ_KEY);
+	}
+
+	if (!app) {
+		return STASIS_ASR_APP_NOT_FOUND;
+	}
+
+	channels = ast_calloc(event_sources_count, sizeof(*channels));
+	if (!channels) {
+		return STASIS_ASR_INTERNAL_ERROR;
+	}
+
+	for (i = 0; res == STASIS_ASR_OK && i < event_sources_count; ++i) {
+		if (ast_begins_with(event_sources[i], CHANNEL_SCHEME)) {
+			channels[i] = ast_channel_get_by_name(event_sources[i] +
+					strlen(CHANNEL_SCHEME));
+			if (!channels[i]) {
+				res = STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
+			}
+		} else {
+			res = STASIS_ASR_EVENT_SOURCE_BAD_SCHEME;
+		}
+	}
+
+	for (i = 0; res == STASIS_ASR_OK && i < event_sources_count; ++i) {
+		int sub_res = app_subscribe_channel(app, channels[i]);
+		if (sub_res != 0) {
+			ast_log(LOG_ERROR,
+				"Error subscribing app '%s' to channel '%s'\n",
+				app_name, ast_channel_name(channels[i]));
+			res = STASIS_ASR_INTERNAL_ERROR;
+		}
+	}
+
+	for (i = 0; i < event_sources_count; ++i) {
+		channels[i] = ast_channel_cleanup(channels[i]);
+	}
+
+	return res;
+}
+
 void stasis_app_ref(void)
 {
 	ast_module_ref(ast_module_info->self);




More information about the asterisk-commits mailing list