[asterisk-commits] dlee: branch dlee/playback r388249 - in /team/dlee/playback/res: ./ stasis/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu May 9 23:07:17 CDT 2013


Author: dlee
Date: Thu May  9 23:07:15 2013
New Revision: 388249

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=388249
Log:
Shuffle code around to ease the merge pain

Modified:
    team/dlee/playback/res/res_stasis.c
    team/dlee/playback/res/res_stasis_answer.c
    team/dlee/playback/res/stasis/app.c
    team/dlee/playback/res/stasis/command.c
    team/dlee/playback/res/stasis/control.c

Modified: team/dlee/playback/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback/res/res_stasis.c?view=diff&rev=388249&r1=388248&r2=388249
==============================================================================
--- team/dlee/playback/res/res_stasis.c (original)
+++ team/dlee/playback/res/res_stasis.c Thu May  9 23:07:15 2013
@@ -60,9 +60,10 @@
 #include "asterisk/stasis_app_impl.h"
 #include "asterisk/stasis_channels.h"
 #include "asterisk/strings.h"
-
 #include "stasis/app.h"
 #include "stasis/control.h"
+
+#include "stasis/command.h"
 
 /*! Time to wait for a frame in the application */
 #define MAX_WAIT_MS 200
@@ -83,6 +84,193 @@
 
 struct ao2_container *app_controls;
 
+struct app {
+	/*! Callback function for this application. */
+	stasis_app_cb handler;
+	/*! Opaque data to hand to callback function. */
+	void *data;
+	/*! Name of the Stasis application */
+	char name[];
+};
+
+static void app_dtor(void *obj)
+{
+	struct app *app = obj;
+
+	ao2_cleanup(app->data);
+	app->data = NULL;
+}
+
+struct app *app_create(const char *name, stasis_app_cb handler, void *data)
+{
+	struct app *app;
+	size_t size;
+
+	ast_assert(name != NULL);
+	ast_assert(handler != NULL);
+
+	size = sizeof(*app) + strlen(name) + 1;
+	app = ao2_alloc_options(size, app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX);
+
+	if (!app) {
+		return NULL;
+	}
+
+	strncpy(app->name, name, size - sizeof(*app));
+	app->handler = handler;
+	ao2_ref(data, +1);
+	app->data = data;
+
+	return app;
+}
+
+/*! AO2 hash function for \ref app */
+static int app_hash(const void *obj, const int flags)
+{
+	const struct app *app = obj;
+	const char *name = flags & OBJ_KEY ? obj : app_name(app);
+
+	return ast_str_hash(name);
+}
+
+/*! AO2 comparison function for \ref app */
+static int app_compare(void *lhs, void *rhs, int flags)
+{
+	const struct app *lhs_app = lhs;
+	const struct app *rhs_app = rhs;
+	const char *lhs_name = app_name(lhs_app);
+	const char *rhs_name = flags & OBJ_KEY ? rhs : app_name(rhs_app);
+
+	if (strcmp(lhs_name, rhs_name) == 0) {
+		return CMP_MATCH | CMP_STOP;
+	} else {
+		return 0;
+	}
+}
+
+/*!
+ * \brief Send a message to the given application.
+ * \param app App to send the message to.
+ * \param message Message to send.
+ */
+void app_send(struct app *app, struct ast_json *message)
+{
+	app->handler(app->data, app->name, message);
+}
+
+struct stasis_app_command {
+	ast_mutex_t lock;
+	ast_cond_t condition;
+	stasis_app_command_cb callback;
+	void *data;
+	void *retval;
+	int is_done:1;
+};
+
+static void command_dtor(void *obj)
+{
+	struct stasis_app_command *command = obj;
+	ast_mutex_destroy(&command->lock);
+	ast_cond_destroy(&command->condition);
+}
+
+struct stasis_app_command *command_create(
+	stasis_app_command_cb callback, void *data)
+{
+	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
+
+	command = ao2_alloc(sizeof(*command), command_dtor);
+	if (!command) {
+		return NULL;
+	}
+
+	ast_mutex_init(&command->lock);
+	ast_cond_init(&command->condition, 0);
+	command->callback = callback;
+	command->data = data;
+
+	ao2_ref(command, +1);
+	return command;
+}
+
+static void command_complete(struct stasis_app_command *command, void *retval)
+{
+	SCOPED_MUTEX(lock, &command->lock);
+
+	command->is_done = 1;
+	command->retval = retval;
+	ast_cond_signal(&command->condition);
+}
+
+void *command_join(struct stasis_app_command *command)
+{
+	SCOPED_MUTEX(lock, &command->lock);
+	while (!command->is_done) {
+		ast_cond_wait(&command->condition, &command->lock);
+	}
+
+	return command->retval;
+}
+
+void command_invoke(struct stasis_app_command *command,
+	struct stasis_app_control *control, struct ast_channel *chan)
+{
+	void *retval = command->callback(control, chan, command->data);
+	command_complete(command, retval);
+}
+
+struct stasis_app_control {
+	/*! Queue of commands to dispatch on the channel */
+	struct ao2_container *command_queue;
+	/*!
+	 * When set, /c app_stasis should exit and continue in the dialplan.
+	 */
+	int is_done:1;
+	/*!
+	 * The associated channel.
+	 * Be very careful with the threading associated w/ manipulating
+	 * the channel.
+	 */
+	struct ast_channel *channel;
+};
+
+struct stasis_app_control *control_create(struct ast_channel *channel)
+{
+	struct stasis_app_control *control;
+
+	control = ao2_alloc(sizeof(*control), NULL);
+	if (!control) {
+		return NULL;
+	}
+
+	control->command_queue = ao2_container_alloc_list(0, 0, NULL, NULL);
+
+	control->channel = channel;
+
+	return control;
+}
+
+static struct stasis_app_command *exec_command(
+	struct stasis_app_control *control, stasis_app_command_cb command_fn,
+	void *data)
+{
+	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
+
+	command = command_create(command_fn, data);
+
+	if (!command) {
+		return NULL;
+	}
+
+	ao2_lock(control);
+	ao2_ref(command, +1);
+	ao2_link(control->command_queue, command);
+	ao2_unlock(control);
+
+	ao2_ref(command, +1);
+	return command;
+}
+
 /*! AO2 hash function for \ref stasis_app_control */
 static int control_hash(const void *obj, const int flags)
 {
@@ -109,30 +297,6 @@
 	}
 }
 
-/*! AO2 hash function for \ref app */
-static int app_hash(const void *obj, const int flags)
-{
-	const struct app *app = obj;
-	const char *name = flags & OBJ_KEY ? obj : app_name(app);
-
-	return ast_str_hash(name);
-}
-
-/*! AO2 comparison function for \ref app */
-static int app_compare(void *lhs, void *rhs, int flags)
-{
-	const struct app *lhs_app = lhs;
-	const struct app *rhs_app = rhs;
-	const char *lhs_name = app_name(lhs_app);
-	const char *rhs_name = flags & OBJ_KEY ? rhs : app_name(rhs_app);
-
-	if (strcmp(lhs_name, rhs_name) == 0) {
-		return CMP_MATCH | CMP_STOP;
-	} else {
-		return 0;
-	}
-}
-
 struct stasis_app_control *stasis_app_control_find_by_channel(
 	const struct ast_channel *chan)
 {
@@ -150,27 +314,217 @@
 	return ao2_find(app_controls, channel_id, OBJ_KEY);
 }
 
-struct ast_channel_snapshot *stasis_app_control_get_snapshot(
-	const struct stasis_app_control *control)
-{
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
-	struct stasis_caching_topic *caching_topic;
-	struct ast_channel_snapshot *snapshot;
-
-	caching_topic = ast_channel_topic_all_cached();
-	ast_assert(caching_topic != NULL);
-
-	msg = stasis_cache_get(caching_topic, ast_channel_snapshot_type(),
+int control_is_done(struct stasis_app_control *control)
+{
+	/* Called from stasis_app_exec thread; no lock needed */
+	return control->is_done;
+}
+
+static void *app_control_continue(struct stasis_app_control *control,
+	struct ast_channel *chan, void *data)
+{
+	/* Called from stasis_app_exec thread; no lock needed */
+	control->is_done = 1;
+	return NULL;
+}
+
+void stasis_app_control_continue(struct stasis_app_control *control)
+{
+	stasis_app_send_command_async(control, app_control_continue, NULL);
+}
+
+static int OK = 0;
+static int FAIL = -1;
+
+static void *app_control_answer(struct stasis_app_control *control,
+	struct ast_channel *chan, void *data)
+{
+	const int delay = 0;
+	const int cdr_answer = 1;
+	ast_debug(3, "%s: Answering",
 		stasis_app_control_get_channel_id(control));
+	return __ast_answer(chan, delay, cdr_answer) == 0 ? &OK : &FAIL;
+}
+
+int stasis_app_control_answer(struct stasis_app_control *control)
+{
+	int *retval;
+
+	ast_debug(3, "%s: Sending answer command\n",
+		stasis_app_control_get_channel_id(control));
+
+	retval = stasis_app_send_command(control, app_control_answer, NULL);
+
+	if (retval == NULL || *retval != 0) {
+		ast_log(LOG_WARNING, "%s: Failed to answer channel",
+			stasis_app_control_get_channel_id(control));
+		return -1;
+	}
+
+	return 0;
+}
+
+static struct ast_json *event_create(
+	const char *event_name,
+	const struct ast_channel_snapshot *snapshot,
+	const struct ast_json *extra_info)
+{
+	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
+
+	if (extra_info) {
+		event = ast_json_deep_copy(extra_info);
+	} else {
+		event = ast_json_object_create();
+	}
+
+	if (snapshot) {
+		int ret;
+
+		/* Mustn't already have a channel field */
+		ast_assert(ast_json_object_get(event, "channel") == NULL);
+
+		ret = ast_json_object_set(
+			event,
+			"channel", ast_channel_snapshot_to_json(snapshot));
+		if (ret != 0) {
+			return NULL;
+		}
+	}
+
+	message = ast_json_pack("{s: o}", event_name, ast_json_ref(event));
+
+	return ast_json_ref(message);
+}
+
+int app_send_start_msg(struct app *app, struct ast_channel *chan,
+	int argc, char *argv[])
+{
+	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+
+	struct ast_json *json_args;
+	int i;
+
+	ast_assert(chan != NULL);
+
+	/* Set channel info */
+	snapshot = ast_channel_snapshot_create(chan);
+	if (!snapshot) {
+		return -1;
+	}
+
+	msg = ast_json_pack("{s: {s: [], s: o}}",
+			    "stasis-start",
+			    "args",
+			    "channel", ast_channel_snapshot_to_json(snapshot));
+
 	if (!msg) {
+		return -1;
+	}
+
+	/* Append arguments to args array */
+	json_args = ast_json_object_get(
+		ast_json_object_get(msg, "stasis-start"),
+		"args");
+	ast_assert(json_args != NULL);
+	for (i = 0; i < argc; ++i) {
+		int r = ast_json_array_append(json_args,
+					      ast_json_string_create(argv[i]));
+		if (r != 0) {
+			ast_log(LOG_ERROR, "Error appending start message\n");
+			return -1;
+		}
+	}
+
+	app_send(app, msg);
+	return 0;
+}
+
+int app_send_end_msg(struct app *app, struct ast_channel *chan)
+{
+	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+
+	ast_assert(chan != NULL);
+
+	/* Set channel info */
+	snapshot = ast_channel_snapshot_create(chan);
+	if (snapshot == NULL) {
+		return -1;
+	}
+	msg = event_create("stasis-end", snapshot, NULL);
+	if (!msg) {
+		return -1;
+	}
+
+	app_send(app, msg);
+	return 0;
+}
+
+static void dtmf_handler(struct app *app, struct ast_channel_blob *obj)
+{
+	RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+	const char *direction;
+
+	/* To simplify events, we'll only generate on receive */
+	direction = ast_json_string_get(
+		ast_json_object_get(obj->blob, "direction"));
+
+	if (strcmp("Received", direction) != 0) {
+		return;
+	}
+
+	extra = ast_json_pack(
+		"{s: o}",
+		"digit", ast_json_ref(ast_json_object_get(obj->blob, "digit")));
+	if (!extra) {
+		return;
+	}
+
+	msg = event_create("dtmf-received", obj->snapshot, extra);
+	if (!msg) {
+		return;
+	}
+
+	app_send(app, msg);
+}
+
+static void sub_handler(void *data, struct stasis_subscription *sub,
+			struct stasis_topic *topic,
+			struct stasis_message *message)
+{
+	struct app *app = data;
+
+	if (stasis_subscription_final_message(sub, message)) {
+		ao2_cleanup(data);
+		return;
+	}
+
+	if (ast_channel_snapshot_type() == stasis_message_type(message)) {
+		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+		struct ast_channel_snapshot *snapshot =
+			stasis_message_data(message);
+
+		msg = event_create("channel-state-change", snapshot, NULL);
+		if (!msg) {
+			return;
+		}
+		app_send(app, msg);
+	} else if (ast_channel_dtmf_end_type() == stasis_message_type(message)) {
+		struct ast_channel_blob *blob = stasis_message_data(message);
+		dtmf_handler(app, blob);
+	}
+}
+
+struct stasis_subscription *app_subscribe(struct app *app,
+	struct stasis_topic *topic)
+{
+	if (app == NULL || topic == NULL) {
 		return NULL;
 	}
-
-	snapshot = stasis_message_data(msg);
-	ast_assert(snapshot != NULL);
-
-	ao2_ref(snapshot, +1);
-	return snapshot;
+	return stasis_subscribe(topic, sub_handler, app);
 }
 
 /*!
@@ -375,3 +729,118 @@
 		"Stasis application support",
 		.load = load_module,
 		.unload = unload_module);
+
+void app_update(struct app *app, stasis_app_cb handler, void *data)
+{
+	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+	SCOPED_AO2LOCK(lock, app);
+
+	msg = event_create("application-replaced", NULL, NULL);
+	app->handler(app->data, app->name, msg);
+
+	app->handler = handler;
+	ao2_cleanup(app->data);
+	ao2_ref(data, +1);
+	app->data = data;
+}
+
+const char *app_name(const struct app *app)
+{
+	return app->name;
+}
+
+struct ast_channel_snapshot *stasis_app_control_get_snapshot(
+	const struct stasis_app_control *control)
+{
+	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct stasis_caching_topic *caching_topic;
+	struct ast_channel_snapshot *snapshot;
+
+	caching_topic = ast_channel_topic_all_cached();
+	ast_assert(caching_topic != NULL);
+
+	msg = stasis_cache_get(caching_topic, ast_channel_snapshot_type(),
+		stasis_app_control_get_channel_id(control));
+	if (!msg) {
+		return NULL;
+	}
+
+	snapshot = stasis_message_data(msg);
+	ast_assert(snapshot != NULL);
+
+	ao2_ref(snapshot, +1);
+	return snapshot;
+}
+
+void *stasis_app_send_command(struct stasis_app_control *control,
+	stasis_app_command_cb command_fn, void *data)
+{
+	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
+
+	if (control == NULL) {
+		return NULL;
+	}
+
+	command = exec_command(control, command_fn, data);
+	if (!command) {
+		return NULL;
+	}
+
+	return command_join(command);
+}
+
+int stasis_app_send_command_async(struct stasis_app_control *control,
+	stasis_app_command_cb command_fn, void *data)
+{
+	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
+
+	if (control == NULL) {
+		return -1;
+	}
+
+	command = exec_command(control, command_fn, data);
+	if (!command) {
+		return -1;
+	}
+
+	return 0;
+}
+
+const char *stasis_app_control_get_channel_id(
+	const struct stasis_app_control *control)
+{
+	return ast_channel_uniqueid(control->channel);
+}
+
+void stasis_app_control_publish(
+	struct stasis_app_control *control, struct stasis_message *message)
+{
+	if (!control || !control->channel || !message) {
+		return;
+	}
+	stasis_publish(ast_channel_topic(control->channel), message);
+}
+
+int control_dispatch_all(struct stasis_app_control *control,
+	struct ast_channel *chan)
+{
+	int count = 0;
+	struct ao2_iterator i;
+	void *obj;
+
+	SCOPED_AO2LOCK(lock, control);
+
+	ast_assert(control->channel == chan);
+
+	i = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK);
+
+	while ((obj = ao2_iterator_next(&i))) {
+		RAII_VAR(struct stasis_app_command *, command, obj, ao2_cleanup);
+		command_invoke(command, control, chan);
+		++count;
+	}
+
+	ao2_iterator_destroy(&i);
+	return count;
+}
+

Modified: team/dlee/playback/res/res_stasis_answer.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback/res/res_stasis_answer.c?view=diff&rev=388249&r1=388248&r2=388249
==============================================================================
--- team/dlee/playback/res/res_stasis_answer.c (original)
+++ team/dlee/playback/res/res_stasis_answer.c Thu May  9 23:07:15 2013
@@ -35,36 +35,6 @@
 #include "asterisk/module.h"
 #include "asterisk/stasis_app_impl.h"
 
-static int OK = 0;
-static int FAIL = -1;
-
-static void *app_control_answer(struct stasis_app_control *control,
-	struct ast_channel *chan, void *data)
-{
-	const int delay = 0;
-	const int cdr_answer = 1;
-	ast_debug(3, "%s: Answering",
-		stasis_app_control_get_channel_id(control));
-	return __ast_answer(chan, delay, cdr_answer) == 0 ? &OK : &FAIL;
-}
-
-int stasis_app_control_answer(struct stasis_app_control *control)
-{
-	int *retval;
-
-	ast_debug(3, "%s: Sending answer command\n",
-		stasis_app_control_get_channel_id(control));
-
-	retval = stasis_app_send_command(control, app_control_answer, NULL);
-
-	if (retval == NULL || *retval != 0) {
-		ast_log(LOG_WARNING, "%s: Failed to answer channel",
-			stasis_app_control_get_channel_id(control));
-		return -1;
-	}
-
-	return 0;
-}
 
 static int load_module(void)
 {

Modified: team/dlee/playback/res/stasis/app.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback/res/stasis/app.c?view=diff&rev=388249&r1=388248&r2=388249
==============================================================================
--- team/dlee/playback/res/stasis/app.c (original)
+++ team/dlee/playback/res/stasis/app.c Thu May  9 23:07:15 2013
@@ -32,236 +32,5 @@
 #include "asterisk/stasis_app.h"
 #include "asterisk/stasis_channels.h"
 
-struct app {
-	/*! Callback function for this application. */
-	stasis_app_cb handler;
-	/*! Opaque data to hand to callback function. */
-	void *data;
-	/*! Name of the Stasis application */
-	char name[];
-};
-
-static void app_dtor(void *obj)
-{
-	struct app *app = obj;
-
-	ao2_cleanup(app->data);
-	app->data = NULL;
-}
-
-struct app *app_create(const char *name, stasis_app_cb handler, void *data)
-{
-	struct app *app;
-	size_t size;
-
-	ast_assert(name != NULL);
-	ast_assert(handler != NULL);
-
-	size = sizeof(*app) + strlen(name) + 1;
-	app = ao2_alloc_options(size, app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX);
-
-	if (!app) {
-		return NULL;
-	}
-
-	strncpy(app->name, name, size - sizeof(*app));
-	app->handler = handler;
-	ao2_ref(data, +1);
-	app->data = data;
-
-	return app;
-}
-
-static struct ast_json *event_create(
-	const char *event_name,
-	const struct ast_channel_snapshot *snapshot,
-	const struct ast_json *extra_info)
-{
-	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
-	RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
-
-	if (extra_info) {
-		event = ast_json_deep_copy(extra_info);
-	} else {
-		event = ast_json_object_create();
-	}
-
-	if (snapshot) {
-		int ret;
-
-		/* Mustn't already have a channel field */
-		ast_assert(ast_json_object_get(event, "channel") == NULL);
-
-		ret = ast_json_object_set(
-			event,
-			"channel", ast_channel_snapshot_to_json(snapshot));
-		if (ret != 0) {
-			return NULL;
-		}
-	}
-
-	message = ast_json_pack("{s: o}", event_name, ast_json_ref(event));
-
-	return ast_json_ref(message);
-}
-
-void app_update(struct app *app, stasis_app_cb handler, void *data)
-{
-	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-	SCOPED_AO2LOCK(lock, app);
-
-	msg = event_create("application-replaced", NULL, NULL);
-	app->handler(app->data, app->name, msg);
-
-	app->handler = handler;
-	ao2_cleanup(app->data);
-	ao2_ref(data, +1);
-	app->data = data;
-}
-
-const char *app_name(const struct app *app)
-{
-	return app->name;
-}
-
-static void dtmf_handler(struct app *app, struct ast_channel_blob *obj)
-{
-	RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
-	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-	const char *direction;
-
-	/* To simplify events, we'll only generate on receive */
-	direction = ast_json_string_get(
-		ast_json_object_get(obj->blob, "direction"));
-
-	if (strcmp("Received", direction) != 0) {
-		return;
-	}
-
-	extra = ast_json_pack(
-		"{s: o}",
-		"digit", ast_json_ref(ast_json_object_get(obj->blob, "digit")));
-	if (!extra) {
-		return;
-	}
-
-	msg = event_create("dtmf-received", obj->snapshot, extra);
-	if (!msg) {
-		return;
-	}
-
-	app_send(app, msg);
-}
-
-static void sub_handler(void *data, struct stasis_subscription *sub,
-			struct stasis_topic *topic,
-			struct stasis_message *message)
-{
-	struct app *app = data;
-
-	if (stasis_subscription_final_message(sub, message)) {
-		ao2_cleanup(data);
-		return;
-	}
-
-	if (ast_channel_snapshot_type() == stasis_message_type(message)) {
-		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-		struct ast_channel_snapshot *snapshot =
-			stasis_message_data(message);
-
-		msg = event_create("channel-state-change", snapshot, NULL);
-		if (!msg) {
-			return;
-		}
-		app_send(app, msg);
-	} else if (ast_channel_dtmf_end_type() == stasis_message_type(message)) {
-		struct ast_channel_blob *blob = stasis_message_data(message);
-		dtmf_handler(app, blob);
-	}
-}
-
-struct stasis_subscription *app_subscribe(struct app *app,
-	struct stasis_topic *topic)
-{
-	if (app == NULL || topic == NULL) {
-		return NULL;
-	}
-	return stasis_subscribe(topic, sub_handler, app);
-}
 
 
-/*!
- * \brief Send a message to the given application.
- * \param app App to send the message to.
- * \param message Message to send.
- */
-void app_send(struct app *app, struct ast_json *message)
-{
-	app->handler(app->data, app->name, message);
-}
-
-
-int app_send_start_msg(struct app *app, struct ast_channel *chan,
-	int argc, char *argv[])
-{
-	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
-
-	struct ast_json *json_args;
-	int i;
-
-	ast_assert(chan != NULL);
-
-	/* Set channel info */
-	snapshot = ast_channel_snapshot_create(chan);
-	if (!snapshot) {
-		return -1;
-	}
-
-	msg = ast_json_pack("{s: {s: [], s: o}}",
-			    "stasis-start",
-			    "args",
-			    "channel", ast_channel_snapshot_to_json(snapshot));
-
-	if (!msg) {
-		return -1;
-	}
-
-	/* Append arguments to args array */
-	json_args = ast_json_object_get(
-		ast_json_object_get(msg, "stasis-start"),
-		"args");
-	ast_assert(json_args != NULL);
-	for (i = 0; i < argc; ++i) {
-		int r = ast_json_array_append(json_args,
-					      ast_json_string_create(argv[i]));
-		if (r != 0) {
-			ast_log(LOG_ERROR, "Error appending start message\n");
-			return -1;
-		}
-	}
-
-	app_send(app, msg);
-	return 0;
-}
-
-int app_send_end_msg(struct app *app, struct ast_channel *chan)
-{
-	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
-
-	ast_assert(chan != NULL);
-
-	/* Set channel info */
-	snapshot = ast_channel_snapshot_create(chan);
-	if (snapshot == NULL) {
-		return -1;
-	}
-	msg = event_create("stasis-end", snapshot, NULL);
-	if (!msg) {
-		return -1;
-	}
-
-	app_send(app, msg);
-	return 0;
-}

Modified: team/dlee/playback/res/stasis/command.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback/res/stasis/command.c?view=diff&rev=388249&r1=388248&r2=388249
==============================================================================
--- team/dlee/playback/res/stasis/command.c (original)
+++ team/dlee/playback/res/stasis/command.c Thu May  9 23:07:15 2013
@@ -32,63 +32,4 @@
 #include "asterisk/lock.h"
 #include "asterisk/stasis_app_impl.h"
 
-struct stasis_app_command {
-	ast_mutex_t lock;
-	ast_cond_t condition;
-	stasis_app_command_cb callback;
-	void *data;
-	void *retval;
-	int is_done:1;
-};
 
-static void command_dtor(void *obj)
-{
-	struct stasis_app_command *command = obj;
-	ast_mutex_destroy(&command->lock);
-	ast_cond_destroy(&command->condition);
-}
-
-struct stasis_app_command *command_create(
-	stasis_app_command_cb callback, void *data)
-{
-	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
-
-	command = ao2_alloc(sizeof(*command), command_dtor);
-	if (!command) {
-		return NULL;
-	}
-
-	ast_mutex_init(&command->lock);
-	ast_cond_init(&command->condition, 0);
-	command->callback = callback;
-	command->data = data;
-
-	ao2_ref(command, +1);
-	return command;
-}
-
-static void command_complete(struct stasis_app_command *command, void *retval)
-{
-	SCOPED_MUTEX(lock, &command->lock);
-
-	command->is_done = 1;
-	command->retval = retval;
-	ast_cond_signal(&command->condition);
-}
-
-void command_invoke(struct stasis_app_command *command,
-	struct stasis_app_control *control, struct ast_channel *chan)
-{
-	void *retval = command->callback(control, chan, command->data);
-	command_complete(command, retval);
-}
-
-void *command_join(struct stasis_app_command *command)
-{
-	SCOPED_MUTEX(lock, &command->lock);
-	while (!command->is_done) {
-		ast_cond_wait(&command->condition, &command->lock);
-	}
-
-	return command->retval;
-}

Modified: team/dlee/playback/res/stasis/control.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback/res/stasis/control.c?view=diff&rev=388249&r1=388248&r2=388249
==============================================================================
--- team/dlee/playback/res/stasis/control.c (original)
+++ team/dlee/playback/res/stasis/control.c Thu May  9 23:07:15 2013
@@ -30,146 +30,3 @@
 #include "command.h"
 #include "control.h"
 
-struct stasis_app_control {
-	/*! Queue of commands to dispatch on the channel */
-	struct ao2_container *command_queue;
-	/*!
-	 * When set, /c app_stasis should exit and continue in the dialplan.
-	 */
-	int is_done:1;
-	/*!
-	 * The associated channel.
-	 * Be very careful with the threading associated w/ manipulating
-	 * the channel.
-	 */
-	struct ast_channel *channel;
-};
-
-struct stasis_app_control *control_create(struct ast_channel *channel)
-{
-	struct stasis_app_control *control;
-
-	control = ao2_alloc(sizeof(*control), NULL);
-	if (!control) {
-		return NULL;
-	}
-
-	control->command_queue = ao2_container_alloc_list(0, 0, NULL, NULL);
-
-	control->channel = channel;
-
-	return control;
-}
-
-const char *stasis_app_control_get_channel_id(
-	const struct stasis_app_control *control)
-{
-	return ast_channel_uniqueid(control->channel);
-}
-
-static struct stasis_app_command *exec_command(
-	struct stasis_app_control *control, stasis_app_command_cb command_fn,
-	void *data)
-{
-	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
-
-	command = command_create(command_fn, data);
-
-	if (!command) {
-		return NULL;
-	}
-
-	ao2_lock(control);
-	ao2_ref(command, +1);
-	ao2_link(control->command_queue, command);
-	ao2_unlock(control);
-
-	ao2_ref(command, +1);
-	return command;
-}
-
-void *stasis_app_send_command(struct stasis_app_control *control,
-	stasis_app_command_cb command_fn, void *data)
-{
-	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
-
-	if (control == NULL) {
-		return NULL;
-	}
-
-	command = exec_command(control, command_fn, data);
-	if (!command) {
-		return NULL;
-	}
-
-	return command_join(command);
-}
-
-int stasis_app_send_command_async(struct stasis_app_control *control,
-	stasis_app_command_cb command_fn, void *data)
-{
-	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
-
-	if (control == NULL) {
-		return -1;
-	}
-
-	command = exec_command(control, command_fn, data);
-	if (!command) {
-		return -1;
-	}
-
-	return 0;
-}
-
-void stasis_app_control_publish(
-	struct stasis_app_control *control, struct stasis_message *message)
-{
-	if (!control || !control->channel || !message) {
-		return;
-	}
-	stasis_publish(ast_channel_topic(control->channel), message);
-}
-
-int control_is_done(struct stasis_app_control *control)
-{
-	/* Called from stasis_app_exec thread; no lock needed */
-	return control->is_done;
-}
-
-static void *app_control_continue(struct stasis_app_control *control,
-	struct ast_channel *chan, void *data)
-{
-	/* Called from stasis_app_exec thread; no lock needed */
-	control->is_done = 1;
-	return NULL;
-}
-
-void stasis_app_control_continue(struct stasis_app_control *control)
-{
-	stasis_app_send_command_async(control, app_control_continue, NULL);
-}
-
-int control_dispatch_all(struct stasis_app_control *control,
-	struct ast_channel *chan)
-{
-	int count = 0;
-	struct ao2_iterator i;
-	void *obj;
-
-	SCOPED_AO2LOCK(lock, control);
-
-	ast_assert(control->channel == chan);
-
-	i = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK);
-
-	while ((obj = ao2_iterator_next(&i))) {
-		RAII_VAR(struct stasis_app_command *, command, obj, ao2_cleanup);
-		command_invoke(command, control, chan);
-		++count;
-	}
-
-	ao2_iterator_destroy(&i);
-	return count;
-}
-




More information about the asterisk-commits mailing list