2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * David M. Lee, II <dlee@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Stasis Message API.
23 * \author David M. Lee, II <dlee@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/astobj2.h"
35 #include "asterisk/hashtab.h"
36 #include "asterisk/stasis_internal.h"
37 #include "asterisk/stasis.h"
38 #include "asterisk/utils.h"
41 #define NUM_CACHE_BUCKETS 17
43 #define NUM_CACHE_BUCKETS 563
48 struct ao2_container *entries;
49 snapshot_get_id id_fn;
53 struct stasis_caching_topic {
54 struct stasis_cache *cache;
55 struct stasis_topic *topic;
56 struct stasis_topic *original_topic;
57 struct stasis_subscription *sub;
60 static void stasis_caching_topic_dtor(void *obj) {
61 struct stasis_caching_topic *caching_topic = obj;
63 /* Caching topics contain subscriptions, and must be manually
65 ast_assert(!stasis_subscription_is_subscribed(caching_topic->sub));
66 /* If there are any messages in flight to this subscription; that would
68 ast_assert(stasis_subscription_is_done(caching_topic->sub));
70 ao2_cleanup(caching_topic->sub);
71 caching_topic->sub = NULL;
72 ao2_cleanup(caching_topic->cache);
73 caching_topic->cache = NULL;
74 ao2_cleanup(caching_topic->topic);
75 caching_topic->topic = NULL;
76 ao2_cleanup(caching_topic->original_topic);
77 caching_topic->original_topic = NULL;
80 struct stasis_topic *stasis_caching_get_topic(struct stasis_caching_topic *caching_topic)
82 return caching_topic->topic;
85 struct stasis_caching_topic *stasis_caching_unsubscribe(struct stasis_caching_topic *caching_topic)
88 RAII_VAR(struct stasis_caching_topic *, hold_ref, NULL,
91 /* The subscription may hold the last reference to this caching
92 * topic, but we want to make sure the unsubscribe finishes
93 * before kicking of the caching topic's dtor.
95 ao2_ref(caching_topic, +1);
96 hold_ref = caching_topic;
98 if (stasis_subscription_is_subscribed(caching_topic->sub)) {
99 /* Increment the reference to hold on to it past the
100 * unsubscribe. Will be cleaned up in dtor. */
101 ao2_ref(caching_topic->sub, +1);
102 stasis_unsubscribe(caching_topic->sub);
104 ast_log(LOG_ERROR, "stasis_caching_topic unsubscribed multiple times\n");
110 struct stasis_caching_topic *stasis_caching_unsubscribe_and_join(struct stasis_caching_topic *caching_topic)
112 if (!caching_topic) {
116 /* Hold a ref past the unsubscribe */
117 ao2_ref(caching_topic, +1);
118 stasis_caching_unsubscribe(caching_topic);
119 stasis_subscription_join(caching_topic->sub);
120 ao2_cleanup(caching_topic);
125 struct stasis_message_type *type;
127 struct stasis_message *snapshot;
130 static void cache_entry_dtor(void *obj)
132 struct cache_entry *entry = obj;
133 ao2_cleanup(entry->type);
137 ao2_cleanup(entry->snapshot);
138 entry->snapshot = NULL;
141 static struct cache_entry *cache_entry_create(struct stasis_message_type *type, const char *id, struct stasis_message *snapshot)
143 RAII_VAR(struct cache_entry *, entry, NULL, ao2_cleanup);
145 ast_assert(type != NULL);
146 ast_assert(id != NULL);
148 entry = ao2_alloc_options(sizeof(*entry), cache_entry_dtor,
149 AO2_ALLOC_OPT_LOCK_NOLOCK);
154 entry->id = ast_strdup(id);
161 if (snapshot != NULL) {
162 ao2_ref(snapshot, +1);
163 entry->snapshot = snapshot;
170 static int cache_entry_hash(const void *obj, int flags)
172 const struct cache_entry *entry = obj;
175 ast_assert(!(flags & OBJ_KEY));
177 hash += ast_hashtab_hash_string(stasis_message_type_name(entry->type));
178 hash += ast_hashtab_hash_string(entry->id);
182 static int cache_entry_cmp(void *obj, void *arg, int flags)
184 const struct cache_entry *left = obj;
185 const struct cache_entry *right = arg;
187 ast_assert(!(flags & OBJ_KEY));
189 if (left->type == right->type && strcmp(left->id, right->id) == 0) {
190 return CMP_MATCH | CMP_STOP;
196 static void cache_dtor(void *obj)
198 struct stasis_cache *cache = obj;
200 ao2_cleanup(cache->entries);
201 cache->entries = NULL;
204 struct stasis_cache *stasis_cache_create(snapshot_get_id id_fn)
206 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
208 cache = ao2_alloc_options(sizeof(*cache), cache_dtor,
209 AO2_ALLOC_OPT_LOCK_NOLOCK);
214 cache->entries = ao2_container_alloc(NUM_CACHE_BUCKETS, cache_entry_hash,
216 if (!cache->entries) {
220 cache->id_fn = id_fn;
226 static struct stasis_message *cache_put(struct stasis_cache *cache,
227 struct stasis_message_type *type, const char *id,
228 struct stasis_message *new_snapshot)
230 RAII_VAR(struct cache_entry *, new_entry, NULL, ao2_cleanup);
231 RAII_VAR(struct cache_entry *, cached_entry, NULL, ao2_cleanup);
232 struct stasis_message *old_snapshot = NULL;
234 ast_assert(cache->entries != NULL);
235 ast_assert(new_snapshot == NULL ||
236 type == stasis_message_type(new_snapshot));
238 new_entry = cache_entry_create(type, id, new_snapshot);
240 if (new_snapshot == NULL) {
241 /* Remove entry from cache */
242 cached_entry = ao2_find(cache->entries, new_entry, OBJ_POINTER | OBJ_UNLINK);
244 old_snapshot = cached_entry->snapshot;
245 cached_entry->snapshot = NULL;
248 /* Insert/update cache */
249 SCOPED_AO2LOCK(lock, cache->entries);
251 cached_entry = ao2_find(cache->entries, new_entry, OBJ_POINTER | OBJ_NOLOCK);
253 /* Update cache. Because objects are moving, no need to update refcounts. */
254 old_snapshot = cached_entry->snapshot;
255 cached_entry->snapshot = new_entry->snapshot;
256 new_entry->snapshot = NULL;
258 /* Insert into the cache */
259 ao2_link_flags(cache->entries, new_entry, OBJ_NOLOCK);
267 struct stasis_message *stasis_cache_get(struct stasis_cache *cache, struct stasis_message_type *type, const char *id)
269 RAII_VAR(struct cache_entry *, search_entry, NULL, ao2_cleanup);
270 RAII_VAR(struct cache_entry *, cached_entry, NULL, ao2_cleanup);
272 ast_assert(cache->entries != NULL);
274 search_entry = cache_entry_create(type, id, NULL);
275 if (search_entry == NULL) {
279 cached_entry = ao2_find(cache->entries, search_entry, OBJ_POINTER);
280 if (cached_entry == NULL) {
284 ast_assert(cached_entry->snapshot != NULL);
285 ao2_ref(cached_entry->snapshot, +1);
286 return cached_entry->snapshot;
289 struct cache_dump_data {
290 struct ao2_container *cached;
291 struct stasis_message_type *type;
294 static int cache_dump_cb(void *obj, void *arg, int flags)
296 struct cache_dump_data *cache_dump = arg;
297 struct cache_entry *entry = obj;
299 if (!cache_dump->type || entry->type == cache_dump->type) {
300 ao2_link(cache_dump->cached, entry->snapshot);
306 struct ao2_container *stasis_cache_dump(struct stasis_cache *cache, struct stasis_message_type *type)
308 struct cache_dump_data cache_dump;
310 ast_assert(cache->entries != NULL);
312 cache_dump.type = type;
313 cache_dump.cached = ao2_container_alloc_options(
314 AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL);
315 if (!cache_dump.cached) {
319 ao2_callback(cache->entries, OBJ_MULTIPLE | OBJ_NODATA, cache_dump_cb, &cache_dump);
320 return cache_dump.cached;
323 STASIS_MESSAGE_TYPE_DEFN(stasis_cache_clear_type);
324 STASIS_MESSAGE_TYPE_DEFN(stasis_cache_update_type);
326 struct stasis_message *stasis_cache_clear_create(struct stasis_message *id_message)
328 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
330 msg = stasis_message_create(stasis_cache_clear_type(), id_message);
339 static void stasis_cache_update_dtor(void *obj)
341 struct stasis_cache_update *update = obj;
342 ao2_cleanup(update->old_snapshot);
343 update->old_snapshot = NULL;
344 ao2_cleanup(update->new_snapshot);
345 update->new_snapshot = NULL;
346 ao2_cleanup(update->type);
350 static struct stasis_message *update_create(struct stasis_message *old_snapshot, struct stasis_message *new_snapshot)
352 RAII_VAR(struct stasis_cache_update *, update, NULL, ao2_cleanup);
353 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
355 ast_assert(old_snapshot != NULL || new_snapshot != NULL);
357 update = ao2_alloc_options(sizeof(*update), stasis_cache_update_dtor,
358 AO2_ALLOC_OPT_LOCK_NOLOCK);
364 ao2_ref(old_snapshot, +1);
365 update->old_snapshot = old_snapshot;
367 ao2_ref(stasis_message_type(old_snapshot), +1);
368 update->type = stasis_message_type(old_snapshot);
372 ao2_ref(new_snapshot, +1);
373 update->new_snapshot = new_snapshot;
374 ao2_ref(stasis_message_type(new_snapshot), +1);
375 update->type = stasis_message_type(new_snapshot);
378 msg = stasis_message_create(stasis_cache_update_type(), update);
387 static void caching_topic_exec(void *data, struct stasis_subscription *sub,
388 struct stasis_message *message)
390 RAII_VAR(struct stasis_caching_topic *, caching_topic_needs_unref, NULL, ao2_cleanup);
391 struct stasis_caching_topic *caching_topic = data;
392 const char *id = NULL;
394 ast_assert(caching_topic != NULL);
395 ast_assert(caching_topic->topic != NULL);
396 ast_assert(caching_topic->cache != NULL);
397 ast_assert(caching_topic->cache->id_fn != NULL);
399 if (stasis_subscription_final_message(sub, message)) {
400 caching_topic_needs_unref = caching_topic;
403 /* Handle cache clear event */
404 if (stasis_cache_clear_type() == stasis_message_type(message)) {
405 RAII_VAR(struct stasis_message *, old_snapshot, NULL, ao2_cleanup);
406 RAII_VAR(struct stasis_message *, update, NULL, ao2_cleanup);
407 struct stasis_message *clear_msg = stasis_message_data(message);
408 const char *clear_id = caching_topic->cache->id_fn(clear_msg);
409 struct stasis_message_type *clear_type = stasis_message_type(clear_msg);
411 ast_assert(clear_type != NULL);
414 old_snapshot = cache_put(caching_topic->cache, clear_type, clear_id, NULL);
416 update = update_create(old_snapshot, NULL);
417 stasis_publish(caching_topic->topic, update);
422 "Attempting to remove an item from the %s cache that isn't there: %s %s\n",
423 stasis_topic_name(caching_topic->topic), stasis_message_type_name(clear_type), clear_id);
428 id = caching_topic->cache->id_fn(message);
430 /* Object isn't cached; discard */
432 /* Update the cache */
433 RAII_VAR(struct stasis_message *, old_snapshot, NULL, ao2_cleanup);
434 RAII_VAR(struct stasis_message *, update, NULL, ao2_cleanup);
436 old_snapshot = cache_put(caching_topic->cache, stasis_message_type(message), id, message);
438 update = update_create(old_snapshot, message);
439 if (update == NULL) {
443 stasis_publish(caching_topic->topic, update);
447 struct stasis_caching_topic *stasis_caching_topic_create(struct stasis_topic *original_topic, struct stasis_cache *cache)
449 RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, ao2_cleanup);
450 struct stasis_subscription *sub;
451 RAII_VAR(char *, new_name, NULL, ast_free);
454 ret = ast_asprintf(&new_name, "%s-cached", stasis_topic_name(original_topic));
459 caching_topic = ao2_alloc_options(sizeof(*caching_topic),
460 stasis_caching_topic_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
461 if (caching_topic == NULL) {
465 caching_topic->topic = stasis_topic_create(new_name);
466 if (caching_topic->topic == NULL) {
471 caching_topic->cache = cache;
473 sub = internal_stasis_subscribe(original_topic, caching_topic_exec, caching_topic, 0);
478 ao2_ref(original_topic, +1);
479 caching_topic->original_topic = original_topic;
481 /* This is for the reference contained in the subscription above */
482 ao2_ref(caching_topic, +1);
483 caching_topic->sub = sub;
485 /* The subscription holds the reference, so no additional ref bump. */
486 return caching_topic;
489 static void stasis_cache_cleanup(void)
491 STASIS_MESSAGE_TYPE_CLEANUP(stasis_cache_clear_type);
492 STASIS_MESSAGE_TYPE_CLEANUP(stasis_cache_update_type);
495 int stasis_cache_init(void)
497 ast_register_cleanup(stasis_cache_cleanup);
499 if (STASIS_MESSAGE_TYPE_INIT(stasis_cache_clear_type) != 0) {
503 if (STASIS_MESSAGE_TYPE_INIT(stasis_cache_update_type) != 0) {