[asterisk-commits] dlee: branch dlee/playback r388319 - in /team/dlee/playback/res: ./ stasis/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri May 10 10:56:34 CDT 2013
Author: dlee
Date: Fri May 10 10:56:32 2013
New Revision: 388319
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=388319
Log:
Breaking it back apart again
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/app.h
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=388319&r1=388318&r2=388319
==============================================================================
--- team/dlee/playback/res/res_stasis.c (original)
+++ team/dlee/playback/res/res_stasis.c Fri May 10 10:56:32 2013
@@ -66,8 +66,6 @@
#include "stasis/control.h"
#include "stasis_http/resource_events.h"
-#include "stasis/command.h"
-
/*! Time to wait for a frame in the application */
#define MAX_WAIT_MS 200
@@ -84,12 +82,6 @@
#define CONTROLS_NUM_BUCKETS 127
/*!
- * \brief Number of buckets for the channels container for app instances. Remember
- * to keep it a prime number!
- */
-#define APP_CHANNELS_BUCKETS 7
-
-/*!
* \brief Number of buckets for the blob_handlers container. Remember to keep
* it a prime number!
*/
@@ -105,56 +97,6 @@
/*! \brief Message router for the channel caching topic */
struct stasis_message_router *channel_router;
-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;
-}
-
/*! AO2 hash function for \ref app */
static int app_hash(const void *obj, const int flags)
{
@@ -177,150 +119,6 @@
} else {
return 0;
}
-}
-
-static 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;
-}
-
-static 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);
-}
-
-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 */
@@ -366,133 +164,15 @@
return ao2_find(app_controls, channel_id, OBJ_KEY);
}
-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);
-}
-
/*! \brief Typedef for blob handler callbacks */
typedef struct ast_json *(*channel_blob_handler_cb)(struct ast_channel_blob *);
-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;
-}
-
-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_json *, blob, 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;
- }
-
- blob = ast_json_pack("{s: []}", "args");
- if (!blob) {
- return -1;
- }
-
- /* Append arguments to args array */
- json_args = ast_json_object_get(blob, "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;
- }
- }
-
- msg = stasis_json_event_stasis_start_create(snapshot, blob);
- if (!msg) {
- 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 = stasis_json_event_stasis_end_create(snapshot);
- if (!msg) {
- return -1;
- }
-
- app_send(app, msg);
- return 0;
-}
-
static int app_watching_channel_cb(void *obj, void *arg, int flags)
{
- RAII_VAR(char *, uniqueid, NULL, ao2_cleanup);
struct app *app = obj;
- char *chan_uniqueid = arg;
-
- uniqueid = ao2_find(app->channels, chan_uniqueid, OBJ_KEY);
- return uniqueid ? CMP_MATCH : 0;
+ char *uniqueid = arg;
+
+ return app_is_watching_channel(app, uniqueid) ? CMP_MATCH : 0;
}
static struct ao2_container *get_watching_apps(const char *uniqueid)
@@ -973,123 +653,3 @@
.load = load_module,
.unload = unload_module);
-void app_update(struct app *app, stasis_app_cb handler, void *data)
-{
- RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
- RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
- SCOPED_AO2LOCK(lock, app);
-
- blob = ast_json_pack("{s: s}", "application", app->name);
- if (blob) {
- msg = stasis_json_event_application_replaced_create(blob);
- if (msg) {
- 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=388319&r1=388318&r2=388319
==============================================================================
--- team/dlee/playback/res/res_stasis_answer.c (original)
+++ team/dlee/playback/res/res_stasis_answer.c Fri May 10 10:56:32 2013
@@ -35,6 +35,36 @@
#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=388319&r1=388318&r2=388319
==============================================================================
--- team/dlee/playback/res/stasis/app.c (original)
+++ team/dlee/playback/res/stasis/app.c Fri May 10 10:56:32 2013
@@ -31,6 +31,189 @@
#include "asterisk/stasis_app.h"
#include "asterisk/stasis_channels.h"
-
-
-
+#include "../stasis_http/resource_events.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);
+}
+
+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_json *, blob, 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;
+ }
+
+ blob = ast_json_pack("{s: []}", "args");
+ if (!blob) {
+ return -1;
+ }
+
+ /* Append arguments to args array */
+ json_args = ast_json_object_get(blob, "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;
+ }
+ }
+
+ msg = stasis_json_event_stasis_start_create(snapshot, blob);
+ if (!msg) {
+ 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 = stasis_json_event_stasis_end_create(snapshot);
+ if (!msg) {
+ return -1;
+ }
+
+ app_send(app, msg);
+ return 0;
+}
+
+void app_update(struct app *app, stasis_app_cb handler, void *data)
+{
+ RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+ RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+ SCOPED_AO2LOCK(lock, app);
+
+ blob = ast_json_pack("{s: s}", "application", app->name);
+ if (blob) {
+ msg = stasis_json_event_application_replaced_create(blob);
+ if (msg) {
+ 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;
+}
+
+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;
+}
Modified: team/dlee/playback/res/stasis/app.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback/res/stasis/app.h?view=diff&rev=388319&r1=388318&r2=388319
==============================================================================
--- team/dlee/playback/res/stasis/app.h (original)
+++ team/dlee/playback/res/stasis/app.h Fri May 10 10:56:32 2013
@@ -31,10 +31,29 @@
#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);
@@ -49,4 +68,10 @@
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 */
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=388319&r1=388318&r2=388319
==============================================================================
--- team/dlee/playback/res/stasis/command.c (original)
+++ team/dlee/playback/res/stasis/command.c Fri May 10 10:56:32 2013
@@ -32,4 +32,64 @@
#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);
+}
+
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=388319&r1=388318&r2=388319
==============================================================================
--- team/dlee/playback/res/stasis/control.c (original)
+++ team/dlee/playback/res/stasis/control.c Fri May 10 10:56:32 2013
@@ -27,6 +27,173 @@
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;
+}
More information about the asterisk-commits
mailing list