~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/main/stasis_message_router.c -rw-r--r-- 11.7 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * David M. Lee, II <dlee@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Stasis message router implementation.
 *
 * \author David M. Lee, II <dlee@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/astobj2.h"
#include "asterisk/stasis_message_router.h"
#include "asterisk/vector.h"

/*! \internal */
struct stasis_message_route {
	/*! Message type handle by this route. */
	struct stasis_message_type *message_type;
	/*! Callback function for incoming message processing. */
	stasis_subscription_cb callback;
	/*! Data pointer to be handed to the callback. */
	void *data;
};

AST_VECTOR(route_table, struct stasis_message_route);

static struct stasis_message_route *route_table_find(struct route_table *table,
	struct stasis_message_type *message_type)
{
	size_t idx;
	struct stasis_message_route *route;

	/* While a linear search for routes may seem very inefficient, most
	 * route tables have six routes or less. For such small data, it's
	 * hard to beat a linear search. If we start having larger route
	 * tables, then we can look into containers with more efficient
	 * lookups.
	 */
	for (idx = 0; idx < AST_VECTOR_SIZE(table); ++idx) {
		route = AST_VECTOR_GET_ADDR(table, idx);
		if (route->message_type == message_type) {
			return route;
		}
	}

	return NULL;
}

/*!
 * \brief route_table comparator for AST_VECTOR_REMOVE_CMP_UNORDERED()
 *
 * \param elem Element to compare against
 * \param value Value to compare with the vector element.
 *
 * \return 0 if element does not match.
 * \return Non-zero if element matches.
 */
#define ROUTE_TABLE_ELEM_CMP(elem, value) ((elem).message_type == (value))

/*!
 * \brief route_table vector element cleanup.
 *
 * \param elem Element to cleanup
 *
 * \return Nothing
 */
#define ROUTE_TABLE_ELEM_CLEANUP(elem)  ao2_cleanup((elem).message_type)

static int route_table_remove(struct route_table *table,
	struct stasis_message_type *message_type)
{
	return AST_VECTOR_REMOVE_CMP_UNORDERED(table, message_type, ROUTE_TABLE_ELEM_CMP,
		ROUTE_TABLE_ELEM_CLEANUP);
}

static int route_table_add(struct route_table *table,
	struct stasis_message_type *message_type,
	stasis_subscription_cb callback, void *data)
{
	struct stasis_message_route route;
	int res;

	ast_assert(callback != NULL);
	ast_assert(route_table_find(table, message_type) == NULL);

	route.message_type = ao2_bump(message_type);
	route.callback = callback;
	route.data = data;

	res = AST_VECTOR_APPEND(table, route);
	if (res) {
		ROUTE_TABLE_ELEM_CLEANUP(route);
	}
	return res;
}

static void route_table_dtor(struct route_table *table)
{
	size_t idx;
	struct stasis_message_route *route;

	for (idx = 0; idx < AST_VECTOR_SIZE(table); ++idx) {
		route = AST_VECTOR_GET_ADDR(table, idx);
		ROUTE_TABLE_ELEM_CLEANUP(*route);
	}
	AST_VECTOR_FREE(table);
}

/*! \internal */
struct stasis_message_router {
	/*! Subscription to the upstream topic */
	struct stasis_subscription *subscription;
	/*! Subscribed routes */
	struct route_table routes;
	/*! Subscribed routes for \ref stasis_cache_update messages */
	struct route_table cache_routes;
	/*! Route of last resort */
	struct stasis_message_route default_route;
};

static void router_dtor(void *obj)
{
	struct stasis_message_router *router = obj;

	ast_assert(!stasis_subscription_is_subscribed(router->subscription));
	ast_assert(stasis_subscription_is_done(router->subscription));

	router->subscription = NULL;

	route_table_dtor(&router->routes);
	route_table_dtor(&router->cache_routes);
}

static int find_route(
	struct stasis_message_router *router,
	struct stasis_message *message,
	struct stasis_message_route *route_out)
{
	struct stasis_message_route *route = NULL;
	struct stasis_message_type *type = stasis_message_type(message);
	SCOPED_AO2LOCK(lock, router);

	ast_assert(route_out != NULL);

	if (type == stasis_cache_update_type()) {
		/* Find a cache route */
		struct stasis_cache_update *update =
			stasis_message_data(message);
		route = route_table_find(&router->cache_routes, update->type);
	}

	if (route == NULL) {
		/* Find a regular route */
		route = route_table_find(&router->routes, type);
	}

	if (route == NULL && router->default_route.callback) {
		/* Maybe the default route, then? */
		route = &router->default_route;
	}

	if (!route) {
		return -1;
	}

	*route_out = *route;
	return 0;
}

static void router_dispatch(void *data,
			    struct stasis_subscription *sub,
			    struct stasis_message *message)
{
	struct stasis_message_router *router = data;
	struct stasis_message_route route;

	if (find_route(router, message, &route) == 0) {
		route.callback(route.data, sub, message);
	}

	if (stasis_subscription_final_message(sub, message)) {
		ao2_cleanup(router);
	}
}

static struct stasis_message_router *stasis_message_router_create_internal(
	struct stasis_topic *topic, int use_thread_pool, const char *file, int lineno,
	const char *func)
{
	int res;
	struct stasis_message_router *router;

	router = ao2_t_alloc(sizeof(*router), router_dtor, stasis_topic_name(topic));
	if (!router) {
		return NULL;
	}

	res = 0;
	res |= AST_VECTOR_INIT(&router->routes, 0);
	res |= AST_VECTOR_INIT(&router->cache_routes, 0);
	if (res) {
		ao2_ref(router, -1);

		return NULL;
	}

	if (use_thread_pool) {
		router->subscription = __stasis_subscribe_pool(topic, router_dispatch, router, file, lineno, func);
	} else {
		router->subscription = __stasis_subscribe(topic, router_dispatch, router, file, lineno, func);
	}

	if (!router->subscription) {
		ao2_ref(router, -1);

		return NULL;
	}

	/* We need to receive subscription change messages so we know when our subscription goes away */
	stasis_subscription_accept_message_type(router->subscription, stasis_subscription_change_type());

	return router;
}

struct stasis_message_router *__stasis_message_router_create(
	struct stasis_topic *topic, const char *file, int lineno, const char *func)
{
	return stasis_message_router_create_internal(topic, 0, file, lineno, func);
}

struct stasis_message_router *__stasis_message_router_create_pool(
	struct stasis_topic *topic, const char *file, int lineno, const char *func)
{
	return stasis_message_router_create_internal(topic, 1, file, lineno, func);
}

void stasis_message_router_unsubscribe(struct stasis_message_router *router)
{
	if (!router) {
		return;
	}

	ao2_lock(router);
	router->subscription = stasis_unsubscribe(router->subscription);
	ao2_unlock(router);
}

void stasis_message_router_unsubscribe_and_join(
	struct stasis_message_router *router)
{
	if (!router) {
		return;
	}
	stasis_unsubscribe_and_join(router->subscription);
}

int stasis_message_router_is_done(struct stasis_message_router *router)
{
	if (!router) {
		/* Null router is about as done as you can get */
		return 1;
	}

	return stasis_subscription_is_done(router->subscription);
}

void stasis_message_router_publish_sync(struct stasis_message_router *router,
	struct stasis_message *message)
{
	ast_assert(router != NULL);

	ao2_bump(router);
	stasis_publish_sync(router->subscription, message);
	ao2_cleanup(router);
}

int stasis_message_router_set_congestion_limits(struct stasis_message_router *router,
	long low_water, long high_water)
{
	int res = -1;

	if (router) {
		res = stasis_subscription_set_congestion_limits(router->subscription,
			low_water, high_water);
	}
	return res;
}

int stasis_message_router_add(struct stasis_message_router *router,
	struct stasis_message_type *message_type,
	stasis_subscription_cb callback, void *data)
{
	int res;

	ast_assert(router != NULL);

	if (!message_type) {
		/* Cannot route to NULL type. */
		return -1;
	}
	ao2_lock(router);
	res = route_table_add(&router->routes, message_type, callback, data);
	if (!res) {
		stasis_subscription_accept_message_type(router->subscription, message_type);
		/* Until a specific message type was added we would already drop the message, so being
		 * selective now doesn't harm us. If we have a default route then we are already forced
		 * to filter nothing and messages will come in regardless.
		 */
		stasis_subscription_set_filter(router->subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
	}
	ao2_unlock(router);
	return res;
}

int stasis_message_router_add_cache_update(struct stasis_message_router *router,
	struct stasis_message_type *message_type,
	stasis_subscription_cb callback, void *data)
{
	int res;

	ast_assert(router != NULL);

	if (!message_type) {
		/* Cannot cache a route to NULL type. */
		return -1;
	}
	ao2_lock(router);
	res = route_table_add(&router->cache_routes, message_type, callback, data);
	if (!res) {
		stasis_subscription_accept_message_type(router->subscription, stasis_cache_update_type());
		stasis_subscription_set_filter(router->subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
	}
	ao2_unlock(router);
	return res;
}

void stasis_message_router_remove(struct stasis_message_router *router,
	struct stasis_message_type *message_type)
{
	ast_assert(router != NULL);

	if (!message_type) {
		/* Cannot remove a NULL type. */
		return;
	}
	ao2_lock(router);
	route_table_remove(&router->routes, message_type);
	ao2_unlock(router);
}

void stasis_message_router_remove_cache_update(
	struct stasis_message_router *router,
	struct stasis_message_type *message_type)
{
	ast_assert(router != NULL);

	if (!message_type) {
		/* Cannot remove a NULL type. */
		return;
	}
	ao2_lock(router);
	route_table_remove(&router->cache_routes, message_type);
	ao2_unlock(router);
}

int stasis_message_router_set_default(struct stasis_message_router *router,
	stasis_subscription_cb callback,
	void *data)
{
	stasis_message_router_set_formatters_default(router, callback, data, STASIS_SUBSCRIPTION_FORMATTER_NONE);

	/* While this implementation can never fail, it used to be able to */
	return 0;
}

void stasis_message_router_set_formatters_default(struct stasis_message_router *router,
	stasis_subscription_cb callback,
	void *data,
	enum stasis_subscription_message_formatters formatters)
{
	ast_assert(router != NULL);
	ast_assert(callback != NULL);

	stasis_subscription_accept_formatters(router->subscription, formatters);

	ao2_lock(router);
	router->default_route.callback = callback;
	router->default_route.data = data;
	ao2_unlock(router);

	if (formatters == STASIS_SUBSCRIPTION_FORMATTER_NONE) {
		/* Formatters govern what messages the default callback get, so it is only if none is
		 * specified that we accept all messages regardless.
		 */
		stasis_subscription_set_filter(router->subscription, STASIS_SUBSCRIPTION_FILTER_FORCED_NONE);
	}
}

void stasis_message_router_accept_formatters(struct stasis_message_router *router,
	enum stasis_subscription_message_formatters formatters)
{
	ast_assert(router != NULL);

	stasis_subscription_accept_formatters(router->subscription, formatters);

	return;
}

#ifdef AST_DEVMODE
#undef stasis_message_router_create
struct stasis_message_router *stasis_message_router_create(
	struct stasis_topic *topic);
#undef stasis_message_router_create_pool
struct stasis_message_router *stasis_message_router_create_pool(
	struct stasis_topic *topic);
#endif
struct stasis_message_router *stasis_message_router_create(
	struct stasis_topic *topic)
{
	return stasis_message_router_create_internal(topic, 0, __FILE__, __LINE__, __PRETTY_FUNCTION__);
}

struct stasis_message_router *stasis_message_router_create_pool(
	struct stasis_topic *topic)
{
	return stasis_message_router_create_internal(topic, 1, __FILE__, __LINE__, __PRETTY_FUNCTION__);
}