command.c File Reference

Stasis application command support. More...

#include "asterisk.h"
#include "command.h"
#include "asterisk/lock.h"
#include "asterisk/stasis_app_impl.h"

Include dependency graph for command.c:

Go to the source code of this file.

Data Structures

struct  stasis_app_command

Functions

void command_complete (struct stasis_app_command *command, int retval)
struct stasis_app_commandcommand_create (stasis_app_command_cb callback, void *data, command_data_destructor_fn data_destructor)
static void command_dtor (void *obj)
void command_invoke (struct stasis_app_command *command, struct stasis_app_control *control, struct ast_channel *chan)
int command_join (struct stasis_app_command *command)
struct ao2_containercommand_prestart_get_container (struct ast_channel *chan)
 Get the Stasis() prestart commands for a channel.
int command_prestart_queue_command (struct ast_channel *chan, stasis_app_command_cb command_fn, void *data, command_data_destructor_fn data_destructor)
 Queue a Stasis() prestart command for a channel.
static void command_queue_prestart_destroy (void *obj)

Variables

static struct ast_datastore_info command_queue_prestart


Detailed Description

Stasis application command support.

Author:
David M. Lee, II <dlee@digium.com>

Definition in file command.c.


Function Documentation

void command_complete ( struct stasis_app_command command,
int  retval 
)

Definition at line 79 of file command.c.

References ast_cond_signal, stasis_app_command::condition, stasis_app_command::is_done, stasis_app_command::lock, lock, stasis_app_command::retval, and SCOPED_MUTEX.

Referenced by command_invoke(), and exec_command_on_condition().

00080 {
00081    SCOPED_MUTEX(lock, &command->lock);
00082 
00083    command->is_done = 1;
00084    command->retval = retval;
00085    ast_cond_signal(&command->condition);
00086 }

struct stasis_app_command* command_create ( stasis_app_command_cb  callback,
void *  data,
command_data_destructor_fn  data_destructor 
) [read]

Definition at line 57 of file command.c.

References ao2_alloc, ast_cond_init, ast_mutex_init, stasis_app_command::callback, command_dtor(), stasis_app_command::condition, stasis_app_command::data, stasis_app_command::data_destructor, stasis_app_command::lock, and NULL.

Referenced by command_prestart_queue_command(), and exec_command_on_condition().

00059 {
00060    struct stasis_app_command *command;
00061 
00062    command = ao2_alloc(sizeof(*command), command_dtor);
00063    if (!command) {
00064       if (data_destructor) {
00065          data_destructor(data);
00066       }
00067       return NULL;
00068    }
00069 
00070    ast_mutex_init(&command->lock);
00071    ast_cond_init(&command->condition, 0);
00072    command->callback = callback;
00073    command->data = data;
00074    command->data_destructor = data_destructor;
00075 
00076    return command;
00077 }

static void command_dtor ( void *  obj  )  [static]

Definition at line 45 of file command.c.

References ast_cond_destroy, ast_mutex_destroy, stasis_app_command::condition, stasis_app_command::data, stasis_app_command::data_destructor, and stasis_app_command::lock.

Referenced by command_create().

00046 {
00047    struct stasis_app_command *command = obj;
00048 
00049    if (command->data_destructor) {
00050       command->data_destructor(command->data);
00051    }
00052 
00053    ast_mutex_destroy(&command->lock);
00054    ast_cond_destroy(&command->condition);
00055 }

void command_invoke ( struct stasis_app_command command,
struct stasis_app_control control,
struct ast_channel chan 
)

Definition at line 98 of file command.c.

References stasis_app_command::callback, command_complete(), stasis_app_command::data, stasis_app_command::data_destructor, NULL, and retval.

Referenced by control_dispatch_all(), and control_prestart_dispatch_all().

00100 {
00101    int retval = command->callback(control, chan, command->data);
00102    if (command->data_destructor) {
00103       command->data_destructor(command->data);
00104       command->data_destructor = NULL;
00105    }
00106    command_complete(command, retval);
00107 }

int command_join ( struct stasis_app_command command  ) 

Definition at line 88 of file command.c.

References ast_cond_wait, stasis_app_command::condition, stasis_app_command::is_done, stasis_app_command::lock, lock, stasis_app_command::retval, and SCOPED_MUTEX.

Referenced by app_send_command_on_condition().

00089 {
00090    SCOPED_MUTEX(lock, &command->lock);
00091    while (!command->is_done) {
00092       ast_cond_wait(&command->condition, &command->lock);
00093    }
00094 
00095    return command->retval;
00096 }

struct ao2_container* command_prestart_get_container ( struct ast_channel chan  )  [read]

Get the Stasis() prestart commands for a channel.

Precondition:
chan must be locked
Parameters:
chan The channel from which to get prestart commands
Returns:
The command prestart container for chan (must be ao2_cleanup()'d)

Definition at line 157 of file command.c.

References ao2_bump, ast_channel_datastore_find(), ast_datastore::data, and NULL.

Referenced by control_prestart_dispatch_all().

00158 {
00159    struct ast_datastore *datastore = ast_channel_datastore_find(chan, &command_queue_prestart, NULL);
00160 
00161    if (!datastore) {
00162       return NULL;
00163    }
00164 
00165    return ao2_bump(datastore->data);
00166 }

int command_prestart_queue_command ( struct ast_channel chan,
stasis_app_command_cb  command_fn,
void *  data,
command_data_destructor_fn  data_destructor 
)

Queue a Stasis() prestart command for a channel.

Precondition:
chan must be locked
Parameters:
chan The channel on which to queue the prestart command
command_fn The callback to call for the command
data The data to pass to the command callback
data_destructor Optional function which will be called on the data in either the event of command completion or failure to schedule or complete the command
Return values:
zero on success
non-zero on failure

Definition at line 120 of file command.c.

References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_cleanup, ao2_container_alloc_list, ao2_link, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_datastore_alloc, command_create(), ast_datastore::data, NULL, and RAII_VAR.

Referenced by bridge_stasis_queue_join_action().

00122 {
00123    struct ast_datastore *datastore;
00124    struct ao2_container *command_queue;
00125    RAII_VAR(struct stasis_app_command *, command,
00126       command_create(command_fn, data, data_destructor), ao2_cleanup);
00127 
00128    if (!command) {
00129       return -1;
00130    }
00131 
00132    datastore = ast_channel_datastore_find(chan, &command_queue_prestart, NULL);
00133    if (datastore) {
00134       command_queue = datastore->data;
00135       ao2_link(command_queue, command);
00136       return 0;
00137    }
00138 
00139    command_queue = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
00140    if (!command_queue) {
00141       return -1;
00142    }
00143 
00144    datastore = ast_datastore_alloc(&command_queue_prestart, NULL);
00145    if (!datastore) {
00146       ao2_cleanup(command_queue);
00147       return -1;
00148    }
00149    ast_channel_datastore_add(chan, datastore);
00150 
00151    datastore->data = command_queue;
00152    ao2_link(command_queue, command);
00153 
00154    return 0;
00155 }

static void command_queue_prestart_destroy ( void *  obj  )  [static]

Definition at line 109 of file command.c.

References ao2_cleanup.

00110 {
00111    /* Clean up the container */
00112    ao2_cleanup(obj);
00113 }


Variable Documentation

Initial value:

 {
   .type = "stasis-command-prestart-queue",
   .destroy = command_queue_prestart_destroy,
}

Definition at line 115 of file command.c.


Generated on Thu Apr 16 06:30:17 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6