stasis: Add internal filtering of messages.
[asterisk/asterisk.git] / main / stasis_message_router.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 message router implementation.
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 #include "asterisk/astobj2.h"
33 #include "asterisk/stasis_message_router.h"
34 #include "asterisk/vector.h"
35
36 /*! \internal */
37 struct stasis_message_route {
38         /*! Message type handle by this route. */
39         struct stasis_message_type *message_type;
40         /*! Callback function for incoming message processing. */
41         stasis_subscription_cb callback;
42         /*! Data pointer to be handed to the callback. */
43         void *data;
44 };
45
46 AST_VECTOR(route_table, struct stasis_message_route);
47
48 static struct stasis_message_route *route_table_find(struct route_table *table,
49         struct stasis_message_type *message_type)
50 {
51         size_t idx;
52         struct stasis_message_route *route;
53
54         /* While a linear search for routes may seem very inefficient, most
55          * route tables have six routes or less. For such small data, it's
56          * hard to beat a linear search. If we start having larger route
57          * tables, then we can look into containers with more efficient
58          * lookups.
59          */
60         for (idx = 0; idx < AST_VECTOR_SIZE(table); ++idx) {
61                 route = AST_VECTOR_GET_ADDR(table, idx);
62                 if (route->message_type == message_type) {
63                         return route;
64                 }
65         }
66
67         return NULL;
68 }
69
70 /*!
71  * \brief route_table comparator for AST_VECTOR_REMOVE_CMP_UNORDERED()
72  *
73  * \param elem Element to compare against
74  * \param value Value to compare with the vector element.
75  *
76  * \return 0 if element does not match.
77  * \return Non-zero if element matches.
78  */
79 #define ROUTE_TABLE_ELEM_CMP(elem, value) ((elem).message_type == (value))
80
81 /*!
82  * \brief route_table vector element cleanup.
83  *
84  * \param elem Element to cleanup
85  *
86  * \return Nothing
87  */
88 #define ROUTE_TABLE_ELEM_CLEANUP(elem)  ao2_cleanup((elem).message_type)
89
90 static int route_table_remove(struct route_table *table,
91         struct stasis_message_type *message_type)
92 {
93         return AST_VECTOR_REMOVE_CMP_UNORDERED(table, message_type, ROUTE_TABLE_ELEM_CMP,
94                 ROUTE_TABLE_ELEM_CLEANUP);
95 }
96
97 static int route_table_add(struct route_table *table,
98         struct stasis_message_type *message_type,
99         stasis_subscription_cb callback, void *data)
100 {
101         struct stasis_message_route route;
102         int res;
103
104         ast_assert(callback != NULL);
105         ast_assert(route_table_find(table, message_type) == NULL);
106
107         route.message_type = ao2_bump(message_type);
108         route.callback = callback;
109         route.data = data;
110
111         res = AST_VECTOR_APPEND(table, route);
112         if (res) {
113                 ROUTE_TABLE_ELEM_CLEANUP(route);
114         }
115         return res;
116 }
117
118 static void route_table_dtor(struct route_table *table)
119 {
120         size_t idx;
121         struct stasis_message_route *route;
122
123         for (idx = 0; idx < AST_VECTOR_SIZE(table); ++idx) {
124                 route = AST_VECTOR_GET_ADDR(table, idx);
125                 ROUTE_TABLE_ELEM_CLEANUP(*route);
126         }
127         AST_VECTOR_FREE(table);
128 }
129
130 /*! \internal */
131 struct stasis_message_router {
132         /*! Subscription to the upstream topic */
133         struct stasis_subscription *subscription;
134         /*! Subscribed routes */
135         struct route_table routes;
136         /*! Subscribed routes for \ref stasis_cache_update messages */
137         struct route_table cache_routes;
138         /*! Route of last resort */
139         struct stasis_message_route default_route;
140 };
141
142 static void router_dtor(void *obj)
143 {
144         struct stasis_message_router *router = obj;
145
146         ast_assert(!stasis_subscription_is_subscribed(router->subscription));
147         ast_assert(stasis_subscription_is_done(router->subscription));
148
149         router->subscription = NULL;
150
151         route_table_dtor(&router->routes);
152         route_table_dtor(&router->cache_routes);
153 }
154
155 static int find_route(
156         struct stasis_message_router *router,
157         struct stasis_message *message,
158         struct stasis_message_route *route_out)
159 {
160         struct stasis_message_route *route = NULL;
161         struct stasis_message_type *type = stasis_message_type(message);
162         SCOPED_AO2LOCK(lock, router);
163
164         ast_assert(route_out != NULL);
165
166         if (type == stasis_cache_update_type()) {
167                 /* Find a cache route */
168                 struct stasis_cache_update *update =
169                         stasis_message_data(message);
170                 route = route_table_find(&router->cache_routes, update->type);
171         }
172
173         if (route == NULL) {
174                 /* Find a regular route */
175                 route = route_table_find(&router->routes, type);
176         }
177
178         if (route == NULL && router->default_route.callback) {
179                 /* Maybe the default route, then? */
180                 route = &router->default_route;
181         }
182
183         if (!route) {
184                 return -1;
185         }
186
187         *route_out = *route;
188         return 0;
189 }
190
191 static void router_dispatch(void *data,
192                             struct stasis_subscription *sub,
193                             struct stasis_message *message)
194 {
195         struct stasis_message_router *router = data;
196         struct stasis_message_route route;
197
198         if (find_route(router, message, &route) == 0) {
199                 route.callback(route.data, sub, message);
200         }
201
202         if (stasis_subscription_final_message(sub, message)) {
203                 ao2_cleanup(router);
204         }
205 }
206
207 static struct stasis_message_router *stasis_message_router_create_internal(
208         struct stasis_topic *topic, int use_thread_pool)
209 {
210         int res;
211         struct stasis_message_router *router;
212
213         router = ao2_t_alloc(sizeof(*router), router_dtor, stasis_topic_name(topic));
214         if (!router) {
215                 return NULL;
216         }
217
218         res = 0;
219         res |= AST_VECTOR_INIT(&router->routes, 0);
220         res |= AST_VECTOR_INIT(&router->cache_routes, 0);
221         if (res) {
222                 ao2_ref(router, -1);
223
224                 return NULL;
225         }
226
227         if (use_thread_pool) {
228                 router->subscription = stasis_subscribe_pool(topic, router_dispatch, router);
229         } else {
230                 router->subscription = stasis_subscribe(topic, router_dispatch, router);
231         }
232         if (!router->subscription) {
233                 ao2_ref(router, -1);
234
235                 return NULL;
236         }
237
238         /* We need to receive subscription change messages so we know when our subscription goes away */
239         stasis_subscription_accept_message_type(router->subscription, stasis_subscription_change_type());
240
241         return router;
242 }
243
244 struct stasis_message_router *stasis_message_router_create(
245         struct stasis_topic *topic)
246 {
247         return stasis_message_router_create_internal(topic, 0);
248 }
249
250 struct stasis_message_router *stasis_message_router_create_pool(
251         struct stasis_topic *topic)
252 {
253         return stasis_message_router_create_internal(topic, 1);
254 }
255
256 void stasis_message_router_unsubscribe(struct stasis_message_router *router)
257 {
258         if (!router) {
259                 return;
260         }
261
262         ao2_lock(router);
263         router->subscription = stasis_unsubscribe(router->subscription);
264         ao2_unlock(router);
265 }
266
267 void stasis_message_router_unsubscribe_and_join(
268         struct stasis_message_router *router)
269 {
270         if (!router) {
271                 return;
272         }
273         stasis_unsubscribe_and_join(router->subscription);
274 }
275
276 int stasis_message_router_is_done(struct stasis_message_router *router)
277 {
278         if (!router) {
279                 /* Null router is about as done as you can get */
280                 return 1;
281         }
282
283         return stasis_subscription_is_done(router->subscription);
284 }
285
286 void stasis_message_router_publish_sync(struct stasis_message_router *router,
287         struct stasis_message *message)
288 {
289         ast_assert(router != NULL);
290
291         ao2_bump(router);
292         stasis_publish_sync(router->subscription, message);
293         ao2_cleanup(router);
294 }
295
296 int stasis_message_router_set_congestion_limits(struct stasis_message_router *router,
297         long low_water, long high_water)
298 {
299         int res = -1;
300
301         if (router) {
302                 res = stasis_subscription_set_congestion_limits(router->subscription,
303                         low_water, high_water);
304         }
305         return res;
306 }
307
308 int stasis_message_router_add(struct stasis_message_router *router,
309         struct stasis_message_type *message_type,
310         stasis_subscription_cb callback, void *data)
311 {
312         int res;
313
314         ast_assert(router != NULL);
315
316         if (!message_type) {
317                 /* Cannot route to NULL type. */
318                 return -1;
319         }
320         ao2_lock(router);
321         res = route_table_add(&router->routes, message_type, callback, data);
322         if (!res) {
323                 stasis_subscription_accept_message_type(router->subscription, message_type);
324                 /* Until a specific message type was added we would already drop the message, so being
325                  * selective now doesn't harm us. If we have a default route then we are already forced
326                  * to filter nothing and messages will come in regardless.
327                  */
328                 stasis_subscription_set_filter(router->subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
329         }
330         ao2_unlock(router);
331         return res;
332 }
333
334 int stasis_message_router_add_cache_update(struct stasis_message_router *router,
335         struct stasis_message_type *message_type,
336         stasis_subscription_cb callback, void *data)
337 {
338         int res;
339
340         ast_assert(router != NULL);
341
342         if (!message_type) {
343                 /* Cannot cache a route to NULL type. */
344                 return -1;
345         }
346         ao2_lock(router);
347         res = route_table_add(&router->cache_routes, message_type, callback, data);
348         if (!res) {
349                 stasis_subscription_accept_message_type(router->subscription, stasis_cache_update_type());
350                 stasis_subscription_set_filter(router->subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
351         }
352         ao2_unlock(router);
353         return res;
354 }
355
356 void stasis_message_router_remove(struct stasis_message_router *router,
357         struct stasis_message_type *message_type)
358 {
359         ast_assert(router != NULL);
360
361         if (!message_type) {
362                 /* Cannot remove a NULL type. */
363                 return;
364         }
365         ao2_lock(router);
366         route_table_remove(&router->routes, message_type);
367         ao2_unlock(router);
368 }
369
370 void stasis_message_router_remove_cache_update(
371         struct stasis_message_router *router,
372         struct stasis_message_type *message_type)
373 {
374         ast_assert(router != NULL);
375
376         if (!message_type) {
377                 /* Cannot remove a NULL type. */
378                 return;
379         }
380         ao2_lock(router);
381         route_table_remove(&router->cache_routes, message_type);
382         ao2_unlock(router);
383 }
384
385 int stasis_message_router_set_default(struct stasis_message_router *router,
386         stasis_subscription_cb callback,
387         void *data)
388 {
389         ast_assert(router != NULL);
390         ast_assert(callback != NULL);
391
392         ao2_lock(router);
393         router->default_route.callback = callback;
394         router->default_route.data = data;
395         ao2_unlock(router);
396
397         stasis_subscription_set_filter(router->subscription, STASIS_SUBSCRIPTION_FILTER_FORCED_NONE);
398
399         /* While this implementation can never fail, it used to be able to */
400         return 0;
401 }