[svn-commits] dlee: branch dlee/stasis-http r380914 - in /team/dlee/stasis-http: apps/ incl...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Feb 5 13:55:05 CST 2013


Author: dlee
Date: Tue Feb  5 13:55:02 2013
New Revision: 380914

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=380914
Log:
It's a hack, but continue to dialplan works.

Added:
    team/dlee/stasis-http/apps/app_stasis.c.exports.in   (with props)
Modified:
    team/dlee/stasis-http/apps/app_stasis.c
    team/dlee/stasis-http/include/asterisk/stasis_app.h
    team/dlee/stasis-http/res/res_stasis_core.c
    team/dlee/stasis-http/res/stasis_http_channels.c

Modified: team/dlee/stasis-http/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/apps/app_stasis.c?view=diff&rev=380914&r1=380913&r2=380914
==============================================================================
--- team/dlee/stasis-http/apps/app_stasis.c (original)
+++ team/dlee/stasis-http/apps/app_stasis.c Tue Feb  5 13:55:02 2013
@@ -32,7 +32,9 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/app.h"
+#include "asterisk/astobj2.h"
 #include "asterisk/channel.h"
+#include "asterisk/hashtab.h"
 #include "asterisk/module.h"
 #include "asterisk/stasis_app.h"
 
@@ -56,7 +58,80 @@
 /*! Maximum number of arguments for the Stasis dialplan application */
 #define MAX_ARGS 128
 
+/*! Must be prime */
+#define HANDLERS_NUM_BUCKETS 127
+
 static const char *stasis = "Stasis";
+
+struct ast_app_stasis_handler {
+	char *channel_uniqueid;
+	int exit:1;
+};
+
+/*! Stasis application container */
+static struct ao2_container *__app_handlers;
+
+static struct ao2_container *app_handlers(void)
+{
+	ao2_ref(__app_handlers, 1);
+	return __app_handlers;
+}
+
+/*! Hash function for stasis_app */
+static int hash_handler(const void *obj, const int flags)
+{
+	const struct ast_app_stasis_handler *app = obj;
+	const char *channel_uniqueid = flags & OBJ_KEY ? obj : app->channel_uniqueid;
+
+	return ast_hashtab_hash_string(channel_uniqueid);
+}
+
+/*! Comparison function for stasis_app */
+static int compare_handler(void *lhs, void *rhs, int flags)
+{
+	const struct ast_app_stasis_handler *lhs_app = lhs;
+	const struct ast_app_stasis_handler *rhs_app = rhs;
+	const char *rhs_channel_uniqueid = flags & OBJ_KEY ? rhs : rhs_app->channel_uniqueid;
+
+	if (strcmp(lhs_app->channel_uniqueid, rhs_channel_uniqueid) == 0) {
+		return CMP_MATCH | CMP_STOP;
+	} else {
+		return 0;
+	}
+}
+
+static void handler_dtor(void *obj)
+{
+	struct ast_app_stasis_handler *app = obj;
+	ast_free(app->channel_uniqueid);
+}
+
+static int app_should_exit(struct ast_app_stasis_handler *handler)
+{
+	int r;
+	ao2_lock(handler);
+	r = handler->exit;
+	ao2_unlock(handler);
+	return r;
+}
+
+struct ast_app_stasis_handler *ast_app_stasis_find_by_channel(struct ast_channel *chan)
+{
+	RAII_VAR(struct ao2_container *, handlers, app_handlers(), ao2_cleanup);
+	return chan == NULL ? NULL : ao2_find(handlers, ast_channel_uniqueid(chan), OBJ_KEY);
+}
+
+void ast_app_stasis_continue(struct ast_app_stasis_handler *handler)
+{
+	ao2_lock(handler);
+	handler->exit = 1;
+	ao2_unlock(handler);
+}
+
+void ast_app_stasis_handler_unref(struct ast_app_stasis_handler *handler)
+{
+	ao2_cleanup(handler);
+}
 
 static struct ast_json *json_name_number(const char *name, const char *number)
 {
@@ -67,6 +142,7 @@
 
 static struct ast_json *json_timeval(struct timeval *tv)
 {
+	/* XXX ISO 8601 would be more ideomatic json */
 	return ast_json_pack("{s: i, s: i}",
 			     "sec", tv->tv_sec,
 			     "usec", tv->tv_usec);
@@ -125,6 +201,7 @@
 {
 	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ast_channel_snapshot_dtor);
+
 	struct ast_json *json_args;
 	int i;
 
@@ -187,6 +264,8 @@
 /*! /brief Stasis dialplan application callback */
 static int stasis_exec(struct ast_channel *chan, const char *data)
 {
+	RAII_VAR(struct ao2_container *, handlers, app_handlers(), ao2_cleanup);
+	RAII_VAR(struct ast_app_stasis_handler *, handler, NULL, ao2_cleanup);
 	int res = 0;
 	char *parse = NULL;
 	int hungup = 0;
@@ -208,13 +287,17 @@
 		return -1;
 	}
 
+	handler = ao2_alloc(sizeof(*handler), handler_dtor);
+	handler->channel_uniqueid = ast_strdup(ast_channel_uniqueid(chan));
+	ao2_link(handlers, handler);
+
 	res = send_start_msg(args.app_name, chan, args.argc - 1, args.app_argv);
 	if (res != 0) {
 		ast_log(LOG_ERROR, "Error sending start message to %s\n", args.app_name);
 		return res;
 	}
 
-	while (!hungup && ast_waitfor(chan, -1) > -1) {
+	while (!hungup && !app_should_exit(handler) && ast_waitfor(chan, -1) > -1) {
 		RAII_VAR(struct ast_frame *, f, ast_read(chan), ast_frame_free_cached);
 		if (!f) {
 			ast_debug(3, "%s: No more frames. Must be done, I guess.\n", ast_channel_uniqueid(chan));
@@ -246,6 +329,10 @@
 static int load_module(void)
 {
 	int r = 0;
+	__app_handlers = ao2_container_alloc(HANDLERS_NUM_BUCKETS, hash_handler, compare_handler);
+	if (__app_handlers == NULL) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
 	r |= ast_register_application_xml(stasis, stasis_exec);
 	return r;
 }
@@ -253,11 +340,13 @@
 static int unload_module(void)
 {
 	int r = 0;
+	ao2_ref(__app_handlers, -1);
+	__app_handlers = NULL;
 	r |= ast_unregister_application(stasis);
 	return r;
 }
 
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Stasis dialplan application",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Stasis dialplan application",
 		.load = load_module,
 		.unload = unload_module,
 		.load_pri = AST_MODPRI_DEFAULT,

Added: team/dlee/stasis-http/apps/app_stasis.c.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/apps/app_stasis.c.exports.in?view=auto&rev=380914
==============================================================================
--- team/dlee/stasis-http/apps/app_stasis.c.exports.in (added)
+++ team/dlee/stasis-http/apps/app_stasis.c.exports.in Tue Feb  5 13:55:02 2013
@@ -1,0 +1,6 @@
+{
+	global:
+		LINKER_SYMBOL_PREFIXast_app_stasis_find_by_channel;
+	local:
+		*;
+};

Propchange: team/dlee/stasis-http/apps/app_stasis.c.exports.in
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/dlee/stasis-http/apps/app_stasis.c.exports.in
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: team/dlee/stasis-http/apps/app_stasis.c.exports.in
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/dlee/stasis-http/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/stasis_app.h?view=diff&rev=380914&r1=380913&r2=380914
==============================================================================
--- team/dlee/stasis-http/include/asterisk/stasis_app.h (original)
+++ team/dlee/stasis-http/include/asterisk/stasis_app.h Tue Feb  5 13:55:02 2013
@@ -30,6 +30,27 @@
  */
 
 #include "asterisk/json.h"
+
+/*! \brief Handler for controlling app_stasis */
+struct ast_app_stasis_handler;
+
+/*!
+ * \brief Returns the handler for the given channel
+ * \param chan Channel to handle.
+ * \return NULL channel not in Stasis application
+ * \return Pointer to app_stasis handler.
+ */
+struct ast_app_stasis_handler *ast_app_stasis_find_by_channel(struct ast_channel *chan);
+
+/*!
+ * \brief Exit app_stasis and continue execution in the dialplan
+ * \param handler Handler for app_stasis
+ * \return 0 on Success
+ * \return Non-zero on error
+ */
+void ast_app_stasis_continue(struct ast_app_stasis_handler *handler);
+
+void ast_app_stasis_handler_unref(struct ast_app_stasis_handler *handler);
 
 /*!
  * \brief Send a message to the given Stasis application

Modified: team/dlee/stasis-http/res/res_stasis_core.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/res/res_stasis_core.c?view=diff&rev=380914&r1=380913&r2=380914
==============================================================================
--- team/dlee/stasis-http/res/res_stasis_core.c (original)
+++ team/dlee/stasis-http/res/res_stasis_core.c Tue Feb  5 13:55:02 2013
@@ -58,7 +58,6 @@
 	ao2_ref(__stasis_apps, 1);
 	return __stasis_apps;
 }
-
 
 /*! Hash function for stasis_app */
 static int hash_app(const void *obj, const int flags)
@@ -122,7 +121,7 @@
 		msg = ast_json_pack("{s: s}", "event", "application-replaced");
 		app->handler(app->data, app_name, msg);
 	} else {
-		app = ao2_alloc(sizeof(*app), app_dtor);
+		app = ao2_alloc_options(sizeof(*app), app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX);
 		app->name = ast_strdup(app_name);
 		if (app) {
 			ao2_link(apps, app);

Modified: team/dlee/stasis-http/res/stasis_http_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/res/stasis_http_channels.c?view=diff&rev=380914&r1=380913&r2=380914
==============================================================================
--- team/dlee/stasis-http/res/stasis_http_channels.c (original)
+++ team/dlee/stasis-http/res/stasis_http_channels.c Tue Feb  5 13:55:02 2013
@@ -31,7 +31,9 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include "asterisk/channel.h"
 #include "asterisk/stasis_http_channels.h"
+#include "asterisk/stasis_app.h"
 
 void stasis_http_dial(struct ast_variable *headers, struct ast_dial_args *args, struct stasis_http_response *response)
 {
@@ -39,8 +41,33 @@
 }
 void stasis_http_continue_in_dialplan(struct ast_variable *headers, struct ast_continue_in_dialplan_args *args, struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_continue_in_dialplan\n");
+	RAII_VAR(struct ast_channel *, chan, ast_channel_get_by_name(args->channel_id), ao2_cleanup);
+	RAII_VAR(struct ast_app_stasis_handler *, handler, ast_app_stasis_find_by_channel(chan), ast_app_stasis_handler_unref);
+
+	ast_assert(response != NULL);
+
+	if (chan == NULL) {
+		response->response_code = 404;
+		response->response_text = "Not Found";
+		response->message = ast_json_pack("{s: s}", "message", "Channel not found");
+		return;
+	}
+
+	if (handler == NULL) {
+		response->response_code = 409;
+		response->response_text = "Conflict";
+		response->message = ast_json_pack("{s: s}", "message", "Channel not in Stasis application");
+		return;
+	}
+
+	ast_app_stasis_continue(handler);
+
+	response->response_code = 200;
+	response->response_text = "OK";
+	response->message = ast_json_pack("{s: s}", "message", "Returned to dialplan");
+	ast_assert(response->message != NULL);
 }
+
 void stasis_http_reject_channel(struct ast_variable *headers, struct ast_reject_channel_args *args, struct stasis_http_response *response)
 {
 	ast_log(LOG_ERROR, "TODO: stasis_http_reject_channel\n");




More information about the svn-commits mailing list