diff options
author | David M. Lee <dlee@digium.com> | 2013-08-01 13:49:34 +0000 |
---|---|---|
committer | David M. Lee <dlee@digium.com> | 2013-08-01 13:49:34 +0000 |
commit | e1b959ccbb4e47421b37a0f75a2bf89ccd34dcb1 (patch) | |
tree | 3026c96da713bafcf1126c77bde6994f348280bb /main/devicestate.c | |
parent | 5c1396946929ab19e94c117f8ad3db5f78a450bc (diff) |
Split caching out from the stasis_caching_topic.
In working with res_stasis, I discovered a significant limitation to
the current structure of stasis_caching_topics: you cannot subscribe
to cache updates for a single channel/bridge/endpoint/etc.
To address this, this patch splits the cache away from the
stasis_caching_topic, making it a first class object. The stasis_cache
object is shared amongst individual stasis_caching_topics that are
created per channel/endpoint/etc. These are still forwarded to global
whatever_all_cached topics, so their use from most of the code does
not change.
In making these changes, I noticed that we frequently used a similar
pattern for bridges, endpoints and channels:
single_topic ----------------> all_topic
^
|
single_topic_cached ----+----> all_topic_cached
|
+----> cache
This pattern was extracted as the 'Stasis Caching Pattern', defined in
stasis_caching_pattern.h. This avoids a lot of duplicate code between
the different domain objects.
Since the cache is now disassociated from its upstream caching topics,
this also necessitated a change to how the 'guaranteed' flag worked
for retrieving from a cache. The code for handling the caching
guarantee was extracted into a 'stasis_topic_wait' function, which
works for any stasis_topic.
(closes issue ASTERISK-22002)
Review: https://reviewboard.asterisk.org/r/2672/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@395954 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/devicestate.c')
-rw-r--r-- | main/devicestate.c | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/main/devicestate.c b/main/devicestate.c index b2c70f764..c16a0628b 100644 --- a/main/devicestate.c +++ b/main/devicestate.c @@ -196,6 +196,7 @@ static ast_cond_t change_pending; struct stasis_subscription *devstate_message_sub; static struct stasis_topic *device_state_topic_all; +static struct stasis_cache *device_state_cache; static struct stasis_caching_topic *device_state_topic_cached; static struct stasis_topic_pool *device_state_topic_pool; @@ -285,7 +286,7 @@ static enum ast_device_state devstate_cached(const char *device) RAII_VAR(struct stasis_message *, cached_msg, NULL, ao2_cleanup); struct ast_device_state_message *device_state; - cached_msg = stasis_cache_get(ast_device_state_topic_cached(), ast_device_state_message_type(), device); + cached_msg = stasis_cache_get(ast_device_state_cache(), ast_device_state_message_type(), device); if (!cached_msg) { return AST_DEVICE_UNKNOWN; } @@ -586,7 +587,7 @@ static enum ast_device_state get_aggregate_state(char *device) ast_devstate_aggregate_init(&aggregate); - cached = stasis_cache_dump(ast_device_state_topic_cached(), NULL); + cached = stasis_cache_dump(ast_device_state_cache(), NULL); ao2_callback_data(cached, OBJ_NODATA, devstate_change_aggregator_cb, &aggregate, device); @@ -598,7 +599,7 @@ static int aggregate_state_changed(char *device, enum ast_device_state new_aggre RAII_VAR(struct stasis_message *, cached_aggregate_msg, NULL, ao2_cleanup); struct ast_device_state_message *cached_aggregate_device_state; - cached_aggregate_msg = stasis_cache_get(ast_device_state_topic_cached(), ast_device_state_message_type(), device); + cached_aggregate_msg = stasis_cache_get(ast_device_state_cache(), ast_device_state_message_type(), device); if (!cached_aggregate_msg) { return 1; } @@ -719,9 +720,14 @@ struct stasis_topic *ast_device_state_topic_all(void) return device_state_topic_all; } -struct stasis_caching_topic *ast_device_state_topic_cached(void) +struct stasis_cache *ast_device_state_cache(void) { - return device_state_topic_cached; + return device_state_cache; +} + +struct stasis_topic *ast_device_state_topic_cached(void) +{ + return stasis_caching_get_topic(device_state_topic_cached); } struct stasis_topic *ast_device_state_topic(const char *device) @@ -777,6 +783,8 @@ static void devstate_cleanup(void) devstate_message_sub = stasis_unsubscribe_and_join(devstate_message_sub); ao2_cleanup(device_state_topic_all); device_state_topic_all = NULL; + ao2_cleanup(device_state_cache); + device_state_cache = NULL; device_state_topic_cached = stasis_caching_unsubscribe_and_join(device_state_topic_cached); STASIS_MESSAGE_TYPE_CLEANUP(ast_device_state_message_type); ao2_cleanup(device_state_topic_pool); @@ -794,7 +802,11 @@ int devstate_init(void) if (!device_state_topic_all) { return -1; } - device_state_topic_cached = stasis_caching_topic_create(device_state_topic_all, device_state_get_id); + device_state_cache = stasis_cache_create(device_state_get_id); + if (!device_state_cache) { + return -1; + } + device_state_topic_cached = stasis_caching_topic_create(device_state_topic_all, device_state_cache); if (!device_state_topic_cached) { return -1; } @@ -803,7 +815,7 @@ int devstate_init(void) return -1; } - devstate_message_sub = stasis_subscribe(stasis_caching_get_topic(ast_device_state_topic_cached()), devstate_change_collector_cb, NULL); + devstate_message_sub = stasis_subscribe(ast_device_state_topic_cached(), devstate_change_collector_cb, NULL); if (!devstate_message_sub) { ast_log(LOG_ERROR, "Failed to create subscription for the device state change collector\n"); |