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 endpoint 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/stasis.h"
36 #include "asterisk/stasis_endpoints.h"
38 STASIS_MESSAGE_TYPE_DEFN(ast_endpoint_snapshot_type);
40 static struct stasis_topic *endpoint_topic_all;
42 static struct stasis_caching_topic *endpoint_topic_all_cached;
44 struct stasis_topic *ast_endpoint_topic_all(void)
46 return endpoint_topic_all;
49 struct stasis_caching_topic *ast_endpoint_topic_all_cached(void)
51 return endpoint_topic_all_cached;
54 struct ast_endpoint_snapshot *ast_endpoint_latest_snapshot(const char *tech,
57 RAII_VAR(char *, id, NULL, ast_free);
58 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
59 struct ast_endpoint_snapshot *snapshot;
61 ast_asprintf(&id, "%s/%s", tech, name);
66 msg = stasis_cache_get(ast_endpoint_topic_all_cached(),
67 ast_endpoint_snapshot_type(), id);
72 snapshot = stasis_message_data(msg);
73 ast_assert(snapshot != NULL);
75 ao2_ref(snapshot, +1);
80 * \brief Callback extract a unique identity from a snapshot message.
82 * This identity is unique to the underlying object of the snapshot, such as the
83 * UniqueId field of a channel.
85 * \param message Message to extract id from.
86 * \return String representing the snapshot's id.
87 * \return \c NULL if the message_type of the message isn't a handled snapshot.
90 static const char *endpoint_snapshot_get_id(struct stasis_message *message)
92 struct ast_endpoint_snapshot *snapshot;
94 if (ast_endpoint_snapshot_type() != stasis_message_type(message)) {
98 snapshot = stasis_message_data(message);
104 struct ast_json *ast_endpoint_snapshot_to_json(
105 const struct ast_endpoint_snapshot *snapshot)
107 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
108 struct ast_json *channel_array;
111 json = ast_json_pack("{s: s, s: s, s: s, s: []}",
112 "technology", snapshot->tech,
113 "resource", snapshot->resource,
114 "state", ast_endpoint_state_to_string(snapshot->state),
121 if (snapshot->max_channels != -1) {
122 int res = ast_json_object_set(json, "max_channels",
123 ast_json_integer_create(snapshot->max_channels));
129 channel_array = ast_json_object_get(json, "channels");
130 ast_assert(channel_array != NULL);
131 for (i = 0; i < snapshot->num_channels; ++i) {
132 int res = ast_json_array_append(channel_array,
133 ast_json_stringf("channel:%s",
134 snapshot->channel_ids[i]));
140 return ast_json_ref(json);
143 static void endpoints_stasis_shutdown(void)
145 stasis_caching_unsubscribe_and_join(endpoint_topic_all_cached);
146 endpoint_topic_all_cached = NULL;
148 ao2_cleanup(endpoint_topic_all);
149 endpoint_topic_all = NULL;
152 int ast_endpoint_stasis_init(void)
154 ast_register_atexit(endpoints_stasis_shutdown);
156 if (!endpoint_topic_all) {
157 endpoint_topic_all = stasis_topic_create("endpoint_topic_all");
160 if (!endpoint_topic_all) {
164 if (!endpoint_topic_all_cached) {
165 endpoint_topic_all_cached =
166 stasis_caching_topic_create(
167 endpoint_topic_all, endpoint_snapshot_get_id);
170 if (!endpoint_topic_all_cached) {
174 if (STASIS_MESSAGE_TYPE_INIT(ast_endpoint_snapshot_type) != 0) {