| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "asterisk.h" |
| 24 |
|
| 25 |
#include "asterisk/stasis_state.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
struct stasis_state_proxy { |
| 32 |
AO2_WEAKPROXY(); |
| 33 |
|
| 34 |
struct stasis_state_manager *manager; |
| 35 |
|
| 36 |
char id[0]; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
struct stasis_state { |
| 51 |
|
| 52 |
unsigned int num_subscribers; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
struct stasis_state_manager *manager; |
| 58 |
|
| 59 |
struct stasis_forward *forward; |
| 60 |
|
| 61 |
struct stasis_topic *topic; |
| 62 |
|
| 63 |
struct stasis_message *msg; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
AST_VECTOR(, struct ast_eid) eids; |
| 69 |
|
| 70 |
char *id; |
| 71 |
}; |
| 72 |
|
| 73 |
AO2_STRING_FIELD_HASH_FN(stasis_state_proxy, id); |
| 74 |
AO2_STRING_FIELD_CMP_FN(stasis_state_proxy, id); |
| 75 |
|
| 76 |
|
| 77 |
#define STATE_BUCKETS 57 |
| 78 |
|
| 79 |
struct stasis_state_manager { |
| 80 |
|
| 81 |
struct ao2_container *states; |
| 82 |
|
| 83 |
struct stasis_topic *all_topic; |
| 84 |
|
| 85 |
AST_VECTOR_RW(, struct stasis_state_observer *) observers; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
static const char *state_id_by_topic(struct stasis_topic *manager_topic, |
| 105 |
const struct stasis_topic *state_topic) |
| 106 |
{ |
| 107 |
const char *id; |
| 108 |
|
| 109 |
|
| 110 |
ast_assert(ast_begins_with(stasis_topic_name(manager_topic), |
| 111 |
stasis_topic_name(state_topic))); |
| 112 |
|
| 113 |
id = strchr(stasis_topic_name(state_topic), '/'); |
| 114 |
|
| 115 |
|
| 116 |
ast_assert(id != NULL && *(id + 1) != '\0'); |
| 117 |
|
| 118 |
return (id + 1); |
| 119 |
} |
| 120 |
|
| 121 |
static void state_dtor(void *obj) |
| 122 |
{ |
| 123 |
struct stasis_state *state = obj; |
| 124 |
|
| 125 |
state->forward = stasis_forward_cancel(state->forward); |
| 126 |
ao2_cleanup(state->topic); |
| 127 |
state->topic = NULL; |
| 128 |
ao2_cleanup(state->msg); |
| 129 |
state->msg = NULL; |
| 130 |
|
| 131 |
|
| 132 |
ast_assert(AST_VECTOR_SIZE(&state->eids) == 0); |
| 133 |
AST_VECTOR_FREE(&state->eids); |
| 134 |
} |
| 135 |
|
| 136 |
static void state_proxy_dtor(void *obj) { |
| 137 |
struct stasis_state_proxy *proxy = obj; |
| 138 |
|
| 139 |
ao2_cleanup(proxy->manager); |
| 140 |
} |
| 141 |
|
| 142 |
static void state_proxy_sub_cb(void *obj, void *data) |
| 143 |
{ |
| 144 |
struct stasis_state_proxy *proxy = obj; |
| 145 |
|
| 146 |
ao2_unlink(proxy->manager->states, proxy); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
static struct stasis_state *state_alloc(struct stasis_state_manager *manager, |
| 169 |
struct stasis_topic *state_topic, const char *id, |
| 170 |
const char *file, int line, const char *func) |
| 171 |
{ |
| 172 |
struct stasis_state_proxy *proxy = NULL; |
| 173 |
struct stasis_state *state = NULL; |
| 174 |
|
| 175 |
if (!id) { |
| 176 |
|
| 177 |
ast_assert(state_topic != NULL); |
| 178 |
|
| 179 |
|
| 180 |
id = state_id_by_topic(manager->all_topic, state_topic); |
| 181 |
} |
| 182 |
|
| 183 |
state = __ao2_alloc(sizeof(*state), state_dtor, AO2_ALLOC_OPT_LOCK_MUTEX, id, file, line, func); |
| 184 |
if (!state) { |
| 185 |
goto error_return; |
| 186 |
} |
| 187 |
|
| 188 |
if (!state_topic) { |
| 189 |
char *name; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
if (ast_asprintf(&name, "%s/%s", stasis_topic_name(manager->all_topic), id) < 0) { |
| 197 |
goto error_return; |
| 198 |
} |
| 199 |
|
| 200 |
state->topic = stasis_topic_create(name); |
| 201 |
|
| 202 |
ast_free(name); |
| 203 |
if (!state->topic) { |
| 204 |
goto error_return; |
| 205 |
} |
| 206 |
} else { |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
ao2_ref(state_topic, +1); |
| 213 |
state->topic = state_topic; |
| 214 |
} |
| 215 |
|
| 216 |
proxy = ao2_t_weakproxy_alloc(sizeof(*proxy) + strlen(id) + 1, state_proxy_dtor, id); |
| 217 |
if (!proxy) { |
| 218 |
goto error_return; |
| 219 |
} |
| 220 |
|
| 221 |
strcpy(proxy->id, id); |
| 222 |
|
| 223 |
state->id = proxy->id; |
| 224 |
proxy->manager = ao2_bump(manager); |
| 225 |
state->manager = proxy->manager; |
| 226 |
|
| 227 |
state->forward = stasis_forward_all(state->topic, manager->all_topic); |
| 228 |
if (!state->forward) { |
| 229 |
goto error_return; |
| 230 |
} |
| 231 |
|
| 232 |
if (AST_VECTOR_INIT(&state->eids, 2)) { |
| 233 |
goto error_return; |
| 234 |
} |
| 235 |
|
| 236 |
if (ao2_t_weakproxy_set_object(proxy, state, OBJ_NOLOCK, "weakproxy link")) { |
| 237 |
goto error_return; |
| 238 |
} |
| 239 |
|
| 240 |
if (ao2_weakproxy_subscribe(proxy, state_proxy_sub_cb, NULL, OBJ_NOLOCK)) { |
| 241 |
goto error_return; |
| 242 |
} |
| 243 |
|
| 244 |
if (!ao2_link_flags(manager->states, proxy, OBJ_NOLOCK)) { |
| 245 |
goto error_return; |
| 246 |
} |
| 247 |
|
| 248 |
ao2_ref(proxy, -1); |
| 249 |
|
| 250 |
return state; |
| 251 |
|
| 252 |
error_return: |
| 253 |
ast_log(LOG_ERROR, "Unable to allocate state '%s' in manager '%s'\n", |
| 254 |
id, stasis_topic_name(manager->all_topic)); |
| 255 |
ao2_cleanup(state); |
| 256 |
ao2_cleanup(proxy); |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
#define state_find_or_add(manager, state_topic, id) __state_find_or_add(manager, state_topic, id, __FILE__, __LINE__, __PRETTY_FUNCTION__) |
| 272 |
static struct stasis_state *__state_find_or_add(struct stasis_state_manager *manager, |
| 273 |
struct stasis_topic *state_topic, const char *id, |
| 274 |
const char *file, int line, const char *func) |
| 275 |
{ |
| 276 |
struct stasis_state *state; |
| 277 |
|
| 278 |
ao2_lock(manager->states); |
| 279 |
if (ast_strlen_zero(id)) { |
| 280 |
id = state_id_by_topic(manager->all_topic, state_topic); |
| 281 |
} |
| 282 |
|
| 283 |
state = ao2_weakproxy_find(manager->states, id, OBJ_SEARCH_KEY | OBJ_NOLOCK, ""); |
| 284 |
if (!state) { |
| 285 |
state = state_alloc(manager, state_topic, id, file, line, func); |
| 286 |
} |
| 287 |
|
| 288 |
ao2_unlock(manager->states); |
| 289 |
|
| 290 |
return state; |
| 291 |
} |
| 292 |
|
| 293 |
static void state_manager_dtor(void *obj) |
| 294 |
{ |
| 295 |
struct stasis_state_manager *manager = obj; |
| 296 |
|
| 297 |
#ifdef AO2_DEBUG |
| 298 |
{ |
| 299 |
char *container_name = |
| 300 |
ast_alloca(strlen(stasis_topic_name(manager->all_topic)) + strlen("-manager") + 1); |
| 301 |
sprintf(container_name, "%s-manager", stasis_topic_name(manager->all_topic)); |
| 302 |
ao2_container_unregister(container_name); |
| 303 |
} |
| 304 |
#endif |
| 305 |
|
| 306 |
ao2_cleanup(manager->states); |
| 307 |
manager->states = NULL; |
| 308 |
ao2_cleanup(manager->all_topic); |
| 309 |
manager->all_topic = NULL; |
| 310 |
AST_VECTOR_RW_FREE(&manager->observers); |
| 311 |
} |
| 312 |
|
| 313 |
#ifdef AO2_DEBUG |
| 314 |
static void state_prnt_obj(void *v_obj, void *where, ao2_prnt_fn *prnt) |
| 315 |
{ |
| 316 |
struct stasis_state *state = v_obj; |
| 317 |
|
| 318 |
if (!state) { |
| 319 |
return; |
| 320 |
} |
| 321 |
prnt(where, "%s", stasis_topic_name(state->topic)); |
| 322 |
} |
| 323 |
#endif |
| 324 |
|
| 325 |
struct stasis_state_manager *stasis_state_manager_create(const char *topic_name) |
| 326 |
{ |
| 327 |
struct stasis_state_manager *manager; |
| 328 |
|
| 329 |
manager = ao2_alloc_options(sizeof(*manager), state_manager_dtor, |
| 330 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 331 |
if (!manager) { |
| 332 |
return NULL; |
| 333 |
} |
| 334 |
|
| 335 |
manager->states = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 336 |
STATE_BUCKETS, stasis_state_proxy_hash_fn, NULL, stasis_state_proxy_cmp_fn); |
| 337 |
if (!manager->states) { |
| 338 |
ao2_ref(manager, -1); |
| 339 |
return NULL; |
| 340 |
} |
| 341 |
|
| 342 |
manager->all_topic = stasis_topic_create(topic_name); |
| 343 |
if (!manager->all_topic) { |
| 344 |
ao2_ref(manager, -1); |
| 345 |
return NULL; |
| 346 |
} |
| 347 |
|
| 348 |
if (AST_VECTOR_RW_INIT(&manager->observers, 2) != 0) { |
| 349 |
ao2_ref(manager, -1); |
| 350 |
return NULL; |
| 351 |
} |
| 352 |
|
| 353 |
#ifdef AO2_DEBUG |
| 354 |
{ |
| 355 |
char *container_name = |
| 356 |
ast_alloca(strlen(stasis_topic_name(manager->all_topic)) + strlen("-manager") + 1); |
| 357 |
sprintf(container_name, "%s-manager", stasis_topic_name(manager->all_topic)); |
| 358 |
ao2_container_register(container_name, manager->states, state_prnt_obj); |
| 359 |
} |
| 360 |
#endif |
| 361 |
|
| 362 |
return manager; |
| 363 |
} |
| 364 |
|
| 365 |
struct stasis_topic *stasis_state_all_topic(struct stasis_state_manager *manager) |
| 366 |
{ |
| 367 |
return manager->all_topic; |
| 368 |
} |
| 369 |
|
| 370 |
struct stasis_topic *stasis_state_topic(struct stasis_state_manager *manager, const char *id) |
| 371 |
{ |
| 372 |
struct stasis_topic *topic; |
| 373 |
struct stasis_state *state; |
| 374 |
|
| 375 |
state = state_find_or_add(manager, NULL, id); |
| 376 |
if (!state) { |
| 377 |
return NULL; |
| 378 |
} |
| 379 |
|
| 380 |
topic = state->topic; |
| 381 |
ao2_ref(state, -1); |
| 382 |
return topic; |
| 383 |
} |
| 384 |
|
| 385 |
struct stasis_state_subscriber { |
| 386 |
|
| 387 |
struct stasis_state *state; |
| 388 |
|
| 389 |
struct stasis_subscription *stasis_sub; |
| 390 |
}; |
| 391 |
|
| 392 |
static void subscriber_dtor(void *obj) |
| 393 |
{ |
| 394 |
size_t i; |
| 395 |
struct stasis_state_subscriber *sub = obj; |
| 396 |
struct stasis_state_manager *manager = sub->state->manager; |
| 397 |
|
| 398 |
AST_VECTOR_RW_RDLOCK(&manager->observers); |
| 399 |
for (i = 0; i < AST_VECTOR_SIZE(&manager->observers); ++i) { |
| 400 |
if (AST_VECTOR_GET(&manager->observers, i)->on_unsubscribe) { |
| 401 |
AST_VECTOR_GET(&manager->observers, i)->on_unsubscribe(sub->state->id, sub); |
| 402 |
} |
| 403 |
} |
| 404 |
AST_VECTOR_RW_UNLOCK(&manager->observers); |
| 405 |
|
| 406 |
ao2_lock(sub->state); |
| 407 |
--sub->state->num_subscribers; |
| 408 |
ao2_unlock(sub->state); |
| 409 |
|
| 410 |
ao2_ref(sub->state, -1); |
| 411 |
} |
| 412 |
|
| 413 |
struct stasis_state_subscriber *stasis_state_add_subscriber( |
| 414 |
struct stasis_state_manager *manager, const char *id) |
| 415 |
{ |
| 416 |
size_t i; |
| 417 |
struct stasis_state_subscriber *sub = ao2_alloc_options( |
| 418 |
sizeof(*sub), subscriber_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 419 |
|
| 420 |
if (!sub) { |
| 421 |
ast_log(LOG_ERROR, "Unable to create subscriber to %s/%s\n", |
| 422 |
stasis_topic_name(manager->all_topic), id); |
| 423 |
return NULL; |
| 424 |
} |
| 425 |
|
| 426 |
sub->state = state_find_or_add(manager, NULL, id); |
| 427 |
if (!sub->state) { |
| 428 |
ao2_ref(sub, -1); |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
|
| 432 |
ao2_lock(sub->state); |
| 433 |
++sub->state->num_subscribers; |
| 434 |
ao2_unlock(sub->state); |
| 435 |
|
| 436 |
AST_VECTOR_RW_RDLOCK(&manager->observers); |
| 437 |
for (i = 0; i < AST_VECTOR_SIZE(&manager->observers); ++i) { |
| 438 |
if (AST_VECTOR_GET(&manager->observers, i)->on_subscribe) { |
| 439 |
AST_VECTOR_GET(&manager->observers, i)->on_subscribe(id, sub); |
| 440 |
} |
| 441 |
} |
| 442 |
AST_VECTOR_RW_UNLOCK(&manager->observers); |
| 443 |
|
| 444 |
return sub; |
| 445 |
} |
| 446 |
|
| 447 |
struct stasis_state_subscriber *stasis_state_subscribe_pool(struct stasis_state_manager *manager, |
| 448 |
const char *id, stasis_subscription_cb callback, void *data) |
| 449 |
{ |
| 450 |
struct stasis_topic *topic; |
| 451 |
struct stasis_state_subscriber *sub = stasis_state_add_subscriber(manager, id); |
| 452 |
|
| 453 |
if (!sub) { |
| 454 |
return NULL; |
| 455 |
} |
| 456 |
|
| 457 |
topic = sub->state->topic; |
| 458 |
ast_debug(3, "Creating stasis state subscription to id '%s'. Topic: '%s':%p %d\n", |
| 459 |
id, stasis_topic_name(topic), topic, (int)ao2_ref(topic, 0)); |
| 460 |
|
| 461 |
sub->stasis_sub = stasis_subscribe_pool(topic, callback, data); |
| 462 |
|
| 463 |
if (!sub->stasis_sub) { |
| 464 |
ao2_ref(sub, -1); |
| 465 |
return NULL; |
| 466 |
} |
| 467 |
|
| 468 |
return sub; |
| 469 |
} |
| 470 |
|
| 471 |
void *stasis_state_unsubscribe(struct stasis_state_subscriber *sub) |
| 472 |
{ |
| 473 |
sub->stasis_sub = stasis_unsubscribe(sub->stasis_sub); |
| 474 |
ao2_ref(sub, -1); |
| 475 |
return NULL; |
| 476 |
} |
| 477 |
|
| 478 |
void *stasis_state_unsubscribe_and_join(struct stasis_state_subscriber *sub) |
| 479 |
{ |
| 480 |
if (sub) { |
| 481 |
sub->stasis_sub = stasis_unsubscribe_and_join(sub->stasis_sub); |
| 482 |
ao2_ref(sub, -1); |
| 483 |
} |
| 484 |
|
| 485 |
return NULL; |
| 486 |
} |
| 487 |
|
| 488 |
const char *stasis_state_subscriber_id(const struct stasis_state_subscriber *sub) |
| 489 |
{ |
| 490 |
return sub->state->id; |
| 491 |
} |
| 492 |
|
| 493 |
struct stasis_topic *stasis_state_subscriber_topic(struct stasis_state_subscriber *sub) |
| 494 |
{ |
| 495 |
return sub->state->topic; |
| 496 |
} |
| 497 |
|
| 498 |
void *stasis_state_subscriber_data(struct stasis_state_subscriber *sub) |
| 499 |
{ |
| 500 |
void *res; |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
ao2_lock(sub->state); |
| 508 |
res = ao2_bump(stasis_message_data(sub->state->msg)); |
| 509 |
ao2_unlock(sub->state); |
| 510 |
|
| 511 |
return res; |
| 512 |
} |
| 513 |
|
| 514 |
struct stasis_subscription *stasis_state_subscriber_subscription( |
| 515 |
struct stasis_state_subscriber *sub) |
| 516 |
{ |
| 517 |
return sub->stasis_sub; |
| 518 |
} |
| 519 |
|
| 520 |
struct stasis_state_publisher { |
| 521 |
|
| 522 |
struct stasis_state *state; |
| 523 |
}; |
| 524 |
|
| 525 |
static void publisher_dtor(void *obj) |
| 526 |
{ |
| 527 |
struct stasis_state_publisher *pub = obj; |
| 528 |
|
| 529 |
ao2_ref(pub->state, -1); |
| 530 |
} |
| 531 |
|
| 532 |
struct stasis_state_publisher *stasis_state_add_publisher( |
| 533 |
struct stasis_state_manager *manager, const char *id) |
| 534 |
{ |
| 535 |
struct stasis_state_publisher *pub = ao2_alloc_options( |
| 536 |
sizeof(*pub), publisher_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 537 |
|
| 538 |
if (!pub) { |
| 539 |
ast_log(LOG_ERROR, "Unable to create publisher to %s/%s\n", |
| 540 |
stasis_topic_name(manager->all_topic), id); |
| 541 |
return NULL; |
| 542 |
} |
| 543 |
|
| 544 |
pub->state = state_find_or_add(manager, NULL, id); |
| 545 |
if (!pub->state) { |
| 546 |
ao2_ref(pub, -1); |
| 547 |
return NULL; |
| 548 |
} |
| 549 |
|
| 550 |
return pub; |
| 551 |
} |
| 552 |
|
| 553 |
const char *stasis_state_publisher_id(const struct stasis_state_publisher *pub) |
| 554 |
{ |
| 555 |
return pub->state->id; |
| 556 |
} |
| 557 |
|
| 558 |
struct stasis_topic *stasis_state_publisher_topic(struct stasis_state_publisher *pub) |
| 559 |
{ |
| 560 |
return pub->state->topic; |
| 561 |
} |
| 562 |
|
| 563 |
void stasis_state_publish(struct stasis_state_publisher *pub, struct stasis_message *msg) |
| 564 |
{ |
| 565 |
ao2_lock(pub->state); |
| 566 |
ao2_replace(pub->state->msg, msg); |
| 567 |
ao2_unlock(pub->state); |
| 568 |
|
| 569 |
stasis_publish(pub->state->topic, msg); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
static void state_find_or_add_eid(struct stasis_state *state, const struct ast_eid *eid) |
| 588 |
{ |
| 589 |
size_t i; |
| 590 |
|
| 591 |
if (!eid) { |
| 592 |
eid = &ast_eid_default; |
| 593 |
} |
| 594 |
|
| 595 |
for (i = 0; i < AST_VECTOR_SIZE(&state->eids); ++i) { |
| 596 |
if (!ast_eid_cmp(AST_VECTOR_GET_ADDR(&state->eids, i), eid)) { |
| 597 |
break; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
if (i == AST_VECTOR_SIZE(&state->eids)) { |
| 602 |
if (!AST_VECTOR_APPEND(&state->eids, *eid)) { |
| 603 |
|
| 604 |
ao2_ref(state, +1); |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
static void state_find_and_remove_eid(struct stasis_state *state, const struct ast_eid *eid) |
| 622 |
{ |
| 623 |
size_t i; |
| 624 |
|
| 625 |
if (!eid) { |
| 626 |
eid = &ast_eid_default; |
| 627 |
} |
| 628 |
|
| 629 |
for (i = 0; i < AST_VECTOR_SIZE(&state->eids); ++i) { |
| 630 |
if (!ast_eid_cmp(AST_VECTOR_GET_ADDR(&state->eids, i), eid)) { |
| 631 |
AST_VECTOR_REMOVE_UNORDERED(&state->eids, i); |
| 632 |
|
| 633 |
ao2_ref(state, -1); |
| 634 |
return; |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
void stasis_state_publish_by_id(struct stasis_state_manager *manager, const char *id, |
| 640 |
const struct ast_eid *eid, struct stasis_message *msg) |
| 641 |
{ |
| 642 |
struct stasis_state *state; |
| 643 |
|
| 644 |
state = state_find_or_add(manager, NULL, id); |
| 645 |
if (!state) { |
| 646 |
return; |
| 647 |
} |
| 648 |
|
| 649 |
ao2_lock(state); |
| 650 |
state_find_or_add_eid(state, eid); |
| 651 |
ao2_replace(state->msg, msg); |
| 652 |
ao2_unlock(state); |
| 653 |
|
| 654 |
stasis_publish(state->topic, msg); |
| 655 |
|
| 656 |
ao2_ref(state, -1); |
| 657 |
} |
| 658 |
|
| 659 |
void stasis_state_remove_publish_by_id(struct stasis_state_manager *manager, |
| 660 |
const char *id, const struct ast_eid *eid, struct stasis_message *msg) |
| 661 |
{ |
| 662 |
struct stasis_state *state = ao2_weakproxy_find(manager->states, id, OBJ_SEARCH_KEY, ""); |
| 663 |
|
| 664 |
if (!state) { |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
ast_debug(5, "Attempted to remove state for id '%s', but state not found\n", id); |
| 675 |
return; |
| 676 |
} |
| 677 |
|
| 678 |
if (msg) { |
| 679 |
stasis_publish(state->topic, msg); |
| 680 |
} |
| 681 |
|
| 682 |
ao2_lock(state); |
| 683 |
state_find_and_remove_eid(state, eid); |
| 684 |
ao2_unlock(state); |
| 685 |
|
| 686 |
ao2_ref(state, -1); |
| 687 |
} |
| 688 |
|
| 689 |
int stasis_state_add_observer(struct stasis_state_manager *manager, |
| 690 |
struct stasis_state_observer *observer) |
| 691 |
{ |
| 692 |
int res; |
| 693 |
|
| 694 |
AST_VECTOR_RW_WRLOCK(&manager->observers); |
| 695 |
res = AST_VECTOR_APPEND(&manager->observers, observer); |
| 696 |
AST_VECTOR_RW_UNLOCK(&manager->observers); |
| 697 |
|
| 698 |
return res; |
| 699 |
} |
| 700 |
|
| 701 |
void stasis_state_remove_observer(struct stasis_state_manager *manager, |
| 702 |
struct stasis_state_observer *observer) |
| 703 |
{ |
| 704 |
AST_VECTOR_RW_WRLOCK(&manager->observers); |
| 705 |
AST_VECTOR_REMOVE_ELEM_UNORDERED(&manager->observers, observer, AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 706 |
AST_VECTOR_RW_UNLOCK(&manager->observers); |
| 707 |
} |
| 708 |
|
| 709 |
static int handle_stasis_state(struct stasis_state *state, on_stasis_state handler, void *data) |
| 710 |
{ |
| 711 |
struct stasis_message *msg; |
| 712 |
int res; |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
ao2_lock(state); |
| 719 |
msg = ao2_bump(state->msg); |
| 720 |
ao2_unlock(state); |
| 721 |
|
| 722 |
res = handler(state->id, msg, data); |
| 723 |
ao2_cleanup(msg); |
| 724 |
return res; |
| 725 |
} |
| 726 |
|
| 727 |
static int handle_stasis_state_proxy(void *obj, void *arg, void *data, int flags) |
| 728 |
{ |
| 729 |
struct stasis_state *state = ao2_weakproxy_get_object(obj, 0); |
| 730 |
|
| 731 |
if (state) { |
| 732 |
int res; |
| 733 |
res = handle_stasis_state(state, arg, data); |
| 734 |
ao2_ref(state, -1); |
| 735 |
return res; |
| 736 |
} |
| 737 |
|
| 738 |
return 0; |
| 739 |
} |
| 740 |
|
| 741 |
void stasis_state_callback_all(struct stasis_state_manager *manager, on_stasis_state handler, |
| 742 |
void *data) |
| 743 |
{ |
| 744 |
ast_assert(handler != NULL); |
| 745 |
|
| 746 |
ao2_callback_data(manager->states, OBJ_MULTIPLE | OBJ_NODATA, |
| 747 |
handle_stasis_state_proxy, handler, data); |
| 748 |
} |
| 749 |
|
| 750 |
static int handle_stasis_state_subscribed(void *obj, void *arg, void *data, int flags) |
| 751 |
{ |
| 752 |
struct stasis_state *state = ao2_weakproxy_get_object(obj, 0); |
| 753 |
int res = 0; |
| 754 |
|
| 755 |
if (state && state->num_subscribers) { |
| 756 |
res = handle_stasis_state(state, arg, data); |
| 757 |
} |
| 758 |
|
| 759 |
ao2_cleanup(state); |
| 760 |
|
| 761 |
return res; |
| 762 |
} |
| 763 |
|
| 764 |
void stasis_state_callback_subscribed(struct stasis_state_manager *manager, on_stasis_state handler, |
| 765 |
void *data) |
| 766 |
{ |
| 767 |
ast_assert(handler != NULL); |
| 768 |
|
| 769 |
ao2_callback_data(manager->states, OBJ_MULTIPLE | OBJ_NODATA, |
| 770 |
handle_stasis_state_subscribed, handler, data); |
| 771 |
} |