Debian Codegraph @

main/stasis_message.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * David M. Lee, II <[email protected]>
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 Message API.
22 *
23 * \author David M. Lee, II <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/astobj2.h"
33 #include "asterisk/stasis.h"
34 #include "asterisk/utils.h"
35 #include "asterisk/hashtab.h"
36
37 /*! \internal */
38 struct stasis_message_type {
39 struct stasis_message_vtable *vtable;
40 char *name;
41 unsigned int hash;
42 int id;
43 enum stasis_subscription_message_formatters available_formatters;
44 };
45
46 static struct stasis_message_vtable null_vtable = {};
47 static int message_type_id;
48
49 static void message_type_dtor(void *obj)
50 {
51 struct stasis_message_type *type = obj;
52 ast_free(type->name);
53 type->name = NULL;
54 }
55
56 enum stasis_message_type_result stasis_message_type_create(const char *name,
57 struct stasis_message_vtable *vtable,
58 struct stasis_message_type **result)
59 {
60 struct stasis_message_type *type;
61
62 /* Check for declination */
63 if (name && stasis_message_type_declined(name)) {
64 return STASIS_MESSAGE_TYPE_DECLINED;
65 }
66
67 type = ao2_t_alloc_options(sizeof(*type), message_type_dtor,
68 AO2_ALLOC_OPT_LOCK_NOLOCK, name ?: "");
69 if (!type) {
70 return STASIS_MESSAGE_TYPE_ERROR;
71 }
72 if (!vtable) {
73 /* Null object pattern, FTW! */
74 vtable = &null_vtable;
75 }
76
77 type->name = ast_strdup(name);
78 if (!type->name) {
79 ao2_cleanup(type);
80 return STASIS_MESSAGE_TYPE_ERROR;
81 }
82 type->hash = ast_hashtab_hash_string(name);
83 type->vtable = vtable;
84 if (vtable->to_json) {
85 type->available_formatters |= STASIS_SUBSCRIPTION_FORMATTER_JSON;
86 }
87 if (vtable->to_ami) {
88 type->available_formatters |= STASIS_SUBSCRIPTION_FORMATTER_AMI;
89 }
90 if (vtable->to_event) {
91 type->available_formatters |= STASIS_SUBSCRIPTION_FORMATTER_EVENT;
92 }
93 type->id = ast_atomic_fetchadd_int(&message_type_id, +1);
94 *result = type;
95
96 return STASIS_MESSAGE_TYPE_SUCCESS;
97 }
98
99 const char *stasis_message_type_name(const struct stasis_message_type *type)
100 {
101 return type->name;
102 }
103
104 unsigned int stasis_message_type_hash(const struct stasis_message_type *type)
105 {
106 return type->hash;
107 }
108
109 int stasis_message_type_id(const struct stasis_message_type *type)
110 {
111 return type->id;
112 }
113
114 enum stasis_subscription_message_formatters stasis_message_type_available_formatters(
115 const struct stasis_message_type *type)
116 {
117 return type->available_formatters;
118 }
119
120 /*! \internal */
121 struct stasis_message {
122 /*! Time the message was created */
123 struct timeval timestamp;
124 /*! Type of the message */
125 struct stasis_message_type *type;
126 /*! Where this message originated. NULL if aggregate message. */
127 const struct ast_eid *eid_ptr;
128 /*! Message content */
129 void *data;
130 /*! Where this message originated. */
131 struct ast_eid eid;
132 };
133
134 static void stasis_message_dtor(void *obj)
135 {
136 struct stasis_message *message = obj;
137 ao2_cleanup(message->data);
138 }
139
140 struct stasis_message *stasis_message_create_full(struct stasis_message_type *type, void *data, const struct ast_eid *eid)
141 {
142 struct stasis_message *message;
143
144 if (type == NULL || data == NULL) {
145 return NULL;
146 }
147
148 message = ao2_t_alloc_options(sizeof(*message), stasis_message_dtor,
149 AO2_ALLOC_OPT_LOCK_NOLOCK, type->name);
150 if (message == NULL) {
151 return NULL;
152 }
153
154 message->timestamp = ast_tvnow();
155 /*
156 * XXX Normal ao2 ref counting rules says we should increment the message
157 * type ref here and decrement it in stasis_message_dtor(). However, the
158 * stasis message could be cached and legitimately cause the type ref count
159 * to hit the excessive ref count assertion. Since the message type
160 * practically has to be a global object anyway, we can get away with not
161 * holding a ref in the stasis message.
162 */
163 message->type = type;
164 ao2_ref(data, +1);
165 message->data = data;
166 if (eid) {
167 message->eid_ptr = &message->eid;
168 message->eid = *eid;
169 }
170
171 return message;
172 }
173
174 struct stasis_message *stasis_message_create(struct stasis_message_type *type, void *data)
175 {
176 return stasis_message_create_full(type, data, &ast_eid_default);
177 }
178
179 const struct ast_eid *stasis_message_eid(const struct stasis_message *msg)
180 {
181 if (msg == NULL) {
182 return NULL;
183 }
184 return msg->eid_ptr;
185 }
186
187 struct stasis_message_type *stasis_message_type(const struct stasis_message *msg)
188 {
189 if (msg == NULL) {
190 return NULL;
191 }
192 return msg->type;
193 }
194
195 void *stasis_message_data(const struct stasis_message *msg)
196 {
197 if (msg == NULL) {
198 return NULL;
199 }
200 return msg->data;
201 }
202
203 const struct timeval *stasis_message_timestamp(const struct stasis_message *msg)
204 {
205 if (msg == NULL) {
206 return NULL;
207 }
208 return &msg->timestamp;
209 }
210
211 #define INVOKE_VIRTUAL(fn, ...) \
212 ({ \
213 if (!msg) { \
214 return NULL; \
215 } \
216 ast_assert(msg->type != NULL); \
217 ast_assert(msg->type->vtable != NULL); \
218 if (!msg->type->vtable->fn) { \
219 return NULL; \
220 } \
221 msg->type->vtable->fn(__VA_ARGS__); \
222 })
223
224 struct ast_manager_event_blob *stasis_message_to_ami(struct stasis_message *msg)
225 {
226 return INVOKE_VIRTUAL(to_ami, msg);
227 }
228
229 struct ast_json *stasis_message_to_json(
230 struct stasis_message *msg,
231 struct stasis_message_sanitizer *sanitize)
232 {
233 return INVOKE_VIRTUAL(to_json, msg, sanitize);
234 }
235
236 struct ast_event *stasis_message_to_event(struct stasis_message *msg)
237 {
238 return INVOKE_VIRTUAL(to_event, msg);
239 }
240
241 #define HAS_VIRTUAL(fn, msg) \
242 ({ \
243 if (!msg) { \
244 return 0; \
245 } \
246 ast_assert(msg->type != NULL); \
247 ast_assert(msg->type->vtable != NULL); \
248 !!msg->type->vtable->fn; \
249 })
250
251 int stasis_message_can_be_ami(struct stasis_message *msg)
252 {
253 return HAS_VIRTUAL(to_ami, msg);
254 }