[asterisk-commits] dlee: branch dlee/ari-url-shuffle r391727 - in /team/dlee/ari-url-shuffle: in...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jun 13 15:25:48 CDT 2013


Author: dlee
Date: Thu Jun 13 15:25:46 2013
New Revision: 391727

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=391727
Log:
Wow. I think that actually works

Added:
    team/dlee/ari-url-shuffle/res/stasis_http/ari_websockets.c   (with props)
Modified:
    team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h
    team/dlee/ari-url-shuffle/res/Makefile
    team/dlee/ari-url-shuffle/res/res_http_websocket.c
    team/dlee/ari-url-shuffle/res/res_stasis_http.c
    team/dlee/ari-url-shuffle/res/res_stasis_http.exports.in
    team/dlee/ari-url-shuffle/res/res_stasis_http_events.c
    team/dlee/ari-url-shuffle/res/res_stasis_websocket.c
    team/dlee/ari-url-shuffle/res/stasis_http/resource_events.c
    team/dlee/ari-url-shuffle/rest-api-templates/res_stasis_http_resource.c.mustache

Modified: team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h?view=diff&rev=391727&r1=391726&r2=391727
==============================================================================
--- team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h (original)
+++ team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h Thu Jun 13 15:25:46 2013
@@ -163,8 +163,10 @@
  *
  * \param session Session to write to.
  * \param message Message to send.
- */
-void ari_websocket_session_write(struct ari_websocket_session *session,
+ * \return 0 on success.
+ * \return Non-zero on error.
+ */
+int ari_websocket_session_write(struct ari_websocket_session *session,
 	struct ast_json *message);
 
 /*!
@@ -177,6 +179,16 @@
 void stasis_websocket_callback(struct ast_websocket *session, struct ast_variable *parameters, struct ast_variable *headers);
 
 /*!
+ * \brief The stock message to return when out of memory.
+ *
+ * The refcount is NOT bumped on this object, so ast_json_ref() if you want to
+ * keep the reference.
+ *
+ * \return JSON message specifying an out-of-memory error.
+ */
+struct ast_json *ari_oom_json(void);
+
+/*!
  * \brief Fill in an error \a stasis_http_response.
  * \param response Response to fill in.
  * \param response_code HTTP response code.

Modified: team/dlee/ari-url-shuffle/res/Makefile
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/Makefile?view=diff&rev=391727&r1=391726&r2=391727
==============================================================================
--- team/dlee/ari-url-shuffle/res/Makefile (original)
+++ team/dlee/ari-url-shuffle/res/Makefile Thu Jun 13 15:25:46 2013
@@ -80,5 +80,8 @@
 $(if $(filter res_parking,$(EMBEDDED_MODS)),modules.link,res_parking.so): $(subst .c,.o,$(wildcard parking/*.c))
 $(subst .c,.o,$(wildcard parking/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_parking)
 
+res_stasis_http.so: stasis_http/ari_websockets.o
+stasis_http/ari_websockets.o: _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_stasis_http_asterisk)
+
 # Dependencies for res_stasis_http_*.so are generated, so they're in this file
 include stasis_http.make

Modified: team/dlee/ari-url-shuffle/res/res_http_websocket.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/res_http_websocket.c?view=diff&rev=391727&r1=391726&r2=391727
==============================================================================
--- team/dlee/ari-url-shuffle/res/res_http_websocket.c (original)
+++ team/dlee/ari-url-shuffle/res/res_http_websocket.c Thu Jun 13 15:25:46 2013
@@ -282,7 +282,7 @@
 
 void AST_OPTIONAL_API_NAME(ast_websocket_unref)(struct ast_websocket *session)
 {
-	ao2_ref(session, -1);
+	ao2_cleanup(session);
 }
 
 int AST_OPTIONAL_API_NAME(ast_websocket_fd)(struct ast_websocket *session)

Modified: team/dlee/ari-url-shuffle/res/res_stasis_http.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/res_stasis_http.c?view=diff&rev=391727&r1=391726&r2=391727
==============================================================================
--- team/dlee/ari-url-shuffle/res/res_stasis_http.c (original)
+++ team/dlee/ari-url-shuffle/res/res_stasis_http.c Thu Jun 13 15:25:46 2013
@@ -72,6 +72,7 @@
  */
 
 /*** MODULEINFO
+	<depend type="module">res_http_websocket</depend>
 	<support_level>core</support_level>
  ***/
 
@@ -211,7 +212,12 @@
 static struct stasis_rest_handlers *root_handler;
 
 /*! Pre-defined message for allocation failures. */
-static struct ast_json *alloc_failed_message;
+static struct ast_json *oom_json;
+
+struct ast_json *ari_oom_json(void)
+{
+	return oom_json;
+}
 
 int stasis_http_add_handler(struct stasis_rest_handlers *handler)
 {
@@ -325,7 +331,7 @@
 
 void stasis_http_response_alloc_failed(struct stasis_http_response *response)
 {
-	response->message = ast_json_ref(alloc_failed_message);
+	response->message = ast_json_ref(oom_json);
 	response->response_code = 500;
 	response->response_text = "Internal Server Error";
 }
@@ -894,6 +900,14 @@
 
 static int load_module(void)
 {
+	oom_json = ast_json_pack(
+		"{s: s}", "error", "AllocationFailed");
+
+	if (!oom_json) {
+		/* Ironic */
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
 	ast_mutex_init(&root_handler_lock);
 
 	root_handler = root_handler_create();
@@ -916,8 +930,6 @@
 		return AST_MODULE_LOAD_DECLINE;
 	}
 
-	alloc_failed_message = ast_json_pack(
-		"{s: s}", "message", "Allocation failed");
 
 	if (is_enabled()) {
 		ast_http_uri_link(&http_uri);
@@ -928,8 +940,8 @@
 
 static int unload_module(void)
 {
-	ast_json_unref(alloc_failed_message);
-	alloc_failed_message = NULL;
+	ast_json_unref(oom_json);
+	oom_json = NULL;
 
 	if (is_enabled()) {
 		ast_http_uri_unlink(&http_uri);
@@ -968,5 +980,6 @@
 	.load = load_module,
 	.unload = unload_module,
 	.reload = reload_module,
+	.nonoptreq = "res_stasis,res_http_websocket",
 	.load_pri = AST_MODPRI_APP_DEPEND,
 	);

Modified: team/dlee/ari-url-shuffle/res/res_stasis_http.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/res_stasis_http.exports.in?view=diff&rev=391727&r1=391726&r2=391727
==============================================================================
--- team/dlee/ari-url-shuffle/res/res_stasis_http.exports.in (original)
+++ team/dlee/ari-url-shuffle/res/res_stasis_http.exports.in Thu Jun 13 15:25:46 2013
@@ -1,6 +1,7 @@
 {
 	global:
 		LINKER_SYMBOL_PREFIXstasis_http_*;
+		LINKER_SYMBOL_PREFIXari_*;
 	local:
 		*;
 };

Modified: team/dlee/ari-url-shuffle/res/res_stasis_http_events.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/res_stasis_http_events.c?view=diff&rev=391727&r1=391726&r2=391727
==============================================================================
--- team/dlee/ari-url-shuffle/res/res_stasis_http_events.c (original)
+++ team/dlee/ari-url-shuffle/res/res_stasis_http_events.c Thu Jun 13 15:25:46 2013
@@ -60,6 +60,10 @@
 		{}
 	}
 	session = ari_websocket_session_create(ws_session);
+	if (!session) {
+		ast_log(LOG_ERROR, "Failed to create ARI session\n");
+		return;
+	}
 	ari_websocket_event_websocket(session, headers, &args);
 }
 

Modified: team/dlee/ari-url-shuffle/res/res_stasis_websocket.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/res_stasis_websocket.c?view=diff&rev=391727&r1=391726&r2=391727
==============================================================================
--- team/dlee/ari-url-shuffle/res/res_stasis_websocket.c (original)
+++ team/dlee/ari-url-shuffle/res/res_stasis_websocket.c Thu Jun 13 15:25:46 2013
@@ -74,7 +74,7 @@
 				   strlen(str));
 }
 
-struct stasis_ws_session_info {
+struct event_session {
 	struct ast_websocket *ws_session;
 	struct ao2_container *websocket_apps;
 };
@@ -82,7 +82,7 @@
 static void session_dtor(void *obj)
 {
 #ifdef AST_DEVMODE /* Avoid unused variable warning */
-	struct stasis_ws_session_info *session = obj;
+	struct event_session *session = obj;
 #endif
 
 	/* session_shutdown should have been called before */
@@ -90,10 +90,10 @@
 	ast_assert(session->websocket_apps == NULL);
 }
 
-static struct stasis_ws_session_info *session_create(
+static struct event_session *session_create(
 	struct ast_websocket *ws_session)
 {
-	RAII_VAR(struct stasis_ws_session_info *, session, NULL, ao2_cleanup);
+	RAII_VAR(struct event_session *, session, NULL, ao2_cleanup);
 
 	session = ao2_alloc(sizeof(*session), session_dtor);
 
@@ -118,7 +118,7 @@
  *
  * \param session Session info struct.
  */
-static void session_shutdown(struct stasis_ws_session_info *session)
+static void session_shutdown(struct event_session *session)
 {
         struct ao2_iterator i;
 	char *app;
@@ -142,7 +142,7 @@
 static void app_handler(void *data, const char *app_name,
 			struct ast_json *message)
 {
-	struct stasis_ws_session_info *session = data;
+	struct event_session *session = data;
 	int res;
 
 	res = ast_json_object_set(message, "application",
@@ -163,7 +163,7 @@
  * \param session Session info struct.
  * \param app_list Comma seperated list of app names to register.
  */
-static int session_register_apps(struct stasis_ws_session_info *session,
+static int session_register_apps(struct event_session *session,
 				 const char *app_list)
 {
 	RAII_VAR(char *, to_free, NULL, ast_free);
@@ -193,7 +193,7 @@
 			       struct ast_variable *parameters,
 			       struct ast_variable *headers)
 {
-	RAII_VAR(struct stasis_ws_session_info *, stasis_session, NULL, ao2_cleanup);
+	RAII_VAR(struct event_session *, stasis_session, NULL, ao2_cleanup);
 	struct ast_variable *param = NULL;
 	int res;
 

Added: team/dlee/ari-url-shuffle/res/stasis_http/ari_websockets.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/stasis_http/ari_websockets.c?view=auto&rev=391727
==============================================================================
--- team/dlee/ari-url-shuffle/res/stasis_http/ari_websockets.c (added)
+++ team/dlee/ari-url-shuffle/res/stasis_http/ari_websockets.c Thu Jun 13 15:25:46 2013
@@ -1,0 +1,124 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/astobj2.h"
+#include "asterisk/stasis_http.h"
+
+/*! \file
+ *
+ * \brief WebSocket support for RESTful API's.
+ * \author David M. Lee, II <dlee at digium.com>
+ */
+
+struct ari_websocket_session {
+	struct ast_websocket *ws_session;
+};
+
+static void websocket_session_dtor(void *obj)
+{
+	struct ari_websocket_session *session = obj;
+
+	ast_websocket_unref(session->ws_session);
+	session->ws_session = NULL;
+}
+
+struct ari_websocket_session *ari_websocket_session_create(
+	struct ast_websocket *ws_session)
+{
+	RAII_VAR(struct ari_websocket_session *, session, NULL, ao2_cleanup);
+
+	if (ws_session == NULL) {
+		return NULL;
+	}
+
+	if (ast_websocket_set_nonblock(ws_session) != 0) {
+		ast_log(LOG_ERROR,
+			"Stasis web socket failed to set nonblock; closing\n");
+		return NULL;
+	}
+
+	session = ao2_alloc(sizeof(*session), websocket_session_dtor);
+	if (!session) {
+		return NULL;
+	}
+
+	ao2_ref(ws_session, +1);
+	session->ws_session = ws_session;
+
+	ao2_ref(session, +1);
+	return session;
+}
+
+struct ast_json *ari_websocket_session_read(
+	struct ari_websocket_session *session)
+{
+	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
+
+	while (!message) {
+		int res;
+		char *payload;
+		uint64_t payload_len;
+		enum ast_websocket_opcode opcode;
+		int fragmented;
+
+		res = ast_wait_for_input(
+			ast_websocket_fd(session->ws_session), -1);
+
+		if (res <= 0) {
+			return NULL;
+		}
+
+		res = ast_websocket_read(session->ws_session, &payload,
+			&payload_len, &opcode, &fragmented);
+
+		if (res != 0) {
+			return NULL;
+		}
+
+		switch (opcode) {
+		case AST_WEBSOCKET_OPCODE_CLOSE:
+			return NULL;
+		case AST_WEBSOCKET_OPCODE_TEXT:
+			message = ast_json_load_buf(payload, payload_len, NULL);
+			break;
+		default:
+			/* Ignore all other message types */
+			break;
+		}
+	}
+
+	return ast_json_ref(message);
+}
+
+int ari_websocket_session_write(struct ari_websocket_session *session,
+	struct ast_json *message)
+{
+	RAII_VAR(char *, str, ast_json_dump_string(message), ast_free);
+
+	if (str == NULL) {
+		ast_log(LOG_ERROR, "Failed to encode JSON object\n");
+		return -1;
+	}
+
+	return ast_websocket_write(session->ws_session,
+		AST_WEBSOCKET_OPCODE_TEXT, str,	strlen(str));
+}

Propchange: team/dlee/ari-url-shuffle/res/stasis_http/ari_websockets.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/dlee/ari-url-shuffle/res/stasis_http/ari_websockets.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/dlee/ari-url-shuffle/res/stasis_http/ari_websockets.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/dlee/ari-url-shuffle/res/stasis_http/resource_events.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/stasis_http/resource_events.c?view=diff&rev=391727&r1=391726&r2=391727
==============================================================================
--- team/dlee/ari-url-shuffle/res/stasis_http/resource_events.c (original)
+++ team/dlee/ari-url-shuffle/res/stasis_http/resource_events.c Thu Jun 13 15:25:46 2013
@@ -27,11 +27,175 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include "asterisk/astobj2.h"
+#include "asterisk/stasis_app.h"
 #include "resource_events.h"
 
-void ari_websocket_event_websocket(struct ari_websocket_session *session,
+/*! Number of buckets for the Stasis application hash table. Remember to keep it
+ *  a prime number!
+ */
+#define APPS_NUM_BUCKETS 7
+
+/*! \brief A connection to the event WebSocket */
+struct event_session {
+	struct ari_websocket_session *ws_session;
+	struct ao2_container *websocket_apps;
+};
+
+/*!
+ * \brief Explicitly shutdown a session.
+ *
+ * An explicit shutdown is necessary, since stasis-app has a reference to this
+ * session. We also need to be sure to null out the \c ws_session field, since
+ * the websocket is about to go away.
+ *
+ * \param session Session info struct.
+ */
+static void session_shutdown(struct event_session *session)
+{
+        struct ao2_iterator i;
+	char *app;
+	SCOPED_AO2LOCK(lock, session);
+
+	i = ao2_iterator_init(session->websocket_apps, 0);
+	while ((app = ao2_iterator_next(&i))) {
+		stasis_app_unregister(app);
+		ao2_cleanup(app);
+	}
+	ao2_iterator_destroy(&i);
+	ao2_cleanup(session->websocket_apps);
+
+	session->websocket_apps = NULL;
+	session->ws_session = NULL;
+}
+
+static void session_dtor(void *obj)
+{
+#ifdef AST_DEVMODE /* Avoid unused variable warning */
+	struct event_session *session = obj;
+#endif
+
+	/* session_shutdown should have been called before */
+	ast_assert(session->ws_session == NULL);
+	ast_assert(session->websocket_apps == NULL);
+}
+
+static void session_cleanup(struct event_session *session)
+{
+	session_shutdown(session);
+	ao2_cleanup(session);
+}
+
+static struct event_session *session_create(
+	struct ari_websocket_session *ws_session)
+{
+	RAII_VAR(struct event_session *, session, NULL, ao2_cleanup);
+
+	session = ao2_alloc(sizeof(*session), session_dtor);
+
+	session->ws_session = ws_session;
+	session->websocket_apps =
+		ast_str_container_alloc(APPS_NUM_BUCKETS);
+
+	if (!session->websocket_apps) {
+		return NULL;
+	}
+
+	ao2_ref(session, +1);
+	return session;
+}
+
+/*!
+ * \brief Callback handler for Stasis application messages.
+ */
+static void app_handler(void *data, const char *app_name,
+			struct ast_json *message)
+{
+	struct event_session *session = data;
+	int res;
+
+	res = ast_json_object_set(message, "application",
+				  ast_json_string_create(app_name));
+	if(res != 0) {
+		return;
+	}
+
+	ao2_lock(session);
+	if (session->ws_session) {
+		ari_websocket_session_write(session->ws_session, message);
+	}
+	ao2_unlock(session);
+}
+
+/*!
+ * \brief Register for all of the apps given.
+ * \param session Session info struct.
+ * \param app_list Comma seperated list of app names to register.
+ */
+static int session_register_apps(struct event_session *session,
+				 const char *app_list)
+{
+	RAII_VAR(char *, to_free, NULL, ast_free);
+	char *apps, *app_name;
+	SCOPED_AO2LOCK(lock, session);
+
+	ast_assert(session->ws_session != NULL);
+	ast_assert(session->websocket_apps != NULL);
+
+	to_free = apps = ast_strdup(app_list);
+	if (!apps) {
+		ari_websocket_session_write(session->ws_session, ari_oom_json());
+		return -1;
+	}
+	while ((app_name = strsep(&apps, ","))) {
+		if (ast_str_container_add(session->websocket_apps, app_name)) {
+			ari_websocket_session_write(session->ws_session, ari_oom_json());
+			return -1;
+		}
+
+		stasis_app_register(app_name, app_handler, session);
+	}
+	return 0;
+}
+
+void ari_websocket_event_websocket(struct ari_websocket_session *ws_session,
 	struct ast_variable *headers,
 	struct ast_event_websocket_args *args)
 {
-	ast_log(LOG_ERROR, "TODO: ari_websocket_event_websocket\n");
-}
+	RAII_VAR(struct event_session *, session, NULL, session_cleanup);
+	struct ast_json *msg;
+	int res;
+
+	ast_debug(3, "/events WebSocket connection\n");
+
+	session = session_create(ws_session);
+	if (!session) {
+		ari_websocket_session_write(ws_session, ari_oom_json());
+		return;
+	}
+
+	res = session_register_apps(session, args->app);
+	if (res != 0) {
+		ari_websocket_session_write(ws_session, ari_oom_json());
+		return;
+	}
+
+	if (ao2_container_count(session->websocket_apps) == 0) {
+		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+
+		msg = ast_json_pack("{s: s, s: [s]}",
+				    "error", "MissingParams",
+				    "params", "app");
+		if (!msg) {
+			msg = ast_json_ref(ari_oom_json());
+		}
+
+		ari_websocket_session_write(session->ws_session, msg);
+		return;
+	}
+
+	/* We don't process any input, but we'll consume it waiting for EOF */
+	while ((msg = ari_websocket_session_read(ws_session))) {
+		ast_json_unref(msg);
+	}
+}

Modified: team/dlee/ari-url-shuffle/rest-api-templates/res_stasis_http_resource.c.mustache
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/rest-api-templates/res_stasis_http_resource.c.mustache?view=diff&rev=391727&r1=391726&r2=391727
==============================================================================
--- team/dlee/ari-url-shuffle/rest-api-templates/res_stasis_http_resource.c.mustache (original)
+++ team/dlee/ari-url-shuffle/rest-api-templates/res_stasis_http_resource.c.mustache Thu Jun 13 15:25:46 2013
@@ -81,6 +81,10 @@
 {{/has_path_parameters}}
 {{> param_parsing}}
 	session = ari_websocket_session_create(ws_session);
+	if (!session) {
+		ast_log(LOG_ERROR, "Failed to create ARI session\n");
+		return;
+	}
 	ari_websocket_{{c_nickname}}(session, headers, &args);
 }
 {{/is_websocket}}




More information about the asterisk-commits mailing list