| 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 "app.h" |
| 29 |
#include "control.h" |
| 30 |
#include "messaging.h" |
| 31 |
|
| 32 |
#include "asterisk/callerid.h" |
| 33 |
#include "asterisk/cli.h" |
| 34 |
#include "asterisk/stasis_app.h" |
| 35 |
#include "asterisk/stasis_bridges.h" |
| 36 |
#include "asterisk/stasis_channels.h" |
| 37 |
#include "asterisk/stasis_endpoints.h" |
| 38 |
#include "asterisk/stasis_message_router.h" |
| 39 |
|
| 40 |
#define BRIDGE_ALL "__AST_BRIDGE_ALL_TOPIC" |
| 41 |
#define CHANNEL_ALL "__AST_CHANNEL_ALL_TOPIC" |
| 42 |
#define ENDPOINT_ALL "__AST_ENDPOINT_ALL_TOPIC" |
| 43 |
|
| 44 |
|
| 45 |
int global_debug; |
| 46 |
|
| 47 |
static int unsubscribe(struct stasis_app *app, const char *kind, const char *id, int terminate); |
| 48 |
|
| 49 |
struct stasis_app { |
| 50 |
|
| 51 |
struct stasis_topic *topic; |
| 52 |
|
| 53 |
struct stasis_message_router *router; |
| 54 |
|
| 55 |
struct stasis_message_router *bridge_router; |
| 56 |
|
| 57 |
struct stasis_message_router *endpoint_router; |
| 58 |
|
| 59 |
struct ao2_container *forwards; |
| 60 |
|
| 61 |
stasis_app_cb handler; |
| 62 |
|
| 63 |
void *data; |
| 64 |
|
| 65 |
enum stasis_app_subscription_model subscription_model; |
| 66 |
|
| 67 |
int debug; |
| 68 |
|
| 69 |
struct ast_json *events_allowed; |
| 70 |
|
| 71 |
struct ast_json *events_disallowed; |
| 72 |
|
| 73 |
char name[]; |
| 74 |
}; |
| 75 |
|
| 76 |
enum forward_type { |
| 77 |
FORWARD_CHANNEL, |
| 78 |
FORWARD_BRIDGE, |
| 79 |
FORWARD_ENDPOINT, |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
struct app_forwards { |
| 84 |
|
| 85 |
int interested; |
| 86 |
|
| 87 |
|
| 88 |
struct stasis_forward *topic_forward; |
| 89 |
|
| 90 |
struct stasis_forward *topic_cached_forward; |
| 91 |
|
| 92 |
|
| 93 |
enum forward_type forward_type; |
| 94 |
|
| 95 |
char id[]; |
| 96 |
}; |
| 97 |
|
| 98 |
static void forwards_dtor(void *obj) |
| 99 |
{ |
| 100 |
#ifdef AST_DEVMODE |
| 101 |
struct app_forwards *forwards = obj; |
| 102 |
#endif |
| 103 |
|
| 104 |
ast_assert(forwards->topic_forward == NULL); |
| 105 |
ast_assert(forwards->topic_cached_forward == NULL); |
| 106 |
} |
| 107 |
|
| 108 |
static void forwards_unsubscribe(struct app_forwards *forwards) |
| 109 |
{ |
| 110 |
stasis_forward_cancel(forwards->topic_forward); |
| 111 |
forwards->topic_forward = NULL; |
| 112 |
stasis_forward_cancel(forwards->topic_cached_forward); |
| 113 |
forwards->topic_cached_forward = NULL; |
| 114 |
} |
| 115 |
|
| 116 |
static struct app_forwards *forwards_create(struct stasis_app *app, |
| 117 |
const char *id) |
| 118 |
{ |
| 119 |
struct app_forwards *forwards; |
| 120 |
|
| 121 |
if (!app || ast_strlen_zero(id)) { |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
|
| 125 |
forwards = ao2_t_alloc(sizeof(*forwards) + strlen(id) + 1, forwards_dtor, id); |
| 126 |
if (!forwards) { |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
|
| 130 |
strcpy(forwards->id, id); |
| 131 |
|
| 132 |
return forwards; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
static struct app_forwards *forwards_create_channel(struct stasis_app *app, |
| 137 |
struct ast_channel *chan) |
| 138 |
{ |
| 139 |
struct app_forwards *forwards; |
| 140 |
|
| 141 |
if (!app) { |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
|
| 145 |
forwards = forwards_create(app, chan ? ast_channel_uniqueid(chan) : CHANNEL_ALL); |
| 146 |
if (!forwards) { |
| 147 |
return NULL; |
| 148 |
} |
| 149 |
|
| 150 |
forwards->forward_type = FORWARD_CHANNEL; |
| 151 |
forwards->topic_forward = stasis_forward_all( |
| 152 |
chan ? ast_channel_topic(chan) : ast_channel_topic_all(), |
| 153 |
app->topic); |
| 154 |
|
| 155 |
if (!forwards->topic_forward) { |
| 156 |
ao2_ref(forwards, -1); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
return forwards; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
static struct app_forwards *forwards_create_bridge(struct stasis_app *app, |
| 165 |
struct ast_bridge *bridge) |
| 166 |
{ |
| 167 |
struct app_forwards *forwards; |
| 168 |
|
| 169 |
if (!app) { |
| 170 |
return NULL; |
| 171 |
} |
| 172 |
|
| 173 |
forwards = forwards_create(app, bridge ? bridge->uniqueid : BRIDGE_ALL); |
| 174 |
if (!forwards) { |
| 175 |
return NULL; |
| 176 |
} |
| 177 |
|
| 178 |
forwards->forward_type = FORWARD_BRIDGE; |
| 179 |
forwards->topic_forward = stasis_forward_all(ast_bridge_topic(bridge), app->topic); |
| 180 |
|
| 181 |
if (!forwards->topic_forward && bridge) { |
| 182 |
forwards_unsubscribe(forwards); |
| 183 |
ao2_ref(forwards, -1); |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
return forwards; |
| 188 |
} |
| 189 |
|
| 190 |
static void endpoint_state_cb(void *data, struct stasis_subscription *sub, |
| 191 |
struct stasis_message *message) |
| 192 |
{ |
| 193 |
struct stasis_app *app = data; |
| 194 |
|
| 195 |
stasis_publish(app->topic, message); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
static struct app_forwards *forwards_create_endpoint(struct stasis_app *app, |
| 200 |
struct ast_endpoint *endpoint) |
| 201 |
{ |
| 202 |
struct app_forwards *forwards; |
| 203 |
int ret = 0; |
| 204 |
|
| 205 |
if (!app) { |
| 206 |
return NULL; |
| 207 |
} |
| 208 |
|
| 209 |
forwards = forwards_create(app, endpoint ? ast_endpoint_get_id(endpoint) : ENDPOINT_ALL); |
| 210 |
if (!forwards) { |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
|
| 214 |
forwards->forward_type = FORWARD_ENDPOINT; |
| 215 |
if (endpoint) { |
| 216 |
forwards->topic_forward = stasis_forward_all(ast_endpoint_topic(endpoint), |
| 217 |
app->topic); |
| 218 |
forwards->topic_cached_forward = stasis_forward_all( |
| 219 |
ast_endpoint_topic_cached(endpoint), app->topic); |
| 220 |
|
| 221 |
if (!forwards->topic_forward || !forwards->topic_cached_forward) { |
| 222 |
|
| 223 |
forwards_unsubscribe(forwards); |
| 224 |
ao2_ref(forwards, -1); |
| 225 |
return NULL; |
| 226 |
} |
| 227 |
} else { |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
ast_assert(app->endpoint_router == NULL); |
| 234 |
app->endpoint_router = stasis_message_router_create(ast_endpoint_topic_all_cached()); |
| 235 |
if (!app->endpoint_router) { |
| 236 |
forwards_unsubscribe(forwards); |
| 237 |
ao2_ref(forwards, -1); |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
ret |= stasis_message_router_add(app->endpoint_router, |
| 242 |
ast_endpoint_state_type(), endpoint_state_cb, app); |
| 243 |
ret |= stasis_message_router_add(app->endpoint_router, |
| 244 |
ast_endpoint_contact_state_type(), endpoint_state_cb, app); |
| 245 |
|
| 246 |
if (ret) { |
| 247 |
ao2_ref(app->endpoint_router, -1); |
| 248 |
app->endpoint_router = NULL; |
| 249 |
ao2_ref(forwards, -1); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return forwards; |
| 255 |
} |
| 256 |
|
| 257 |
static int forwards_sort(const void *obj_left, const void *obj_right, int flags) |
| 258 |
{ |
| 259 |
const struct app_forwards *object_left = obj_left; |
| 260 |
const struct app_forwards *object_right = obj_right; |
| 261 |
const char *right_key = obj_right; |
| 262 |
int cmp; |
| 263 |
|
| 264 |
switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) { |
| 265 |
case OBJ_POINTER: |
| 266 |
right_key = object_right->id; |
| 267 |
|
| 268 |
case OBJ_KEY: |
| 269 |
cmp = strcmp(object_left->id, right_key); |
| 270 |
break; |
| 271 |
case OBJ_PARTIAL_KEY: |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
cmp = strncmp(object_left->id, right_key, strlen(right_key)); |
| 277 |
break; |
| 278 |
default: |
| 279 |
|
| 280 |
ast_assert(0); |
| 281 |
cmp = 0; |
| 282 |
break; |
| 283 |
} |
| 284 |
return cmp; |
| 285 |
} |
| 286 |
|
| 287 |
static void app_dtor(void *obj) |
| 288 |
{ |
| 289 |
struct stasis_app *app = obj; |
| 290 |
size_t size = strlen("stasis-") + strlen(app->name) + 1; |
| 291 |
char context_name[size]; |
| 292 |
|
| 293 |
ast_verb(1, "Destroying Stasis app %s\n", app->name); |
| 294 |
|
| 295 |
ast_assert(app->router == NULL); |
| 296 |
ast_assert(app->bridge_router == NULL); |
| 297 |
ast_assert(app->endpoint_router == NULL); |
| 298 |
|
| 299 |
|
| 300 |
strcpy(context_name, "stasis-"); |
| 301 |
strcat(context_name, app->name); |
| 302 |
ast_context_destroy_by_name(context_name, "res_stasis"); |
| 303 |
|
| 304 |
ao2_cleanup(app->topic); |
| 305 |
app->topic = NULL; |
| 306 |
ao2_cleanup(app->forwards); |
| 307 |
app->forwards = NULL; |
| 308 |
ao2_cleanup(app->data); |
| 309 |
app->data = NULL; |
| 310 |
|
| 311 |
ast_json_unref(app->events_allowed); |
| 312 |
app->events_allowed = NULL; |
| 313 |
ast_json_unref(app->events_disallowed); |
| 314 |
app->events_disallowed = NULL; |
| 315 |
|
| 316 |
} |
| 317 |
|
| 318 |
static void call_forwarded_handler(struct stasis_app *app, struct stasis_message *message) |
| 319 |
{ |
| 320 |
struct ast_multi_channel_blob *payload = stasis_message_data(message); |
| 321 |
struct ast_channel_snapshot *snapshot = ast_multi_channel_blob_get_channel(payload, "forwarded"); |
| 322 |
struct ast_channel *chan; |
| 323 |
|
| 324 |
if (!snapshot) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
chan = ast_channel_get_by_name(snapshot->base->uniqueid); |
| 329 |
if (!chan) { |
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
app_subscribe_channel(app, chan); |
| 334 |
ast_channel_unref(chan); |
| 335 |
} |
| 336 |
|
| 337 |
static void sub_subscription_change_handler(void *data, struct stasis_subscription *sub, |
| 338 |
struct stasis_message *message) |
| 339 |
{ |
| 340 |
struct stasis_app *app = data; |
| 341 |
|
| 342 |
if (stasis_subscription_final_message(sub, message)) { |
| 343 |
ao2_cleanup(app); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
static void sub_default_handler(void *data, struct stasis_subscription *sub, |
| 348 |
struct stasis_message *message) |
| 349 |
{ |
| 350 |
struct stasis_app *app = data; |
| 351 |
struct ast_json *json; |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
if (stasis_message_type(message) == ast_channel_dial_type()) { |
| 357 |
call_forwarded_handler(app, message); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
json = stasis_message_to_json(message, stasis_app_get_sanitizer()); |
| 362 |
if (!json) { |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
app_send(app, json); |
| 367 |
ast_json_unref(json); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
typedef struct ast_json *(*channel_snapshot_monitor)( |
| 372 |
struct ast_channel_snapshot *old_snapshot, |
| 373 |
struct ast_channel_snapshot *new_snapshot, |
| 374 |
const struct timeval *tv); |
| 375 |
|
| 376 |
static struct ast_json *simple_channel_event( |
| 377 |
const char *type, |
| 378 |
struct ast_channel_snapshot *snapshot, |
| 379 |
const struct timeval *tv) |
| 380 |
{ |
| 381 |
struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, stasis_app_get_sanitizer()); |
| 382 |
|
| 383 |
if (!json_channel) { |
| 384 |
return NULL; |
| 385 |
} |
| 386 |
|
| 387 |
return ast_json_pack("{s: s, s: o, s: o}", |
| 388 |
"type", type, |
| 389 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 390 |
"channel", json_channel); |
| 391 |
} |
| 392 |
|
| 393 |
static struct ast_json *channel_created_event( |
| 394 |
struct ast_channel_snapshot *snapshot, |
| 395 |
const struct timeval *tv) |
| 396 |
{ |
| 397 |
return simple_channel_event("ChannelCreated", snapshot, tv); |
| 398 |
} |
| 399 |
|
| 400 |
static struct ast_json *channel_destroyed_event( |
| 401 |
struct ast_channel_snapshot *snapshot, |
| 402 |
const struct timeval *tv) |
| 403 |
{ |
| 404 |
struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, stasis_app_get_sanitizer()); |
| 405 |
struct ast_json *blob; |
| 406 |
|
| 407 |
if (!json_channel) { |
| 408 |
return NULL; |
| 409 |
} |
| 410 |
|
| 411 |
blob = ast_json_pack("{s: s, s: o, s: i, s: s}", |
| 412 |
"type", "ChannelDestroyed", |
| 413 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 414 |
"cause", snapshot->hangup->cause, |
| 415 |
"cause_txt", ast_cause2str(snapshot->hangup->cause)); |
| 416 |
|
| 417 |
if (!blob) { |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
|
| 421 |
if (snapshot->hangup->tech_cause) { |
| 422 |
ast_json_object_set(blob, "tech_cause", |
| 423 |
ast_json_integer_create(snapshot->hangup->tech_cause)); |
| 424 |
} |
| 425 |
|
| 426 |
ast_json_object_set(blob, "channel", json_channel); |
| 427 |
|
| 428 |
return blob; |
| 429 |
} |
| 430 |
|
| 431 |
static struct ast_json *channel_state_change_event( |
| 432 |
struct ast_channel_snapshot *snapshot, |
| 433 |
const struct timeval *tv) |
| 434 |
{ |
| 435 |
return simple_channel_event("ChannelStateChange", snapshot, tv); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
static struct ast_json *channel_state( |
| 446 |
struct ast_channel_snapshot *old_snapshot, |
| 447 |
struct ast_channel_snapshot *new_snapshot, |
| 448 |
const struct timeval *tv) |
| 449 |
{ |
| 450 |
|
| 451 |
if (!old_snapshot) { |
| 452 |
return channel_created_event(new_snapshot, tv); |
| 453 |
} else if (ast_test_flag(&new_snapshot->flags, AST_FLAG_DEAD)) { |
| 454 |
return channel_destroyed_event(new_snapshot, tv); |
| 455 |
} else if (old_snapshot->state != new_snapshot->state) { |
| 456 |
return channel_state_change_event(new_snapshot, tv); |
| 457 |
} |
| 458 |
|
| 459 |
return NULL; |
| 460 |
} |
| 461 |
|
| 462 |
static struct ast_json *channel_dialplan( |
| 463 |
struct ast_channel_snapshot *old_snapshot, |
| 464 |
struct ast_channel_snapshot *new_snapshot, |
| 465 |
const struct timeval *tv) |
| 466 |
{ |
| 467 |
struct ast_json *json_channel; |
| 468 |
|
| 469 |
|
| 470 |
if (!old_snapshot) { |
| 471 |
return NULL; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
if (ast_strlen_zero(new_snapshot->dialplan->appl)) { |
| 476 |
return NULL; |
| 477 |
} |
| 478 |
|
| 479 |
if (ast_channel_snapshot_cep_equal(old_snapshot, new_snapshot)) { |
| 480 |
return NULL; |
| 481 |
} |
| 482 |
|
| 483 |
json_channel = ast_channel_snapshot_to_json(new_snapshot, stasis_app_get_sanitizer()); |
| 484 |
if (!json_channel) { |
| 485 |
return NULL; |
| 486 |
} |
| 487 |
|
| 488 |
return ast_json_pack("{s: s, s: o, s: s, s: s, s: o}", |
| 489 |
"type", "ChannelDialplan", |
| 490 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 491 |
"dialplan_app", new_snapshot->dialplan->appl, |
| 492 |
"dialplan_app_data", AST_JSON_UTF8_VALIDATE(new_snapshot->dialplan->data), |
| 493 |
"channel", json_channel); |
| 494 |
} |
| 495 |
|
| 496 |
static struct ast_json *channel_callerid( |
| 497 |
struct ast_channel_snapshot *old_snapshot, |
| 498 |
struct ast_channel_snapshot *new_snapshot, |
| 499 |
const struct timeval *tv) |
| 500 |
{ |
| 501 |
struct ast_json *json_channel; |
| 502 |
|
| 503 |
|
| 504 |
if (!old_snapshot) { |
| 505 |
return NULL; |
| 506 |
} |
| 507 |
|
| 508 |
if (ast_channel_snapshot_caller_id_equal(old_snapshot, new_snapshot)) { |
| 509 |
return NULL; |
| 510 |
} |
| 511 |
|
| 512 |
json_channel = ast_channel_snapshot_to_json(new_snapshot, stasis_app_get_sanitizer()); |
| 513 |
if (!json_channel) { |
| 514 |
return NULL; |
| 515 |
} |
| 516 |
|
| 517 |
return ast_json_pack("{s: s, s: o, s: i, s: s, s: o}", |
| 518 |
"type", "ChannelCallerId", |
| 519 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 520 |
"caller_presentation", new_snapshot->caller->pres, |
| 521 |
"caller_presentation_txt", ast_describe_caller_presentation( |
| 522 |
new_snapshot->caller->pres), |
| 523 |
"channel", json_channel); |
| 524 |
} |
| 525 |
|
| 526 |
static struct ast_json *channel_connected_line( |
| 527 |
struct ast_channel_snapshot *old_snapshot, |
| 528 |
struct ast_channel_snapshot *new_snapshot, |
| 529 |
const struct timeval *tv) |
| 530 |
{ |
| 531 |
struct ast_json *json_channel; |
| 532 |
|
| 533 |
|
| 534 |
if (!old_snapshot) { |
| 535 |
return NULL; |
| 536 |
} |
| 537 |
|
| 538 |
if (ast_channel_snapshot_connected_line_equal(old_snapshot, new_snapshot)) { |
| 539 |
return NULL; |
| 540 |
} |
| 541 |
|
| 542 |
json_channel = ast_channel_snapshot_to_json(new_snapshot, stasis_app_get_sanitizer()); |
| 543 |
if (!json_channel) { |
| 544 |
return NULL; |
| 545 |
} |
| 546 |
|
| 547 |
return ast_json_pack("{s: s, s: o, s: o}", |
| 548 |
"type", "ChannelConnectedLine", |
| 549 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 550 |
"channel", json_channel); |
| 551 |
} |
| 552 |
|
| 553 |
static channel_snapshot_monitor channel_monitors[] = { |
| 554 |
channel_state, |
| 555 |
channel_dialplan, |
| 556 |
channel_callerid, |
| 557 |
channel_connected_line, |
| 558 |
}; |
| 559 |
|
| 560 |
static void sub_channel_update_handler(void *data, |
| 561 |
struct stasis_subscription *sub, |
| 562 |
struct stasis_message *message) |
| 563 |
{ |
| 564 |
struct stasis_app *app = data; |
| 565 |
struct ast_channel_snapshot_update *update = stasis_message_data(message); |
| 566 |
int i; |
| 567 |
|
| 568 |
for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) { |
| 569 |
struct ast_json *msg; |
| 570 |
|
| 571 |
msg = channel_monitors[i](update->old_snapshot, update->new_snapshot, |
| 572 |
stasis_message_timestamp(message)); |
| 573 |
if (msg) { |
| 574 |
app_send(app, msg); |
| 575 |
ast_json_unref(msg); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
if (ast_test_flag(&update->new_snapshot->flags, AST_FLAG_DEAD)) { |
| 580 |
unsubscribe(app, "channel", update->new_snapshot->base->uniqueid, 1); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
static struct ast_json *simple_endpoint_event( |
| 585 |
const char *type, |
| 586 |
struct ast_endpoint_snapshot *snapshot, |
| 587 |
const struct timeval *tv) |
| 588 |
{ |
| 589 |
struct ast_json *json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer()); |
| 590 |
|
| 591 |
if (!json_endpoint) { |
| 592 |
return NULL; |
| 593 |
} |
| 594 |
|
| 595 |
return ast_json_pack("{s: s, s: o, s: o}", |
| 596 |
"type", type, |
| 597 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 598 |
"endpoint", json_endpoint); |
| 599 |
} |
| 600 |
|
| 601 |
static int message_received_handler(const char *endpoint_id, struct ast_json *json_msg, void *pvt) |
| 602 |
{ |
| 603 |
struct ast_endpoint_snapshot *snapshot; |
| 604 |
struct ast_json *json_endpoint; |
| 605 |
struct ast_json *message; |
| 606 |
struct stasis_app *app = pvt; |
| 607 |
char *tech; |
| 608 |
char *resource; |
| 609 |
|
| 610 |
tech = ast_strdupa(endpoint_id); |
| 611 |
resource = strchr(tech, '/'); |
| 612 |
if (resource) { |
| 613 |
resource[0] = '\0'; |
| 614 |
resource++; |
| 615 |
} |
| 616 |
|
| 617 |
if (ast_strlen_zero(tech) || ast_strlen_zero(resource)) { |
| 618 |
return -1; |
| 619 |
} |
| 620 |
|
| 621 |
snapshot = ast_endpoint_latest_snapshot(tech, resource); |
| 622 |
if (!snapshot) { |
| 623 |
return -1; |
| 624 |
} |
| 625 |
|
| 626 |
json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer()); |
| 627 |
ao2_ref(snapshot, -1); |
| 628 |
if (!json_endpoint) { |
| 629 |
return -1; |
| 630 |
} |
| 631 |
|
| 632 |
message = ast_json_pack("{s: s, s: o, s: o, s: o}", |
| 633 |
"type", "TextMessageReceived", |
| 634 |
"timestamp", ast_json_timeval(ast_tvnow(), NULL), |
| 635 |
"endpoint", json_endpoint, |
| 636 |
"message", ast_json_ref(json_msg)); |
| 637 |
if (message) { |
| 638 |
app_send(app, message); |
| 639 |
ast_json_unref(message); |
| 640 |
} |
| 641 |
|
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
static void sub_endpoint_update_handler(void *data, |
| 646 |
struct stasis_subscription *sub, |
| 647 |
struct stasis_message *message) |
| 648 |
{ |
| 649 |
struct stasis_app *app = data; |
| 650 |
struct stasis_cache_update *update; |
| 651 |
struct ast_endpoint_snapshot *new_snapshot; |
| 652 |
struct ast_endpoint_snapshot *old_snapshot; |
| 653 |
const struct timeval *tv; |
| 654 |
|
| 655 |
ast_assert(stasis_message_type(message) == stasis_cache_update_type()); |
| 656 |
|
| 657 |
update = stasis_message_data(message); |
| 658 |
|
| 659 |
ast_assert(update->type == ast_endpoint_snapshot_type()); |
| 660 |
|
| 661 |
new_snapshot = stasis_message_data(update->new_snapshot); |
| 662 |
old_snapshot = stasis_message_data(update->old_snapshot); |
| 663 |
|
| 664 |
if (new_snapshot) { |
| 665 |
struct ast_json *json; |
| 666 |
|
| 667 |
tv = stasis_message_timestamp(update->new_snapshot); |
| 668 |
|
| 669 |
json = simple_endpoint_event("EndpointStateChange", new_snapshot, tv); |
| 670 |
if (!json) { |
| 671 |
return; |
| 672 |
} |
| 673 |
|
| 674 |
app_send(app, json); |
| 675 |
ast_json_unref(json); |
| 676 |
} |
| 677 |
|
| 678 |
if (!new_snapshot && old_snapshot) { |
| 679 |
unsubscribe(app, "endpoint", old_snapshot->id, 1); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
static struct ast_json *simple_bridge_event( |
| 684 |
const char *type, |
| 685 |
struct ast_bridge_snapshot *snapshot, |
| 686 |
const struct timeval *tv) |
| 687 |
{ |
| 688 |
struct ast_json *json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()); |
| 689 |
if (!json_bridge) { |
| 690 |
return NULL; |
| 691 |
} |
| 692 |
|
| 693 |
return ast_json_pack("{s: s, s: o, s: o}", |
| 694 |
"type", type, |
| 695 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 696 |
"bridge", json_bridge); |
| 697 |
} |
| 698 |
|
| 699 |
static void sub_bridge_update_handler(void *data, |
| 700 |
struct stasis_subscription *sub, |
| 701 |
struct stasis_message *message) |
| 702 |
{ |
| 703 |
struct ast_json *json = NULL; |
| 704 |
struct stasis_app *app = data; |
| 705 |
struct ast_bridge_snapshot_update *update; |
| 706 |
const struct timeval *tv; |
| 707 |
|
| 708 |
update = stasis_message_data(message); |
| 709 |
|
| 710 |
tv = stasis_message_timestamp(message); |
| 711 |
|
| 712 |
if (!update->new_snapshot) { |
| 713 |
json = simple_bridge_event("BridgeDestroyed", update->old_snapshot, tv); |
| 714 |
} else if (!update->old_snapshot) { |
| 715 |
json = simple_bridge_event("BridgeCreated", update->new_snapshot, tv); |
| 716 |
} else if (update->new_snapshot && update->old_snapshot |
| 717 |
&& strcmp(update->new_snapshot->video_source_id, update->old_snapshot->video_source_id)) { |
| 718 |
json = simple_bridge_event("BridgeVideoSourceChanged", update->new_snapshot, tv); |
| 719 |
if (json && !ast_strlen_zero(update->old_snapshot->video_source_id)) { |
| 720 |
ast_json_object_set(json, "old_video_source_id", |
| 721 |
ast_json_string_create(update->old_snapshot->video_source_id)); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
if (json) { |
| 726 |
app_send(app, json); |
| 727 |
ast_json_unref(json); |
| 728 |
} |
| 729 |
|
| 730 |
if (!update->new_snapshot && update->old_snapshot) { |
| 731 |
unsubscribe(app, "bridge", update->old_snapshot->uniqueid, 1); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
static int bridge_app_subscribed(struct stasis_app *app, const char *uniqueid) |
| 738 |
{ |
| 739 |
struct app_forwards *forwards = NULL; |
| 740 |
|
| 741 |
forwards = ao2_find(app->forwards, uniqueid, OBJ_SEARCH_KEY); |
| 742 |
if (!forwards) { |
| 743 |
return 0; |
| 744 |
} |
| 745 |
|
| 746 |
ao2_ref(forwards, -1); |
| 747 |
return 1; |
| 748 |
} |
| 749 |
|
| 750 |
static void bridge_merge_handler(void *data, struct stasis_subscription *sub, |
| 751 |
struct stasis_message *message) |
| 752 |
{ |
| 753 |
struct stasis_app *app = data; |
| 754 |
struct ast_bridge_merge_message *merge; |
| 755 |
|
| 756 |
merge = stasis_message_data(message); |
| 757 |
|
| 758 |
|
| 759 |
if (bridge_app_subscribed(app, merge->from->uniqueid) || |
| 760 |
bridge_app_subscribed(app, merge->to->uniqueid)) { |
| 761 |
|
| 762 |
stasis_publish(app->topic, message); |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
static int bridge_app_subscribed_involved(struct stasis_app *app, struct ast_bridge_snapshot *snapshot) |
| 768 |
{ |
| 769 |
int subscribed = 0; |
| 770 |
struct ao2_iterator iter; |
| 771 |
char *uniqueid; |
| 772 |
|
| 773 |
if (bridge_app_subscribed(app, snapshot->uniqueid)) { |
| 774 |
return 1; |
| 775 |
} |
| 776 |
|
| 777 |
iter = ao2_iterator_init(snapshot->channels, 0); |
| 778 |
for (; (uniqueid = ao2_iterator_next(&iter)); ao2_ref(uniqueid, -1)) { |
| 779 |
if (bridge_app_subscribed(app, uniqueid)) { |
| 780 |
subscribed = 1; |
| 781 |
ao2_ref(uniqueid, -1); |
| 782 |
break; |
| 783 |
} |
| 784 |
} |
| 785 |
ao2_iterator_destroy(&iter); |
| 786 |
|
| 787 |
return subscribed; |
| 788 |
} |
| 789 |
|
| 790 |
static void bridge_blind_transfer_handler(void *data, struct stasis_subscription *sub, |
| 791 |
struct stasis_message *message) |
| 792 |
{ |
| 793 |
struct stasis_app *app = data; |
| 794 |
struct ast_blind_transfer_message *transfer_msg = stasis_message_data(message); |
| 795 |
struct ast_bridge_snapshot *bridge = transfer_msg->bridge; |
| 796 |
|
| 797 |
if (bridge_app_subscribed(app, transfer_msg->transferer->base->uniqueid) || |
| 798 |
(bridge && bridge_app_subscribed_involved(app, bridge))) { |
| 799 |
stasis_publish(app->topic, message); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
static void bridge_attended_transfer_handler(void *data, struct stasis_subscription *sub, |
| 804 |
struct stasis_message *message) |
| 805 |
{ |
| 806 |
struct stasis_app *app = data; |
| 807 |
struct ast_attended_transfer_message *transfer_msg = stasis_message_data(message); |
| 808 |
int subscribed = 0; |
| 809 |
|
| 810 |
subscribed = bridge_app_subscribed(app, transfer_msg->to_transferee.channel_snapshot->base->uniqueid); |
| 811 |
if (!subscribed) { |
| 812 |
subscribed = bridge_app_subscribed(app, transfer_msg->to_transfer_target.channel_snapshot->base->uniqueid); |
| 813 |
} |
| 814 |
if (!subscribed && transfer_msg->to_transferee.bridge_snapshot) { |
| 815 |
subscribed = bridge_app_subscribed_involved(app, transfer_msg->to_transferee.bridge_snapshot); |
| 816 |
} |
| 817 |
if (!subscribed && transfer_msg->to_transfer_target.bridge_snapshot) { |
| 818 |
subscribed = bridge_app_subscribed_involved(app, transfer_msg->to_transfer_target.bridge_snapshot); |
| 819 |
} |
| 820 |
|
| 821 |
if (!subscribed) { |
| 822 |
switch (transfer_msg->dest_type) { |
| 823 |
case AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE: |
| 824 |
subscribed = bridge_app_subscribed(app, transfer_msg->dest.bridge); |
| 825 |
break; |
| 826 |
case AST_ATTENDED_TRANSFER_DEST_LINK: |
| 827 |
subscribed = bridge_app_subscribed(app, transfer_msg->dest.links[0]->base->uniqueid); |
| 828 |
if (!subscribed) { |
| 829 |
subscribed = bridge_app_subscribed(app, transfer_msg->dest.links[1]->base->uniqueid); |
| 830 |
} |
| 831 |
break; |
| 832 |
break; |
| 833 |
case AST_ATTENDED_TRANSFER_DEST_THREEWAY: |
| 834 |
subscribed = bridge_app_subscribed_involved(app, transfer_msg->dest.threeway.bridge_snapshot); |
| 835 |
if (!subscribed) { |
| 836 |
subscribed = bridge_app_subscribed(app, transfer_msg->dest.threeway.channel_snapshot->base->uniqueid); |
| 837 |
} |
| 838 |
break; |
| 839 |
default: |
| 840 |
break; |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
if (subscribed) { |
| 845 |
stasis_publish(app->topic, message); |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
static void bridge_subscription_change_handler(void *data, struct stasis_subscription *sub, |
| 850 |
struct stasis_message *message) |
| 851 |
{ |
| 852 |
struct stasis_app *app = data; |
| 853 |
|
| 854 |
if (stasis_subscription_final_message(sub, message)) { |
| 855 |
ao2_cleanup(app); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
void stasis_app_set_debug(struct stasis_app *app, int debug) |
| 860 |
{ |
| 861 |
if (!app) { |
| 862 |
return; |
| 863 |
} |
| 864 |
|
| 865 |
app->debug = debug; |
| 866 |
} |
| 867 |
|
| 868 |
void stasis_app_set_debug_by_name(const char *app_name, int debug) |
| 869 |
{ |
| 870 |
struct stasis_app *app = stasis_app_get_by_name(app_name); |
| 871 |
|
| 872 |
if (!app) { |
| 873 |
return; |
| 874 |
} |
| 875 |
|
| 876 |
app->debug = debug; |
| 877 |
ao2_cleanup(app); |
| 878 |
} |
| 879 |
|
| 880 |
int stasis_app_get_debug(struct stasis_app *app) |
| 881 |
{ |
| 882 |
return (app ? app->debug : 0) || global_debug; |
| 883 |
} |
| 884 |
|
| 885 |
int stasis_app_get_debug_by_name(const char *app_name) |
| 886 |
{ |
| 887 |
int debug_enabled = 0; |
| 888 |
|
| 889 |
if (global_debug) { |
| 890 |
debug_enabled = 1; |
| 891 |
} else { |
| 892 |
struct stasis_app *app = stasis_app_get_by_name(app_name); |
| 893 |
|
| 894 |
if (app) { |
| 895 |
if (app->debug) { |
| 896 |
debug_enabled = 1; |
| 897 |
} |
| 898 |
ao2_ref(app, -1); |
| 899 |
} |
| 900 |
} |
| 901 |
return debug_enabled; |
| 902 |
} |
| 903 |
|
| 904 |
void stasis_app_set_global_debug(int debug) |
| 905 |
{ |
| 906 |
global_debug = debug; |
| 907 |
if (!global_debug) { |
| 908 |
struct ao2_container *app_names = stasis_app_get_all(); |
| 909 |
struct ao2_iterator it_app_names; |
| 910 |
char *app_name; |
| 911 |
struct stasis_app *app; |
| 912 |
|
| 913 |
if (!app_names || !ao2_container_count(app_names)) { |
| 914 |
ao2_cleanup(app_names); |
| 915 |
return; |
| 916 |
} |
| 917 |
|
| 918 |
it_app_names = ao2_iterator_init(app_names, 0); |
| 919 |
while ((app_name = ao2_iterator_next(&it_app_names))) { |
| 920 |
if ((app = stasis_app_get_by_name(app_name))) { |
| 921 |
stasis_app_set_debug(app, 0); |
| 922 |
} |
| 923 |
|
| 924 |
ao2_cleanup(app_name); |
| 925 |
ao2_cleanup(app); |
| 926 |
} |
| 927 |
ao2_iterator_cleanup(&it_app_names); |
| 928 |
ao2_cleanup(app_names); |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
struct stasis_app *app_create(const char *name, stasis_app_cb handler, void *data, enum stasis_app_subscription_model subscription_model) |
| 933 |
{ |
| 934 |
RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup); |
| 935 |
size_t size; |
| 936 |
int res = 0; |
| 937 |
size_t context_size = strlen("stasis-") + strlen(name) + 1; |
| 938 |
char context_name[context_size]; |
| 939 |
char *topic_name; |
| 940 |
int ret; |
| 941 |
|
| 942 |
ast_assert(name != NULL); |
| 943 |
ast_assert(handler != NULL); |
| 944 |
|
| 945 |
ast_verb(1, "Creating Stasis app '%s'\n", name); |
| 946 |
|
| 947 |
size = sizeof(*app) + strlen(name) + 1; |
| 948 |
app = ao2_alloc_options(size, app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX); |
| 949 |
if (!app) { |
| 950 |
return NULL; |
| 951 |
} |
| 952 |
app->subscription_model = subscription_model; |
| 953 |
|
| 954 |
app->forwards = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX, |
| 955 |
AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT, |
| 956 |
forwards_sort, NULL); |
| 957 |
if (!app->forwards) { |
| 958 |
return NULL; |
| 959 |
} |
| 960 |
|
| 961 |
ret = ast_asprintf(&topic_name, "ari:application/%s", name); |
| 962 |
if (ret < 0) { |
| 963 |
return NULL; |
| 964 |
} |
| 965 |
|
| 966 |
app->topic = stasis_topic_create(topic_name); |
| 967 |
ast_free(topic_name); |
| 968 |
if (!app->topic) { |
| 969 |
return NULL; |
| 970 |
} |
| 971 |
|
| 972 |
app->bridge_router = stasis_message_router_create(ast_bridge_topic_all()); |
| 973 |
if (!app->bridge_router) { |
| 974 |
return NULL; |
| 975 |
} |
| 976 |
|
| 977 |
res |= stasis_message_router_add(app->bridge_router, |
| 978 |
ast_bridge_merge_message_type(), bridge_merge_handler, app); |
| 979 |
|
| 980 |
res |= stasis_message_router_add(app->bridge_router, |
| 981 |
ast_blind_transfer_type(), bridge_blind_transfer_handler, app); |
| 982 |
|
| 983 |
res |= stasis_message_router_add(app->bridge_router, |
| 984 |
ast_attended_transfer_type(), bridge_attended_transfer_handler, app); |
| 985 |
|
| 986 |
res |= stasis_message_router_add(app->bridge_router, |
| 987 |
stasis_subscription_change_type(), bridge_subscription_change_handler, app); |
| 988 |
|
| 989 |
if (res != 0) { |
| 990 |
return NULL; |
| 991 |
} |
| 992 |
|
| 993 |
ao2_ref(app, +1); |
| 994 |
|
| 995 |
app->router = stasis_message_router_create(app->topic); |
| 996 |
if (!app->router) { |
| 997 |
return NULL; |
| 998 |
} |
| 999 |
|
| 1000 |
res |= stasis_message_router_add(app->router, |
| 1001 |
ast_bridge_snapshot_type(), sub_bridge_update_handler, app); |
| 1002 |
|
| 1003 |
res |= stasis_message_router_add(app->router, |
| 1004 |
ast_channel_snapshot_type(), sub_channel_update_handler, app); |
| 1005 |
|
| 1006 |
res |= stasis_message_router_add_cache_update(app->router, |
| 1007 |
ast_endpoint_snapshot_type(), sub_endpoint_update_handler, app); |
| 1008 |
|
| 1009 |
res |= stasis_message_router_add(app->router, |
| 1010 |
stasis_subscription_change_type(), sub_subscription_change_handler, app); |
| 1011 |
|
| 1012 |
stasis_message_router_set_formatters_default(app->router, |
| 1013 |
sub_default_handler, app, STASIS_SUBSCRIPTION_FORMATTER_JSON); |
| 1014 |
|
| 1015 |
if (res != 0) { |
| 1016 |
return NULL; |
| 1017 |
} |
| 1018 |
|
| 1019 |
ao2_ref(app, +1); |
| 1020 |
|
| 1021 |
strncpy(app->name, name, size - sizeof(*app)); |
| 1022 |
app->handler = handler; |
| 1023 |
app->data = ao2_bump(data); |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
strcpy(context_name, "stasis-"); |
| 1028 |
strcat(context_name, name); |
| 1029 |
if (!ast_context_find(context_name)) { |
| 1030 |
if (!ast_context_find_or_create(NULL, NULL, context_name, "res_stasis")) { |
| 1031 |
ast_log(LOG_WARNING, "Could not create context '%s' for Stasis application '%s'\n", context_name, name); |
| 1032 |
} else { |
| 1033 |
ast_add_extension(context_name, 0, "_.", 1, NULL, NULL, "Stasis", ast_strdup(name), ast_free_ptr, "res_stasis"); |
| 1034 |
ast_add_extension(context_name, 0, "h", 1, NULL, NULL, "NoOp", NULL, NULL, "res_stasis"); |
| 1035 |
} |
| 1036 |
} else { |
| 1037 |
ast_log(LOG_WARNING, "Not creating context '%s' for Stasis application '%s' because it already exists\n", |
| 1038 |
context_name, name); |
| 1039 |
} |
| 1040 |
|
| 1041 |
ao2_ref(app, +1); |
| 1042 |
return app; |
| 1043 |
} |
| 1044 |
|
| 1045 |
struct stasis_topic *ast_app_get_topic(struct stasis_app *app) |
| 1046 |
{ |
| 1047 |
return app->topic; |
| 1048 |
} |
| 1049 |
|
| 1050 |
void app_send(struct stasis_app *app, struct ast_json *message) |
| 1051 |
{ |
| 1052 |
stasis_app_cb handler; |
| 1053 |
char eid[20]; |
| 1054 |
void *data; |
| 1055 |
|
| 1056 |
if (ast_json_object_set(message, "asterisk_id", ast_json_string_create( |
| 1057 |
ast_eid_to_str(eid, sizeof(eid), &ast_eid_default)))) { |
| 1058 |
ast_log(AST_LOG_WARNING, "Failed to append EID to outgoing event %s\n", |
| 1059 |
ast_json_string_get(ast_json_object_get(message, "type"))); |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
ao2_lock(app); |
| 1064 |
handler = app->handler; |
| 1065 |
data = ao2_bump(app->data); |
| 1066 |
ao2_unlock(app); |
| 1067 |
|
| 1068 |
|
| 1069 |
if (handler) { |
| 1070 |
handler(data, app->name, message); |
| 1071 |
} else { |
| 1072 |
ast_verb(3, |
| 1073 |
"Inactive Stasis app '%s' missed message\n", app->name); |
| 1074 |
} |
| 1075 |
ao2_cleanup(data); |
| 1076 |
} |
| 1077 |
|
| 1078 |
void app_deactivate(struct stasis_app *app) |
| 1079 |
{ |
| 1080 |
ao2_lock(app); |
| 1081 |
|
| 1082 |
ast_verb(1, "Deactivating Stasis app '%s'\n", app->name); |
| 1083 |
app->handler = NULL; |
| 1084 |
ao2_cleanup(app->data); |
| 1085 |
app->data = NULL; |
| 1086 |
|
| 1087 |
ao2_unlock(app); |
| 1088 |
} |
| 1089 |
|
| 1090 |
void app_shutdown(struct stasis_app *app) |
| 1091 |
{ |
| 1092 |
ao2_lock(app); |
| 1093 |
|
| 1094 |
ast_assert(app_is_finished(app)); |
| 1095 |
|
| 1096 |
stasis_message_router_unsubscribe(app->router); |
| 1097 |
app->router = NULL; |
| 1098 |
stasis_message_router_unsubscribe(app->bridge_router); |
| 1099 |
app->bridge_router = NULL; |
| 1100 |
stasis_message_router_unsubscribe(app->endpoint_router); |
| 1101 |
app->endpoint_router = NULL; |
| 1102 |
|
| 1103 |
ao2_unlock(app); |
| 1104 |
} |
| 1105 |
|
| 1106 |
int app_is_active(struct stasis_app *app) |
| 1107 |
{ |
| 1108 |
int ret; |
| 1109 |
|
| 1110 |
ao2_lock(app); |
| 1111 |
ret = app->handler != NULL; |
| 1112 |
ao2_unlock(app); |
| 1113 |
|
| 1114 |
return ret; |
| 1115 |
} |
| 1116 |
|
| 1117 |
int app_is_finished(struct stasis_app *app) |
| 1118 |
{ |
| 1119 |
int ret; |
| 1120 |
|
| 1121 |
ao2_lock(app); |
| 1122 |
ret = app->handler == NULL && ao2_container_count(app->forwards) == 0; |
| 1123 |
ao2_unlock(app); |
| 1124 |
|
| 1125 |
return ret; |
| 1126 |
} |
| 1127 |
|
| 1128 |
void app_update(struct stasis_app *app, stasis_app_cb handler, void *data) |
| 1129 |
{ |
| 1130 |
ao2_lock(app); |
| 1131 |
if (app->handler && app->data) { |
| 1132 |
struct ast_json *msg; |
| 1133 |
|
| 1134 |
ast_verb(1, "Replacing Stasis app '%s'\n", app->name); |
| 1135 |
|
| 1136 |
msg = ast_json_pack("{s: s, s: o?, s: s}", |
| 1137 |
"type", "ApplicationReplaced", |
| 1138 |
"timestamp", ast_json_timeval(ast_tvnow(), NULL), |
| 1139 |
"application", app->name); |
| 1140 |
if (msg) { |
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
ao2_unlock(app); |
| 1147 |
app_send(app, msg); |
| 1148 |
ao2_lock(app); |
| 1149 |
ast_json_unref(msg); |
| 1150 |
if (!app->handler) { |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
ao2_unlock(app); |
| 1156 |
return; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
} else { |
| 1160 |
ast_verb(1, "Activating Stasis app '%s'\n", app->name); |
| 1161 |
} |
| 1162 |
|
| 1163 |
app->handler = handler; |
| 1164 |
ao2_replace(app->data, data); |
| 1165 |
ao2_unlock(app); |
| 1166 |
} |
| 1167 |
|
| 1168 |
const char *stasis_app_name(const struct stasis_app *app) |
| 1169 |
{ |
| 1170 |
return app->name; |
| 1171 |
} |
| 1172 |
|
| 1173 |
static int forwards_filter_by_type(void *obj, void *arg, int flags) |
| 1174 |
{ |
| 1175 |
struct app_forwards *forward = obj; |
| 1176 |
enum forward_type *forward_type = arg; |
| 1177 |
|
| 1178 |
if (forward->forward_type == *forward_type) { |
| 1179 |
return CMP_MATCH; |
| 1180 |
} |
| 1181 |
|
| 1182 |
return 0; |
| 1183 |
} |
| 1184 |
|
| 1185 |
void stasis_app_to_cli(const struct stasis_app *app, struct ast_cli_args *a) |
| 1186 |
{ |
| 1187 |
struct ao2_iterator *channels; |
| 1188 |
struct ao2_iterator *endpoints; |
| 1189 |
struct ao2_iterator *bridges; |
| 1190 |
struct app_forwards *forward; |
| 1191 |
enum forward_type forward_type; |
| 1192 |
|
| 1193 |
ast_cli(a->fd, "Name: %s\n" |
| 1194 |
" Debug: %s\n" |
| 1195 |
" Subscription Model: %s\n", |
| 1196 |
app->name, |
| 1197 |
app->debug ? "Yes" : "No", |
| 1198 |
app->subscription_model == STASIS_APP_SUBSCRIBE_ALL ? |
| 1199 |
"Global Resource Subscription" : |
| 1200 |
"Application/Explicit Resource Subscription"); |
| 1201 |
ast_cli(a->fd, " Subscriptions: %d\n", ao2_container_count(app->forwards)); |
| 1202 |
|
| 1203 |
ast_cli(a->fd, " Channels:\n"); |
| 1204 |
forward_type = FORWARD_CHANNEL; |
| 1205 |
channels = ao2_callback(app->forwards, OBJ_MULTIPLE, |
| 1206 |
forwards_filter_by_type, &forward_type); |
| 1207 |
if (channels) { |
| 1208 |
while ((forward = ao2_iterator_next(channels))) { |
| 1209 |
ast_cli(a->fd, " %s (%d)\n", forward->id, forward->interested); |
| 1210 |
ao2_ref(forward, -1); |
| 1211 |
} |
| 1212 |
ao2_iterator_destroy(channels); |
| 1213 |
} |
| 1214 |
|
| 1215 |
ast_cli(a->fd, " Bridges:\n"); |
| 1216 |
forward_type = FORWARD_BRIDGE; |
| 1217 |
bridges = ao2_callback(app->forwards, OBJ_MULTIPLE, |
| 1218 |
forwards_filter_by_type, &forward_type); |
| 1219 |
if (bridges) { |
| 1220 |
while ((forward = ao2_iterator_next(bridges))) { |
| 1221 |
ast_cli(a->fd, " %s (%d)\n", forward->id, forward->interested); |
| 1222 |
ao2_ref(forward, -1); |
| 1223 |
} |
| 1224 |
ao2_iterator_destroy(bridges); |
| 1225 |
} |
| 1226 |
|
| 1227 |
ast_cli(a->fd, " Endpoints:\n"); |
| 1228 |
forward_type = FORWARD_ENDPOINT; |
| 1229 |
endpoints = ao2_callback(app->forwards, OBJ_MULTIPLE, |
| 1230 |
forwards_filter_by_type, &forward_type); |
| 1231 |
if (endpoints) { |
| 1232 |
while ((forward = ao2_iterator_next(endpoints))) { |
| 1233 |
ast_cli(a->fd, " %s (%d)\n", forward->id, forward->interested); |
| 1234 |
ao2_ref(forward, -1); |
| 1235 |
} |
| 1236 |
ao2_iterator_destroy(endpoints); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
struct ast_json *app_to_json(const struct stasis_app *app) |
| 1241 |
{ |
| 1242 |
struct ast_json *json; |
| 1243 |
struct ast_json *channels; |
| 1244 |
struct ast_json *bridges; |
| 1245 |
struct ast_json *endpoints; |
| 1246 |
struct ao2_iterator i; |
| 1247 |
struct app_forwards *forwards; |
| 1248 |
|
| 1249 |
json = ast_json_pack("{s: s, s: [], s: [], s: []}", |
| 1250 |
"name", app->name, |
| 1251 |
"channel_ids", "bridge_ids", "endpoint_ids"); |
| 1252 |
if (!json) { |
| 1253 |
return NULL; |
| 1254 |
} |
| 1255 |
channels = ast_json_object_get(json, "channel_ids"); |
| 1256 |
bridges = ast_json_object_get(json, "bridge_ids"); |
| 1257 |
endpoints = ast_json_object_get(json, "endpoint_ids"); |
| 1258 |
|
| 1259 |
i = ao2_iterator_init(app->forwards, 0); |
| 1260 |
while ((forwards = ao2_iterator_next(&i))) { |
| 1261 |
struct ast_json *array = NULL; |
| 1262 |
int append_res; |
| 1263 |
|
| 1264 |
switch (forwards->forward_type) { |
| 1265 |
case FORWARD_CHANNEL: |
| 1266 |
array = channels; |
| 1267 |
break; |
| 1268 |
case FORWARD_BRIDGE: |
| 1269 |
array = bridges; |
| 1270 |
break; |
| 1271 |
case FORWARD_ENDPOINT: |
| 1272 |
array = endpoints; |
| 1273 |
break; |
| 1274 |
} |
| 1275 |
|
| 1276 |
|
| 1277 |
append_res = ast_json_array_append(array, ast_json_string_create(forwards->id)); |
| 1278 |
ao2_ref(forwards, -1); |
| 1279 |
|
| 1280 |
if (append_res != 0) { |
| 1281 |
ast_log(LOG_ERROR, "Error building response\n"); |
| 1282 |
ao2_iterator_destroy(&i); |
| 1283 |
ast_json_unref(json); |
| 1284 |
|
| 1285 |
return NULL; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
ao2_iterator_destroy(&i); |
| 1289 |
|
| 1290 |
return json; |
| 1291 |
} |
| 1292 |
|
| 1293 |
int app_subscribe_channel(struct stasis_app *app, struct ast_channel *chan) |
| 1294 |
{ |
| 1295 |
struct app_forwards *forwards; |
| 1296 |
|
| 1297 |
if (!app) { |
| 1298 |
return -1; |
| 1299 |
} |
| 1300 |
|
| 1301 |
ao2_lock(app->forwards); |
| 1302 |
|
| 1303 |
forwards = ao2_find(app->forwards, CHANNEL_ALL, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 1304 |
if (forwards) { |
| 1305 |
ao2_unlock(app->forwards); |
| 1306 |
ao2_ref(forwards, -1); |
| 1307 |
|
| 1308 |
return 0; |
| 1309 |
} |
| 1310 |
|
| 1311 |
forwards = ao2_find(app->forwards, |
| 1312 |
chan ? ast_channel_uniqueid(chan) : CHANNEL_ALL, |
| 1313 |
OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 1314 |
if (!forwards) { |
| 1315 |
int res; |
| 1316 |
|
| 1317 |
|
| 1318 |
forwards = forwards_create_channel(app, chan); |
| 1319 |
if (!forwards) { |
| 1320 |
ao2_unlock(app->forwards); |
| 1321 |
|
| 1322 |
return -1; |
| 1323 |
} |
| 1324 |
|
| 1325 |
res = ao2_link_flags(app->forwards, forwards, OBJ_NOLOCK); |
| 1326 |
if (!res) { |
| 1327 |
ao2_unlock(app->forwards); |
| 1328 |
ao2_ref(forwards, -1); |
| 1329 |
|
| 1330 |
return -1; |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
++forwards->interested; |
| 1335 |
ast_debug(3, "Channel '%s' is %d interested in %s\n", |
| 1336 |
chan ? ast_channel_uniqueid(chan) : "ALL", |
| 1337 |
forwards->interested, |
| 1338 |
app->name); |
| 1339 |
|
| 1340 |
ao2_unlock(app->forwards); |
| 1341 |
ao2_ref(forwards, -1); |
| 1342 |
|
| 1343 |
return 0; |
| 1344 |
} |
| 1345 |
|
| 1346 |
static int subscribe_channel(struct stasis_app *app, void *obj) |
| 1347 |
{ |
| 1348 |
return app_subscribe_channel(app, obj); |
| 1349 |
} |
| 1350 |
|
| 1351 |
static int unsubscribe(struct stasis_app *app, const char *kind, const char *id, int terminate) |
| 1352 |
{ |
| 1353 |
struct app_forwards *forwards; |
| 1354 |
|
| 1355 |
if (!id) { |
| 1356 |
if (!strcmp(kind, "bridge")) { |
| 1357 |
id = BRIDGE_ALL; |
| 1358 |
} else if (!strcmp(kind, "channel")) { |
| 1359 |
id = CHANNEL_ALL; |
| 1360 |
} else if (!strcmp(kind, "endpoint")) { |
| 1361 |
id = ENDPOINT_ALL; |
| 1362 |
} else { |
| 1363 |
ast_log(LOG_WARNING, "Unknown subscription kind '%s'\n", kind); |
| 1364 |
return -1; |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
ao2_lock(app->forwards); |
| 1369 |
forwards = ao2_find(app->forwards, id, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 1370 |
if (!forwards) { |
| 1371 |
ao2_unlock(app->forwards); |
| 1372 |
ast_debug(3, "App '%s' not subscribed to %s '%s'\n", app->name, kind, id); |
| 1373 |
return -1; |
| 1374 |
} |
| 1375 |
forwards->interested--; |
| 1376 |
|
| 1377 |
ast_debug(3, "%s '%s': is %d interested in %s\n", kind, id, forwards->interested, app->name); |
| 1378 |
if (forwards->interested == 0 || terminate) { |
| 1379 |
|
| 1380 |
ast_debug(3, "%s '%s' unsubscribed from %s\n", kind, id, app->name); |
| 1381 |
forwards_unsubscribe(forwards); |
| 1382 |
ao2_find(app->forwards, forwards, |
| 1383 |
OBJ_POINTER | OBJ_NOLOCK | OBJ_UNLINK | |
| 1384 |
OBJ_NODATA); |
| 1385 |
|
| 1386 |
if (!strcmp(kind, "endpoint")) { |
| 1387 |
messaging_app_unsubscribe_endpoint(app->name, id); |
| 1388 |
} |
| 1389 |
} |
| 1390 |
ao2_unlock(app->forwards); |
| 1391 |
ao2_ref(forwards, -1); |
| 1392 |
|
| 1393 |
return 0; |
| 1394 |
} |
| 1395 |
|
| 1396 |
int app_unsubscribe_channel(struct stasis_app *app, struct ast_channel *chan) |
| 1397 |
{ |
| 1398 |
if (!app) { |
| 1399 |
return -1; |
| 1400 |
} |
| 1401 |
|
| 1402 |
return app_unsubscribe_channel_id(app, chan ? ast_channel_uniqueid(chan) : CHANNEL_ALL); |
| 1403 |
} |
| 1404 |
|
| 1405 |
int app_unsubscribe_channel_id(struct stasis_app *app, const char *channel_id) |
| 1406 |
{ |
| 1407 |
if (!app) { |
| 1408 |
return -1; |
| 1409 |
} |
| 1410 |
|
| 1411 |
return unsubscribe(app, "channel", channel_id, 0); |
| 1412 |
} |
| 1413 |
|
| 1414 |
int app_is_subscribed_channel_id(struct stasis_app *app, const char *channel_id) |
| 1415 |
{ |
| 1416 |
struct app_forwards *forwards; |
| 1417 |
|
| 1418 |
if (ast_strlen_zero(channel_id)) { |
| 1419 |
channel_id = CHANNEL_ALL; |
| 1420 |
} |
| 1421 |
forwards = ao2_find(app->forwards, channel_id, OBJ_SEARCH_KEY); |
| 1422 |
ao2_cleanup(forwards); |
| 1423 |
|
| 1424 |
return forwards != NULL; |
| 1425 |
} |
| 1426 |
|
| 1427 |
static void *channel_find(const struct stasis_app *app, const char *id) |
| 1428 |
{ |
| 1429 |
return ast_channel_get_by_name(id); |
| 1430 |
} |
| 1431 |
|
| 1432 |
struct stasis_app_event_source channel_event_source = { |
| 1433 |
.scheme = "channel:", |
| 1434 |
.find = channel_find, |
| 1435 |
.subscribe = subscribe_channel, |
| 1436 |
.unsubscribe = app_unsubscribe_channel_id, |
| 1437 |
.is_subscribed = app_is_subscribed_channel_id |
| 1438 |
}; |
| 1439 |
|
| 1440 |
int app_subscribe_bridge(struct stasis_app *app, struct ast_bridge *bridge) |
| 1441 |
{ |
| 1442 |
struct app_forwards *forwards; |
| 1443 |
|
| 1444 |
if (!app) { |
| 1445 |
return -1; |
| 1446 |
} |
| 1447 |
|
| 1448 |
ao2_lock(app->forwards); |
| 1449 |
|
| 1450 |
forwards = ao2_find(app->forwards, BRIDGE_ALL, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 1451 |
if (forwards) { |
| 1452 |
ao2_unlock(app->forwards); |
| 1453 |
ao2_ref(forwards, -1); |
| 1454 |
|
| 1455 |
return 0; |
| 1456 |
} |
| 1457 |
|
| 1458 |
forwards = ao2_find(app->forwards, |
| 1459 |
bridge ? bridge->uniqueid : BRIDGE_ALL, |
| 1460 |
OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 1461 |
if (!forwards) { |
| 1462 |
int res; |
| 1463 |
|
| 1464 |
|
| 1465 |
forwards = forwards_create_bridge(app, bridge); |
| 1466 |
if (!forwards) { |
| 1467 |
ao2_unlock(app->forwards); |
| 1468 |
|
| 1469 |
return -1; |
| 1470 |
} |
| 1471 |
|
| 1472 |
res = ao2_link_flags(app->forwards, forwards, OBJ_NOLOCK); |
| 1473 |
if (!res) { |
| 1474 |
ao2_unlock(app->forwards); |
| 1475 |
ao2_ref(forwards, -1); |
| 1476 |
|
| 1477 |
return -1; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
++forwards->interested; |
| 1482 |
ast_debug(3, "Bridge '%s' is %d interested in %s\n", |
| 1483 |
bridge ? bridge->uniqueid : "ALL", |
| 1484 |
forwards->interested, |
| 1485 |
app->name); |
| 1486 |
|
| 1487 |
ao2_unlock(app->forwards); |
| 1488 |
ao2_ref(forwards, -1); |
| 1489 |
|
| 1490 |
return 0; |
| 1491 |
} |
| 1492 |
|
| 1493 |
static int subscribe_bridge(struct stasis_app *app, void *obj) |
| 1494 |
{ |
| 1495 |
return app_subscribe_bridge(app, obj); |
| 1496 |
} |
| 1497 |
|
| 1498 |
int app_unsubscribe_bridge(struct stasis_app *app, struct ast_bridge *bridge) |
| 1499 |
{ |
| 1500 |
if (!app) { |
| 1501 |
return -1; |
| 1502 |
} |
| 1503 |
|
| 1504 |
return app_unsubscribe_bridge_id(app, bridge ? bridge->uniqueid : BRIDGE_ALL); |
| 1505 |
} |
| 1506 |
|
| 1507 |
int app_unsubscribe_bridge_id(struct stasis_app *app, const char *bridge_id) |
| 1508 |
{ |
| 1509 |
if (!app) { |
| 1510 |
return -1; |
| 1511 |
} |
| 1512 |
|
| 1513 |
return unsubscribe(app, "bridge", bridge_id, 0); |
| 1514 |
} |
| 1515 |
|
| 1516 |
int app_is_subscribed_bridge_id(struct stasis_app *app, const char *bridge_id) |
| 1517 |
{ |
| 1518 |
struct app_forwards *forwards; |
| 1519 |
|
| 1520 |
if (ast_strlen_zero(bridge_id)) { |
| 1521 |
bridge_id = BRIDGE_ALL; |
| 1522 |
} |
| 1523 |
|
| 1524 |
forwards = ao2_find(app->forwards, bridge_id, OBJ_SEARCH_KEY); |
| 1525 |
ao2_cleanup(forwards); |
| 1526 |
|
| 1527 |
return forwards != NULL; |
| 1528 |
} |
| 1529 |
|
| 1530 |
static void *bridge_find(const struct stasis_app *app, const char *id) |
| 1531 |
{ |
| 1532 |
return stasis_app_bridge_find_by_id(id); |
| 1533 |
} |
| 1534 |
|
| 1535 |
struct stasis_app_event_source bridge_event_source = { |
| 1536 |
.scheme = "bridge:", |
| 1537 |
.find = bridge_find, |
| 1538 |
.subscribe = subscribe_bridge, |
| 1539 |
.unsubscribe = app_unsubscribe_bridge_id, |
| 1540 |
.is_subscribed = app_is_subscribed_bridge_id |
| 1541 |
}; |
| 1542 |
|
| 1543 |
int app_subscribe_endpoint(struct stasis_app *app, struct ast_endpoint *endpoint) |
| 1544 |
{ |
| 1545 |
struct app_forwards *forwards; |
| 1546 |
|
| 1547 |
if (!app) { |
| 1548 |
return -1; |
| 1549 |
} |
| 1550 |
|
| 1551 |
ao2_lock(app->forwards); |
| 1552 |
|
| 1553 |
forwards = ao2_find(app->forwards, ENDPOINT_ALL, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 1554 |
if (forwards) { |
| 1555 |
ao2_unlock(app->forwards); |
| 1556 |
ao2_ref(forwards, -1); |
| 1557 |
|
| 1558 |
return 0; |
| 1559 |
} |
| 1560 |
|
| 1561 |
forwards = ao2_find(app->forwards, |
| 1562 |
endpoint ? ast_endpoint_get_id(endpoint) : ENDPOINT_ALL, |
| 1563 |
OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 1564 |
if (!forwards) { |
| 1565 |
int res; |
| 1566 |
|
| 1567 |
|
| 1568 |
forwards = forwards_create_endpoint(app, endpoint); |
| 1569 |
if (!forwards) { |
| 1570 |
ao2_unlock(app->forwards); |
| 1571 |
|
| 1572 |
return -1; |
| 1573 |
} |
| 1574 |
|
| 1575 |
res = ao2_link_flags(app->forwards, forwards, OBJ_NOLOCK); |
| 1576 |
if (!res) { |
| 1577 |
ao2_unlock(app->forwards); |
| 1578 |
ao2_ref(forwards, -1); |
| 1579 |
|
| 1580 |
return -1; |
| 1581 |
} |
| 1582 |
|
| 1583 |
|
| 1584 |
messaging_app_subscribe_endpoint(app->name, endpoint, &message_received_handler, app); |
| 1585 |
} |
| 1586 |
|
| 1587 |
++forwards->interested; |
| 1588 |
ast_debug(3, "Endpoint '%s' is %d interested in %s\n", |
| 1589 |
endpoint ? ast_endpoint_get_id(endpoint) : "ALL", |
| 1590 |
forwards->interested, |
| 1591 |
app->name); |
| 1592 |
|
| 1593 |
ao2_unlock(app->forwards); |
| 1594 |
ao2_ref(forwards, -1); |
| 1595 |
|
| 1596 |
return 0; |
| 1597 |
} |
| 1598 |
|
| 1599 |
static int subscribe_endpoint(struct stasis_app *app, void *obj) |
| 1600 |
{ |
| 1601 |
return app_subscribe_endpoint(app, obj); |
| 1602 |
} |
| 1603 |
|
| 1604 |
int app_unsubscribe_endpoint_id(struct stasis_app *app, const char *endpoint_id) |
| 1605 |
{ |
| 1606 |
if (!app) { |
| 1607 |
return -1; |
| 1608 |
} |
| 1609 |
|
| 1610 |
return unsubscribe(app, "endpoint", endpoint_id, 0); |
| 1611 |
} |
| 1612 |
|
| 1613 |
int app_is_subscribed_endpoint_id(struct stasis_app *app, const char *endpoint_id) |
| 1614 |
{ |
| 1615 |
struct app_forwards *forwards; |
| 1616 |
|
| 1617 |
if (ast_strlen_zero(endpoint_id)) { |
| 1618 |
endpoint_id = ENDPOINT_ALL; |
| 1619 |
} |
| 1620 |
forwards = ao2_find(app->forwards, endpoint_id, OBJ_SEARCH_KEY); |
| 1621 |
ao2_cleanup(forwards); |
| 1622 |
|
| 1623 |
return forwards != NULL; |
| 1624 |
} |
| 1625 |
|
| 1626 |
static void *endpoint_find(const struct stasis_app *app, const char *id) |
| 1627 |
{ |
| 1628 |
return ast_endpoint_find_by_id(id); |
| 1629 |
} |
| 1630 |
|
| 1631 |
struct stasis_app_event_source endpoint_event_source = { |
| 1632 |
.scheme = "endpoint:", |
| 1633 |
.find = endpoint_find, |
| 1634 |
.subscribe = subscribe_endpoint, |
| 1635 |
.unsubscribe = app_unsubscribe_endpoint_id, |
| 1636 |
.is_subscribed = app_is_subscribed_endpoint_id |
| 1637 |
}; |
| 1638 |
|
| 1639 |
void stasis_app_register_event_sources(void) |
| 1640 |
{ |
| 1641 |
stasis_app_register_event_source(&channel_event_source); |
| 1642 |
stasis_app_register_event_source(&bridge_event_source); |
| 1643 |
stasis_app_register_event_source(&endpoint_event_source); |
| 1644 |
} |
| 1645 |
|
| 1646 |
void stasis_app_unregister_event_sources(void) |
| 1647 |
{ |
| 1648 |
stasis_app_unregister_event_source(&endpoint_event_source); |
| 1649 |
stasis_app_unregister_event_source(&bridge_event_source); |
| 1650 |
stasis_app_unregister_event_source(&channel_event_source); |
| 1651 |
} |
| 1652 |
|
| 1653 |
struct ast_json *stasis_app_event_filter_to_json(struct stasis_app *app, struct ast_json *json) |
| 1654 |
{ |
| 1655 |
if (!app || !json) { |
| 1656 |
return json; |
| 1657 |
} |
| 1658 |
|
| 1659 |
ast_json_object_set(json, "events_allowed", app->events_allowed ? |
| 1660 |
ast_json_ref(app->events_allowed) : ast_json_array_create()); |
| 1661 |
ast_json_object_set(json, "events_disallowed", app->events_disallowed ? |
| 1662 |
ast_json_ref(app->events_disallowed) : ast_json_array_create()); |
| 1663 |
|
| 1664 |
return json; |
| 1665 |
} |
| 1666 |
|
| 1667 |
static int app_event_filter_set(struct stasis_app *app, struct ast_json **member, |
| 1668 |
struct ast_json *filter, const char *filter_type) |
| 1669 |
{ |
| 1670 |
if (filter && ast_json_typeof(filter) == AST_JSON_OBJECT) { |
| 1671 |
if (!ast_json_object_size(filter)) { |
| 1672 |
|
| 1673 |
filter = NULL; |
| 1674 |
} else { |
| 1675 |
|
| 1676 |
filter = ast_json_object_get(filter, filter_type); |
| 1677 |
if (!filter) { |
| 1678 |
|
| 1679 |
return 0; |
| 1680 |
} |
| 1681 |
} |
| 1682 |
} |
| 1683 |
|
| 1684 |
|
| 1685 |
if (filter && ast_json_typeof(filter) != AST_JSON_ARRAY) { |
| 1686 |
ast_log(LOG_ERROR, "Invalid json type event filter - app: %s, filter: %s\n", |
| 1687 |
app->name, filter_type); |
| 1688 |
return -1; |
| 1689 |
} |
| 1690 |
|
| 1691 |
if (filter) { |
| 1692 |
|
| 1693 |
struct ast_json *obj; |
| 1694 |
int i; |
| 1695 |
|
| 1696 |
for (i = 0; i < ast_json_array_size(filter) && |
| 1697 |
(obj = ast_json_array_get(filter, i)); ++i) { |
| 1698 |
|
| 1699 |
if (ast_strlen_zero(ast_json_object_string_get(obj, "type"))) { |
| 1700 |
ast_log(LOG_ERROR, "Filter event must have a type - app: %s, " |
| 1701 |
"filter: %s\n", app->name, filter_type); |
| 1702 |
return -1; |
| 1703 |
} |
| 1704 |
} |
| 1705 |
} |
| 1706 |
|
| 1707 |
ao2_lock(app); |
| 1708 |
ast_json_unref(*member); |
| 1709 |
*member = filter ? ast_json_ref(filter) : NULL; |
| 1710 |
ao2_unlock(app); |
| 1711 |
|
| 1712 |
return 0; |
| 1713 |
} |
| 1714 |
|
| 1715 |
static int app_events_allowed_set(struct stasis_app *app, struct ast_json *filter) |
| 1716 |
{ |
| 1717 |
return app_event_filter_set(app, &app->events_allowed, filter, "allowed"); |
| 1718 |
} |
| 1719 |
|
| 1720 |
static int app_events_disallowed_set(struct stasis_app *app, struct ast_json *filter) |
| 1721 |
{ |
| 1722 |
return app_event_filter_set(app, &app->events_disallowed, filter, "disallowed"); |
| 1723 |
} |
| 1724 |
|
| 1725 |
int stasis_app_event_filter_set(struct stasis_app *app, struct ast_json *filter) |
| 1726 |
{ |
| 1727 |
return app_events_disallowed_set(app, filter) || app_events_allowed_set(app, filter); |
| 1728 |
} |
| 1729 |
|
| 1730 |
static int app_event_filter_matched(struct ast_json *array, struct ast_json *event, int empty) |
| 1731 |
{ |
| 1732 |
struct ast_json *obj; |
| 1733 |
int i; |
| 1734 |
|
| 1735 |
if (!array || !ast_json_array_size(array)) { |
| 1736 |
return empty; |
| 1737 |
} |
| 1738 |
|
| 1739 |
for (i = 0; i < ast_json_array_size(array) && |
| 1740 |
(obj = ast_json_array_get(array, i)); ++i) { |
| 1741 |
|
| 1742 |
if (ast_strings_equal(ast_json_object_string_get(obj, "type"), |
| 1743 |
ast_json_object_string_get(event, "type"))) { |
| 1744 |
return 1; |
| 1745 |
} |
| 1746 |
} |
| 1747 |
|
| 1748 |
return 0; |
| 1749 |
} |
| 1750 |
|
| 1751 |
int stasis_app_event_allowed(const char *app_name, struct ast_json *event) |
| 1752 |
{ |
| 1753 |
struct stasis_app *app = stasis_app_get_by_name(app_name); |
| 1754 |
int res; |
| 1755 |
|
| 1756 |
if (!app) { |
| 1757 |
return 0; |
| 1758 |
} |
| 1759 |
|
| 1760 |
ao2_lock(app); |
| 1761 |
res = !app_event_filter_matched(app->events_disallowed, event, 0) && |
| 1762 |
app_event_filter_matched(app->events_allowed, event, 1); |
| 1763 |
ao2_unlock(app); |
| 1764 |
ao2_ref(app, -1); |
| 1765 |
|
| 1766 |
return res; |
| 1767 |
} |