Initialize the message type before the topic
[asterisk/asterisk.git] / main / stasis_endpoints.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Stasis endpoint API.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/astobj2.h"
35 #include "asterisk/stasis.h"
36 #include "asterisk/stasis_endpoints.h"
37
38 STASIS_MESSAGE_TYPE_DEFN(ast_endpoint_snapshot_type);
39
40 static struct stasis_topic *endpoint_topic_all;
41
42 static struct stasis_caching_topic *endpoint_topic_all_cached;
43
44 struct stasis_topic *ast_endpoint_topic_all(void)
45 {
46         return endpoint_topic_all;
47 }
48
49 struct stasis_caching_topic *ast_endpoint_topic_all_cached(void)
50 {
51         return endpoint_topic_all_cached;
52 }
53
54 struct ast_endpoint_snapshot *ast_endpoint_latest_snapshot(const char *tech,
55         const char *name)
56 {
57         RAII_VAR(char *, id, NULL, ast_free);
58         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
59         struct ast_endpoint_snapshot *snapshot;
60
61         ast_asprintf(&id, "%s/%s", tech, name);
62         if (!id) {
63                 return NULL;
64         }
65
66         msg = stasis_cache_get(ast_endpoint_topic_all_cached(),
67                 ast_endpoint_snapshot_type(), id);
68         if (!msg) {
69                 return NULL;
70         }
71
72         snapshot = stasis_message_data(msg);
73         ast_assert(snapshot != NULL);
74
75         ao2_ref(snapshot, +1);
76         return snapshot;
77 }
78
79 /*!
80  * \brief Callback extract a unique identity from a snapshot message.
81  *
82  * This identity is unique to the underlying object of the snapshot, such as the
83  * UniqueId field of a channel.
84  *
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.
88  * \since 12
89  */
90 static const char *endpoint_snapshot_get_id(struct stasis_message *message)
91 {
92         struct ast_endpoint_snapshot *snapshot;
93
94         if (ast_endpoint_snapshot_type() != stasis_message_type(message)) {
95                 return NULL;
96         }
97
98         snapshot = stasis_message_data(message);
99
100         return snapshot->id;
101 }
102
103
104 struct ast_json *ast_endpoint_snapshot_to_json(
105         const struct ast_endpoint_snapshot *snapshot)
106 {
107         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
108         struct ast_json *channel_array;
109         int i;
110
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),
115                 "channels");
116
117         if (json == NULL) {
118                 return NULL;
119         }
120
121         if (snapshot->max_channels != -1) {
122                 int res = ast_json_object_set(json, "max_channels",
123                         ast_json_integer_create(snapshot->max_channels));
124                 if (res != 0) {
125                         return NULL;
126                 }
127         }
128
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]));
135                 if (res != 0) {
136                         return NULL;
137                 }
138         }
139
140         return ast_json_ref(json);
141 }
142
143 static void endpoints_stasis_shutdown(void)
144 {
145         stasis_caching_unsubscribe_and_join(endpoint_topic_all_cached);
146         endpoint_topic_all_cached = NULL;
147
148         ao2_cleanup(endpoint_topic_all);
149         endpoint_topic_all = NULL;
150 }
151
152 int ast_endpoint_stasis_init(void)
153 {
154         ast_register_atexit(endpoints_stasis_shutdown);
155
156         if (STASIS_MESSAGE_TYPE_INIT(ast_endpoint_snapshot_type) != 0) {
157                 return -1;
158         }
159
160         if (!endpoint_topic_all) {
161                 endpoint_topic_all = stasis_topic_create("endpoint_topic_all");
162         }
163
164         if (!endpoint_topic_all) {
165                 return -1;
166         }
167
168         if (!endpoint_topic_all_cached) {
169                 endpoint_topic_all_cached =
170                         stasis_caching_topic_create(
171                                 endpoint_topic_all, endpoint_snapshot_get_id);
172         }
173
174         if (!endpoint_topic_all_cached) {
175                 return -1;
176         }
177
178         return 0;
179 }