summaryrefslogtreecommitdiff
path: root/res/stasis
diff options
context:
space:
mode:
authorDavid M. Lee <dlee@digium.com>2013-05-14 21:45:08 +0000
committerDavid M. Lee <dlee@digium.com>2013-05-14 21:45:08 +0000
commite8f4ac6c610265b46014da9259062bb74d3892d1 (patch)
tree1a891ebe67da746feef4733618da502f43efe8d3 /res/stasis
parentd1d1425327378e16e77e7de6e64daccf289b4cc3 (diff)
Break res_stasis into smaller files.
When implementing playback for stasis-http, the monolithicedness of res_stasis really started to get in my way. This patch breaks the major components of res_stasis.c into individual files. * res/stasis/app.c - Stasis application tracking * res/stasis/control.c - Channel control objects * res/stasis/command.c - Channel command object This refactoring also allows res_stasis applications to be loaded as independent modules, such as the new res_stasis_answer module. The bulk of this patch is simply moving code from one file to another, adjusting names and adding accessors as necessary. Review: https://reviewboard.asterisk.org/r/2530/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@388729 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/stasis')
-rw-r--r--res/stasis/app.c142
-rw-r--r--res/stasis/app.h77
-rw-r--r--res/stasis/command.c95
-rw-r--r--res/stasis/command.h42
-rw-r--r--res/stasis/control.c199
-rw-r--r--res/stasis/control.h55
6 files changed, 610 insertions, 0 deletions
diff --git a/res/stasis/app.c b/res/stasis/app.c
new file mode 100644
index 000000000..d552eb4ab
--- /dev/null
+++ b/res/stasis/app.c
@@ -0,0 +1,142 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@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.
+ */
+
+/*! \file
+ *
+ * \brief Stasis application support.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "app.h"
+
+#include "asterisk/stasis_app.h"
+#include "asterisk/stasis_channels.h"
+
+/*!
+ * \brief Number of buckets for the channels container for app instances. Remember
+ * to keep it a prime number!
+ */
+#define APP_CHANNELS_BUCKETS 7
+
+struct app {
+ /*! Callback function for this application. */
+ stasis_app_cb handler;
+ /*! Opaque data to hand to callback function. */
+ void *data;
+ /*! List of channel identifiers this app instance is interested in */
+ struct ao2_container *channels;
+ /*! Name of the Stasis application */
+ char name[];
+};
+
+static void app_dtor(void *obj)
+{
+ struct app *app = obj;
+
+ ao2_cleanup(app->data);
+ app->data = NULL;
+ ao2_cleanup(app->channels);
+ app->channels = NULL;
+}
+
+struct app *app_create(const char *name, stasis_app_cb handler, void *data)
+{
+ RAII_VAR(struct app *, app, NULL, ao2_cleanup);
+ 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;
+
+ app->channels = ast_str_container_alloc(APP_CHANNELS_BUCKETS);
+ if (!app->channels) {
+ return NULL;
+ }
+
+ ao2_ref(app, +1);
+ return app;
+}
+
+int app_add_channel(struct app *app, const struct ast_channel *chan)
+{
+ const char *uniqueid;
+ ast_assert(chan != NULL);
+ ast_assert(app != NULL);
+
+ uniqueid = ast_channel_uniqueid(chan);
+ if (!ast_str_container_add(app->channels, uniqueid)) {
+ return -1;
+ }
+ return 0;
+}
+
+void app_remove_channel(struct app* app, const struct ast_channel *chan)
+{
+ ast_assert(chan != NULL);
+ ast_assert(app != NULL);
+
+ ao2_find(app->channels, ast_channel_uniqueid(chan), OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
+}
+
+/*!
+ * \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);
+}
+
+void app_update(struct app *app, stasis_app_cb handler, void *data)
+{
+ SCOPED_AO2LOCK(lock, app);
+
+ 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;
+}
+
+int app_is_watching_channel(struct app *app, const char *uniqueid)
+{
+ RAII_VAR(char *, found, NULL, ao2_cleanup);
+ found = ao2_find(app->channels, uniqueid, OBJ_KEY);
+ return found != NULL;
+}
diff --git a/res/stasis/app.h b/res/stasis/app.h
new file mode 100644
index 000000000..7b76db107
--- /dev/null
+++ b/res/stasis/app.h
@@ -0,0 +1,77 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@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.
+ */
+
+#ifndef _ASTERISK_RES_STASIS_APP_H
+#define _ASTERISK_RES_STASIS_APP_H
+
+/*! \file
+ *
+ * \brief Internal API for the Stasis application controller.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ * \since 12
+ */
+
+#include "asterisk/channel.h"
+#include "asterisk/stasis.h"
+#include "asterisk/stasis_app.h"
+
+/*!
+ * \brief Opaque pointer to \c res_stasis app structure.
+ */
+struct app;
+
+/*!
+ * \brief Create a res_stasis application.
+ *
+ * \param name Name of the application.
+ * \param handler Callback for messages sent to the application.
+ * \param data Data pointer provided to the callback.
+ * \return New \c res_stasis application.
+ * \return \c NULL on error.
+ */
+struct app *app_create(const char *name, stasis_app_cb handler, void *data);
+
+/*!
+ * \brief Update the handler and data for a \c res_stasis application.
+ *
+ * \param app Application to update.
+ * \param handler New application callback.
+ * \param data New data pointer for the callback.
+ */
+void app_update(struct app *app, stasis_app_cb handler, void *data);
+
+const char *app_name(const struct app *app);
+
+struct stasis_subscription *app_subscribe(struct app *app,
+ struct stasis_topic *topic);
+
+void app_send(struct app *app, struct ast_json *message);
+
+int app_send_start_msg(struct app *app, struct ast_channel *chan, int argc,
+ char *argv[]);
+
+int app_send_end_msg(struct app *app, struct ast_channel *chan);
+
+int app_is_watching_channel(struct app *app, const char *uniqueid);
+
+int app_add_channel(struct app* app, const struct ast_channel *chan);
+
+void app_remove_channel(struct app *app, const struct ast_channel *chan);
+
+#endif /* _ASTERISK_RES_STASIS_APP_H */
diff --git a/res/stasis/command.c b/res/stasis/command.c
new file mode 100644
index 000000000..f1f7f8f3b
--- /dev/null
+++ b/res/stasis/command.c
@@ -0,0 +1,95 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@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.
+ */
+
+/*! \file
+ *
+ * \brief Stasis application command support.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "command.h"
+
+#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_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);
+}
+
diff --git a/res/stasis/command.h b/res/stasis/command.h
new file mode 100644
index 000000000..21f4df0c0
--- /dev/null
+++ b/res/stasis/command.h
@@ -0,0 +1,42 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@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.
+ */
+
+#ifndef _ASTERISK_RES_STASIS_COMMAND_H
+#define _ASTERISK_RES_STASIS_COMMAND_H
+
+/*! \file
+ *
+ * \brief Internal API for the Stasis application commands.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ * \since 12
+ */
+
+#include "asterisk/stasis_app_impl.h"
+
+struct stasis_app_command;
+
+struct stasis_app_command *command_create(
+ stasis_app_command_cb callback, void *data);
+
+void command_invoke(struct stasis_app_command *command,
+ struct stasis_app_control *control, struct ast_channel *chan);
+
+void *command_join(struct stasis_app_command *command);
+
+#endif /* _ASTERISK_RES_STASIS_CONTROL_H */
diff --git a/res/stasis/control.c b/res/stasis/control.c
new file mode 100644
index 000000000..e32781b5f
--- /dev/null
+++ b/res/stasis/control.c
@@ -0,0 +1,199 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@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.
+ */
+
+/*! \file
+ *
+ * \brief Stasis application control support.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/stasis_channels.h"
+
+#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;
+}
+
+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;
+}
+
+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);
+}
+
+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;
+}
diff --git a/res/stasis/control.h b/res/stasis/control.h
new file mode 100644
index 000000000..9a4243be1
--- /dev/null
+++ b/res/stasis/control.h
@@ -0,0 +1,55 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@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.
+ */
+
+#ifndef _ASTERISK_RES_STASIS_CONTROL_H
+#define _ASTERISK_RES_STASIS_CONTROL_H
+
+/*! \file
+ *
+ * \brief Internal API for the Stasis application controller.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ * \since 12
+ */
+
+#include "asterisk/stasis_app.h"
+
+/*!
+ * \brief Create a control object.
+ *
+ * \param channel Channel to control.
+ * \return New control object.
+ * \return \c NULL on error.
+ */
+struct stasis_app_control *control_create(struct ast_channel *channel);
+
+/*!
+ * \brief Dispatch all commands enqueued to this control.
+ *
+ * \param control Control object to dispatch.
+ * \param chan Associated channel.
+ * \return Number of commands executed
+ */
+int control_dispatch_all(struct stasis_app_control *control,
+ struct ast_channel *chan);
+
+int control_is_done(struct stasis_app_control *control);
+
+void control_continue(struct stasis_app_control *control);
+
+#endif /* _ASTERISK_RES_STASIS_CONTROL_H */