| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
|
| 21 |
#include "asterisk/astobj2.h" |
| 22 |
#include "asterisk/sched.h" |
| 23 |
#include "asterisk/utils.h" |
| 24 |
|
| 25 |
#include "asterisk/res_aeap.h" |
| 26 |
#include "asterisk/res_aeap_message.h" |
| 27 |
|
| 28 |
#include "general.h" |
| 29 |
#include "logger.h" |
| 30 |
#include "transaction.h" |
| 31 |
|
| 32 |
struct aeap_transaction { |
| 33 |
|
| 34 |
struct ast_aeap *aeap; |
| 35 |
|
| 36 |
struct ao2_container *container; |
| 37 |
|
| 38 |
int sched_id; |
| 39 |
|
| 40 |
int handled; |
| 41 |
|
| 42 |
ast_cond_t handled_cond; |
| 43 |
|
| 44 |
int result; |
| 45 |
|
| 46 |
struct ast_aeap_tsx_params params; |
| 47 |
|
| 48 |
char id[0]; |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
#define AEAP_TRANSACTION_BUCKETS 11 |
| 53 |
|
| 54 |
AO2_STRING_FIELD_HASH_FN(aeap_transaction, id); |
| 55 |
AO2_STRING_FIELD_CMP_FN(aeap_transaction, id); |
| 56 |
|
| 57 |
int aeap_transaction_cancel_timer(struct aeap_transaction *tsx) |
| 58 |
{ |
| 59 |
if (tsx && tsx->sched_id != -1) { |
| 60 |
AST_SCHED_DEL_UNREF(aeap_sched_context(), tsx->sched_id, ao2_ref(tsx, -1)); |
| 61 |
return tsx->sched_id != -1; |
| 62 |
} |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
void aeap_transaction_params_cleanup(struct ast_aeap_tsx_params *params) |
| 68 |
{ |
| 69 |
ao2_cleanup(params->msg); |
| 70 |
|
| 71 |
if (params->obj_cleanup) { |
| 72 |
params->obj_cleanup(params->obj); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
static void transaction_destructor(void *obj) |
| 77 |
{ |
| 78 |
struct aeap_transaction *tsx = obj; |
| 79 |
|
| 80 |
|
| 81 |
aeap_transaction_cancel_timer(tsx); |
| 82 |
|
| 83 |
aeap_transaction_params_cleanup(&tsx->params); |
| 84 |
|
| 85 |
ast_cond_destroy(&tsx->handled_cond); |
| 86 |
} |
| 87 |
|
| 88 |
static struct aeap_transaction *transaction_create(const char *id, |
| 89 |
struct ast_aeap_tsx_params *params, struct ast_aeap *aeap) |
| 90 |
{ |
| 91 |
struct aeap_transaction *tsx; |
| 92 |
|
| 93 |
if (!id) { |
| 94 |
aeap_error(aeap, "transaction", "missing transaction id"); |
| 95 |
aeap_transaction_params_cleanup(params); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
tsx = ao2_alloc(sizeof(*tsx) + strlen(id) + 1, transaction_destructor); |
| 100 |
if (!tsx) { |
| 101 |
aeap_error(aeap, "transaction", "unable to create for '%s'", id); |
| 102 |
aeap_transaction_params_cleanup(params); |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
strcpy(tsx->id, id); |
| 107 |
tsx->sched_id = -1; |
| 108 |
|
| 109 |
ast_cond_init(&tsx->handled_cond, NULL); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
tsx->aeap = aeap; |
| 117 |
tsx->params = *params; |
| 118 |
|
| 119 |
return tsx; |
| 120 |
} |
| 121 |
|
| 122 |
static void transaction_end(struct aeap_transaction *tsx, int timed_out, int result) |
| 123 |
{ |
| 124 |
if (!tsx) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
ao2_lock(tsx); |
| 129 |
|
| 130 |
tsx->result = result; |
| 131 |
|
| 132 |
if (tsx->container) { |
| 133 |
ao2_unlink(tsx->container, tsx); |
| 134 |
tsx->container = NULL; |
| 135 |
} |
| 136 |
|
| 137 |
if (!timed_out) { |
| 138 |
aeap_transaction_cancel_timer(tsx); |
| 139 |
} else if (tsx->sched_id != -1) { |
| 140 |
tsx->sched_id = -1; |
| 141 |
} |
| 142 |
|
| 143 |
if (!tsx->handled) { |
| 144 |
if (timed_out) { |
| 145 |
if (tsx->params.on_timeout) { |
| 146 |
tsx->params.on_timeout(tsx->aeap, tsx->params.msg, tsx->params.obj); |
| 147 |
} else { |
| 148 |
aeap_error(tsx->aeap, "transaction", "message '%s' timed out", |
| 149 |
ast_aeap_message_name(tsx->params.msg)); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
tsx->handled = 1; |
| 154 |
ast_cond_signal(&tsx->handled_cond); |
| 155 |
} |
| 156 |
|
| 157 |
ao2_unlock(tsx); |
| 158 |
|
| 159 |
ao2_ref(tsx, -1); |
| 160 |
} |
| 161 |
|
| 162 |
static int transaction_raise_timeout(const void *data) |
| 163 |
{ |
| 164 |
|
| 165 |
transaction_end((struct aeap_transaction *)data, 1, -1); |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
static int transaction_sched_timer(struct aeap_transaction *tsx) |
| 171 |
{ |
| 172 |
if (tsx->params.timeout <= 0 || tsx->sched_id != -1) { |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
tsx->sched_id = ast_sched_add(aeap_sched_context(), tsx->params.timeout, |
| 177 |
transaction_raise_timeout, ao2_bump(tsx)); |
| 178 |
if (tsx->sched_id == -1) { |
| 179 |
aeap_error(tsx->aeap, "transaction", "unable to schedule timeout for '%s'", tsx->id); |
| 180 |
ao2_ref(tsx, -1); |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static void transaction_wait(struct aeap_transaction *tsx) |
| 188 |
{ |
| 189 |
ao2_lock(tsx); |
| 190 |
|
| 191 |
while (!tsx->handled) { |
| 192 |
ast_cond_wait(&tsx->handled_cond, ao2_object_get_lockaddr(tsx)); |
| 193 |
} |
| 194 |
|
| 195 |
ao2_unlock(tsx); |
| 196 |
} |
| 197 |
|
| 198 |
int aeap_transaction_start(struct aeap_transaction *tsx) |
| 199 |
{ |
| 200 |
if (transaction_sched_timer(tsx)) { |
| 201 |
return -1; |
| 202 |
} |
| 203 |
|
| 204 |
if (tsx->params.wait) { |
| 205 |
|
| 206 |
transaction_wait(tsx); |
| 207 |
} |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
struct aeap_transaction *aeap_transaction_get(struct ao2_container *transactions, const char *id) |
| 213 |
{ |
| 214 |
return ao2_find(transactions, id, OBJ_SEARCH_KEY); |
| 215 |
} |
| 216 |
|
| 217 |
void aeap_transaction_end(struct aeap_transaction *tsx, int result) |
| 218 |
{ |
| 219 |
transaction_end(tsx, 0, result); |
| 220 |
} |
| 221 |
|
| 222 |
int aeap_transaction_result(struct aeap_transaction *tsx) |
| 223 |
{ |
| 224 |
return tsx->result; |
| 225 |
} |
| 226 |
|
| 227 |
void *aeap_transaction_user_obj(struct aeap_transaction *tsx) |
| 228 |
{ |
| 229 |
return tsx->params.obj; |
| 230 |
} |
| 231 |
|
| 232 |
struct aeap_transaction *aeap_transaction_create_and_add(struct ao2_container *transactions, |
| 233 |
const char *id, struct ast_aeap_tsx_params *params, struct ast_aeap *aeap) |
| 234 |
{ |
| 235 |
struct aeap_transaction *tsx; |
| 236 |
|
| 237 |
tsx = transaction_create(id, params, aeap); |
| 238 |
if (!tsx) { |
| 239 |
return NULL; |
| 240 |
} |
| 241 |
|
| 242 |
if (!ao2_link(transactions, tsx)) { |
| 243 |
aeap_error(tsx->aeap, "transaction", "unable to add '%s' to container", id); |
| 244 |
ao2_ref(tsx, -1); |
| 245 |
return NULL; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
tsx->container = transactions; |
| 268 |
|
| 269 |
return tsx; |
| 270 |
} |
| 271 |
|
| 272 |
struct ao2_container *aeap_transactions_create(void) |
| 273 |
{ |
| 274 |
struct ao2_container *transactions; |
| 275 |
|
| 276 |
transactions = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, AEAP_TRANSACTION_BUCKETS, |
| 277 |
aeap_transaction_hash_fn, NULL, aeap_transaction_cmp_fn); |
| 278 |
if (!transactions) { |
| 279 |
ast_log(LOG_ERROR, "AEAP transaction: unable to create container\n"); |
| 280 |
return NULL; |
| 281 |
} |
| 282 |
|
| 283 |
return transactions; |
| 284 |
} |