static void sub_endpoint_update_handler(void *data, struct stasis_subscription *sub, struct stasis_message *message) { RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); struct stasis_app *app = data; struct stasis_cache_update *update; struct ast_endpoint_snapshot *new_snapshot; struct ast_endpoint_snapshot *old_snapshot; const struct timeval *tv; ast_assert(stasis_message_type(message) == stasis_cache_update_type()); update = stasis_message_data(message); ast_assert(update->type == ast_endpoint_snapshot_type()); new_snapshot = stasis_message_data(update->new_snapshot); old_snapshot = stasis_message_data(update->old_snapshot); if (new_snapshot) { tv = stasis_message_timestamp(update->new_snapshot); json = simple_endpoint_event("EndpointStateChange", new_snapshot, tv); if (!json) { return; } app_send(app, json); } if (!new_snapshot && old_snapshot) { unsubscribe(app, "endpoint", old_snapshot->id, 1); } }
static int find_route( struct stasis_message_router *router, struct stasis_message *message, struct stasis_message_route *route_out) { struct stasis_message_route *route = NULL; struct stasis_message_type *type = stasis_message_type(message); SCOPED_AO2LOCK(lock, router); ast_assert(route_out != NULL); if (type == stasis_cache_update_type()) { /* Find a cache route */ struct stasis_cache_update *update = stasis_message_data(message); route = route_table_find(&router->cache_routes, update->type); } if (route == NULL) { /* Find a regular route */ route = route_table_find(&router->routes, type); } if (route == NULL && router->default_route.callback) { /* Maybe the default route, then? */ route = &router->default_route; } if (!route) { return -1; } *route_out = *route; return 0; }
static int load_module(void) { struct ao2_container *endpoints; router = stasis_message_router_create(ast_endpoint_topic_all_cached()); if (!router) { return AST_MODULE_LOAD_FAILURE; } stasis_message_router_add(router, stasis_cache_update_type(), cache_update_cb, NULL); endpoints = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type()); if (endpoints) { ao2_callback(endpoints, OBJ_MULTIPLE | OBJ_NODATA | OBJ_NOLOCK, dump_cache_load, NULL); ao2_ref(endpoints, -1); } return AST_MODULE_LOAD_SUCCESS; }
static void sub_bridge_update_handler(void *data, struct stasis_subscription *sub, struct stasis_message *message) { RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); struct stasis_app *app = data; struct stasis_cache_update *update; struct ast_bridge_snapshot *new_snapshot; struct ast_bridge_snapshot *old_snapshot; const struct timeval *tv; ast_assert(stasis_message_type(message) == stasis_cache_update_type()); update = stasis_message_data(message); ast_assert(update->type == ast_bridge_snapshot_type()); new_snapshot = stasis_message_data(update->new_snapshot); old_snapshot = stasis_message_data(update->old_snapshot); tv = update->new_snapshot ? stasis_message_timestamp(update->new_snapshot) : stasis_message_timestamp(message); if (!new_snapshot) { json = simple_bridge_event("BridgeDestroyed", old_snapshot, tv); } else if (!old_snapshot) { json = simple_bridge_event("BridgeCreated", new_snapshot, tv); } else if (new_snapshot && old_snapshot && strcmp(new_snapshot->video_source_id, old_snapshot->video_source_id)) { json = simple_bridge_event("BridgeVideoSourceChanged", new_snapshot, tv); if (json && !ast_strlen_zero(old_snapshot->video_source_id)) { ast_json_object_set(json, "old_video_source_id", ast_json_string_create(old_snapshot->video_source_id)); } } if (json) { app_send(app, json); } if (!new_snapshot && old_snapshot) { unsubscribe(app, "bridge", old_snapshot->uniqueid, 1); } }
static int load_module(void) { /* You can create a message router to route messages by type */ router = stasis_message_router_create( ast_channel_topic_all_cached()); if (!router) { return AST_MODULE_LOAD_FAILURE; } stasis_message_router_add(router, stasis_cache_update_type(), updates, NULL); stasis_message_router_set_default(router, default_route, NULL); /* Or a subscription to receive all of the messages from a topic */ sub = stasis_subscribe(ast_channel_topic_all(), statsmaker, NULL); if (!sub) { return AST_MODULE_LOAD_FAILURE; } return AST_MODULE_LOAD_SUCCESS; }
/*! \brief Message matcher looking for cache update messages */ static int cache_update(struct stasis_message *msg, const void *data) { struct stasis_cache_update *update; struct ast_endpoint_snapshot *snapshot; const char *name = data; if (stasis_cache_update_type() != stasis_message_type(msg)) { return 0; } update = stasis_message_data(msg); if (ast_endpoint_snapshot_type() != update->type) { return 0; } snapshot = stasis_message_data(update->old_snapshot); if (!snapshot) { snapshot = stasis_message_data(update->new_snapshot); } return 0 == strcmp(name, snapshot->resource); }
static void sub_channel_update_handler(void *data, struct stasis_subscription *sub, struct stasis_message *message) { struct stasis_app *app = data; struct stasis_cache_update *update; struct ast_channel_snapshot *new_snapshot; struct ast_channel_snapshot *old_snapshot; const struct timeval *tv; int i; ast_assert(stasis_message_type(message) == stasis_cache_update_type()); update = stasis_message_data(message); ast_assert(update->type == ast_channel_snapshot_type()); new_snapshot = stasis_message_data(update->new_snapshot); old_snapshot = stasis_message_data(update->old_snapshot); /* Pull timestamp from the new snapshot, or from the update message * when there isn't one. */ tv = update->new_snapshot ? stasis_message_timestamp(update->new_snapshot) : stasis_message_timestamp(message); for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) { RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref); msg = channel_monitors[i](old_snapshot, new_snapshot, tv); if (msg) { app_send(app, msg); } } if (!new_snapshot && old_snapshot) { unsubscribe(app, "channel", old_snapshot->uniqueid, 1); } }
static struct stasis_message *update_create(struct stasis_message *old_snapshot, struct stasis_message *new_snapshot) { RAII_VAR(struct stasis_cache_update *, update, NULL, ao2_cleanup); RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); ast_assert(old_snapshot != NULL || new_snapshot != NULL); update = ao2_alloc_options(sizeof(*update), stasis_cache_update_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); if (!update) { return NULL; } if (old_snapshot) { ao2_ref(old_snapshot, +1); update->old_snapshot = old_snapshot; if (!new_snapshot) { ao2_ref(stasis_message_type(old_snapshot), +1); update->type = stasis_message_type(old_snapshot); } } if (new_snapshot) { ao2_ref(new_snapshot, +1); update->new_snapshot = new_snapshot; ao2_ref(stasis_message_type(new_snapshot), +1); update->type = stasis_message_type(new_snapshot); } msg = stasis_message_create(stasis_cache_update_type(), update); if (!msg) { return NULL; } ao2_ref(msg, +1); return msg; }
static void consumer_wait_for(struct consumer *consumer) { int res; struct timeval start = ast_tvnow(); struct timespec end = { .tv_sec = start.tv_sec + 10, .tv_nsec = start.tv_usec * 1000 }; SCOPED_AO2LOCK(lock, consumer); while (!consumer->already_out) { res = ast_cond_timedwait(&consumer->out, ao2_object_get_lockaddr(consumer), &end); if (!res || res == ETIMEDOUT) { break; } } } static int remove_device_states_cb(void *obj, void *arg, int flags) { struct stasis_message *msg = obj; struct ast_device_state_message *device_state = stasis_message_data(msg); if (strcmp(UNIT_TEST_DEVICE_IDENTIFIER, device_state->device)) { /* Not a unit test device */ return 0; } msg = stasis_cache_clear_create(msg); if (msg) { /* topic guaranteed to have been created by this point */ stasis_publish(ast_device_state_topic(device_state->device), msg); } ao2_cleanup(msg); return 0; } static void cache_cleanup(int unused) { struct ao2_container *cache_dump; /* remove all device states created during this test */ cache_dump = stasis_cache_dump_all(ast_device_state_cache(), NULL); if (!cache_dump) { return; } ao2_callback(cache_dump, 0, remove_device_states_cb, NULL); ao2_cleanup(cache_dump); } AST_TEST_DEFINE(device_state_aggregation_test) { RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup); RAII_VAR(struct stasis_message_router *, device_msg_router, NULL, stasis_message_router_unsubscribe); RAII_VAR(struct ast_eid *, foreign_eid, NULL, ast_free); RAII_VAR(int, cleanup_cache, 0, cache_cleanup); RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); int res; struct ast_device_state_message *device_state; switch (cmd) { case TEST_INIT: info->name = "device_state_aggregation_test"; info->category = "/main/devicestate/"; info->summary = "Tests message routing and aggregation through the Stasis device state system."; info->description = "Verifies that the device state system passes " "messages appropriately, that the aggregator is " "working properly, that the aggregate results match " "the expected combined devstate, and that the cached " "aggregate devstate is correct."; return AST_TEST_NOT_RUN; case TEST_EXECUTE: break; } foreign_eid = ast_malloc(sizeof(*foreign_eid)); ast_test_validate(test, NULL != foreign_eid); memset(foreign_eid, 0xFF, sizeof(*foreign_eid)); consumer = consumer_create(); ast_test_validate(test, NULL != consumer); device_msg_router = stasis_message_router_create(ast_device_state_topic_cached()); ast_test_validate(test, NULL != device_msg_router); ao2_ref(consumer, +1); res = stasis_message_router_add(device_msg_router, stasis_cache_update_type(), consumer_exec, consumer); ast_test_validate(test, !res); res = stasis_message_router_add(device_msg_router, stasis_subscription_change_type(), consumer_finalize, consumer); ast_test_validate(test, !res); /* push local state */ ast_publish_device_state(UNIT_TEST_DEVICE_IDENTIFIER, AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE); /* Check cache aggregate state immediately */ ao2_cleanup(msg); msg = stasis_cache_get_by_eid(ast_device_state_cache(), ast_device_state_message_type(), UNIT_TEST_DEVICE_IDENTIFIER, NULL); device_state = stasis_message_data(msg); ast_test_validate(test, AST_DEVICE_NOT_INUSE == device_state->state); consumer_wait_for(consumer); ast_test_validate(test, AST_DEVICE_NOT_INUSE == consumer->state); ast_test_validate(test, AST_DEVICE_NOT_INUSE == consumer->aggregate_state); ast_test_validate(test, 2 == consumer->event_count); consumer_reset(consumer); /* push remote state */ /* this will not produce a new aggregate state message since the aggregate state does not change */ consumer->sig_on_non_aggregate_state = 1; ast_publish_device_state_full(UNIT_TEST_DEVICE_IDENTIFIER, AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, foreign_eid); /* Check cache aggregate state immediately */ ao2_cleanup(msg); msg = stasis_cache_get_by_eid(ast_device_state_cache(), ast_device_state_message_type(), UNIT_TEST_DEVICE_IDENTIFIER, NULL); device_state = stasis_message_data(msg); ast_test_validate(test, AST_DEVICE_NOT_INUSE == device_state->state); /* Check for expected events. */ consumer_wait_for(consumer); ast_test_validate(test, AST_DEVICE_NOT_INUSE == consumer->state); ast_test_validate(test, AST_DEVICE_TOTAL == consumer->aggregate_state); ast_test_validate(test, 1 == consumer->event_count); consumer_reset(consumer); /* push remote state different from local state */ ast_publish_device_state_full(UNIT_TEST_DEVICE_IDENTIFIER, AST_DEVICE_INUSE, AST_DEVSTATE_CACHABLE, foreign_eid); /* Check cache aggregate state immediately */ ao2_cleanup(msg); msg = stasis_cache_get_by_eid(ast_device_state_cache(), ast_device_state_message_type(), UNIT_TEST_DEVICE_IDENTIFIER, NULL); device_state = stasis_message_data(msg); ast_test_validate(test, AST_DEVICE_INUSE == device_state->state); /* Check for expected events. */ consumer_wait_for(consumer); ast_test_validate(test, AST_DEVICE_INUSE == consumer->state); ast_test_validate(test, AST_DEVICE_INUSE == consumer->aggregate_state); ast_test_validate(test, 2 == consumer->event_count); consumer_reset(consumer); /* push local state that will cause aggregated state different from local non-aggregate state */ ast_publish_device_state(UNIT_TEST_DEVICE_IDENTIFIER, AST_DEVICE_RINGING, AST_DEVSTATE_CACHABLE); /* Check cache aggregate state immediately */ ao2_cleanup(msg); msg = stasis_cache_get_by_eid(ast_device_state_cache(), ast_device_state_message_type(), UNIT_TEST_DEVICE_IDENTIFIER, NULL); device_state = stasis_message_data(msg); ast_test_validate(test, AST_DEVICE_RINGINUSE == device_state->state); /* Check for expected events. */ consumer_wait_for(consumer); ast_test_validate(test, AST_DEVICE_RINGING == consumer->state); ast_test_validate(test, AST_DEVICE_RINGINUSE == consumer->aggregate_state); ast_test_validate(test, 2 == consumer->event_count); consumer_reset(consumer); return AST_TEST_PASS; } static int unload_module(void) { AST_TEST_UNREGISTER(device2extenstate_test); AST_TEST_UNREGISTER(device_state_aggregation_test); return 0; }