| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include "command.h" |
| 29 |
|
| 30 |
#include "asterisk/lock.h" |
| 31 |
#include "asterisk/stasis_app_impl.h" |
| 32 |
|
| 33 |
struct stasis_app_command { |
| 34 |
ast_mutex_t lock; |
| 35 |
ast_cond_t condition; |
| 36 |
stasis_app_command_cb callback; |
| 37 |
void *data; |
| 38 |
command_data_destructor_fn data_destructor; |
| 39 |
int retval; |
| 40 |
unsigned int is_done:1; |
| 41 |
}; |
| 42 |
|
| 43 |
static void command_dtor(void *obj) |
| 44 |
{ |
| 45 |
struct stasis_app_command *command = obj; |
| 46 |
|
| 47 |
if (command->data_destructor) { |
| 48 |
command->data_destructor(command->data); |
| 49 |
} |
| 50 |
|
| 51 |
ast_mutex_destroy(&command->lock); |
| 52 |
ast_cond_destroy(&command->condition); |
| 53 |
} |
| 54 |
|
| 55 |
struct stasis_app_command *command_create( |
| 56 |
stasis_app_command_cb callback, void *data, command_data_destructor_fn data_destructor) |
| 57 |
{ |
| 58 |
struct stasis_app_command *command; |
| 59 |
|
| 60 |
command = ao2_alloc(sizeof(*command), command_dtor); |
| 61 |
if (!command) { |
| 62 |
if (data_destructor) { |
| 63 |
data_destructor(data); |
| 64 |
} |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
|
| 68 |
ast_mutex_init(&command->lock); |
| 69 |
ast_cond_init(&command->condition, 0); |
| 70 |
command->callback = callback; |
| 71 |
command->data = data; |
| 72 |
command->data_destructor = data_destructor; |
| 73 |
|
| 74 |
return command; |
| 75 |
} |
| 76 |
|
| 77 |
void command_complete(struct stasis_app_command *command, int retval) |
| 78 |
{ |
| 79 |
ast_mutex_lock(&command->lock); |
| 80 |
command->is_done = 1; |
| 81 |
command->retval = retval; |
| 82 |
ast_cond_signal(&command->condition); |
| 83 |
ast_mutex_unlock(&command->lock); |
| 84 |
} |
| 85 |
|
| 86 |
int command_join(struct stasis_app_command *command) |
| 87 |
{ |
| 88 |
int ret; |
| 89 |
|
| 90 |
ast_mutex_lock(&command->lock); |
| 91 |
while (!command->is_done) { |
| 92 |
ast_cond_wait(&command->condition, &command->lock); |
| 93 |
} |
| 94 |
|
| 95 |
ret = command->retval; |
| 96 |
ast_mutex_unlock(&command->lock); |
| 97 |
|
| 98 |
return ret; |
| 99 |
} |
| 100 |
|
| 101 |
void command_invoke(struct stasis_app_command *command, |
| 102 |
struct stasis_app_control *control, struct ast_channel *chan) |
| 103 |
{ |
| 104 |
int retval = command->callback(control, chan, command->data); |
| 105 |
if (command->data_destructor) { |
| 106 |
command->data_destructor(command->data); |
| 107 |
command->data_destructor = NULL; |
| 108 |
} |
| 109 |
command_complete(command, retval); |
| 110 |
} |
| 111 |
|
| 112 |
static void command_queue_prestart_destroy(void *obj) |
| 113 |
{ |
| 114 |
|
| 115 |
ao2_cleanup(obj); |
| 116 |
} |
| 117 |
|
| 118 |
static const struct ast_datastore_info command_queue_prestart = { |
| 119 |
.type = "stasis-command-prestart-queue", |
| 120 |
.destroy = command_queue_prestart_destroy, |
| 121 |
}; |
| 122 |
|
| 123 |
int command_prestart_queue_command(struct ast_channel *chan, |
| 124 |
stasis_app_command_cb command_fn, void *data, command_data_destructor_fn data_destructor) |
| 125 |
{ |
| 126 |
struct ast_datastore *datastore; |
| 127 |
struct ao2_container *command_queue; |
| 128 |
RAII_VAR(struct stasis_app_command *, command, |
| 129 |
command_create(command_fn, data, data_destructor), ao2_cleanup); |
| 130 |
|
| 131 |
if (!command) { |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
datastore = ast_channel_datastore_find(chan, &command_queue_prestart, NULL); |
| 136 |
if (datastore) { |
| 137 |
command_queue = datastore->data; |
| 138 |
ao2_link(command_queue, command); |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
command_queue = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL); |
| 143 |
if (!command_queue) { |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
datastore = ast_datastore_alloc(&command_queue_prestart, NULL); |
| 148 |
if (!datastore) { |
| 149 |
ao2_cleanup(command_queue); |
| 150 |
return -1; |
| 151 |
} |
| 152 |
ast_channel_datastore_add(chan, datastore); |
| 153 |
|
| 154 |
datastore->data = command_queue; |
| 155 |
ao2_link(command_queue, command); |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
struct ao2_container *command_prestart_get_container(struct ast_channel *chan) |
| 161 |
{ |
| 162 |
struct ast_datastore *datastore = ast_channel_datastore_find(chan, &command_queue_prestart, NULL); |
| 163 |
|
| 164 |
if (!datastore) { |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
return ao2_bump(datastore->data); |
| 169 |
} |