aboutsummaryrefslogtreecommitdiffstats
path: root/res/ais/evt.c
blob: 7e8c8adf6b114a69fc7a6b556a19c9d249c2c6e4 (plain)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2007, Digium, Inc.
 *
 * Russell Bryant <russell@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
 * \author Russell Bryant <russell@digium.com>
 *
 * \brief Usage of the SAForum AIS (Application Interface Specification)
 *
 * \arg http://www.openais.org/
 *
 * This file contains the code specific to the use of the EVT 
 * (Event) Service.
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$");

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "ais.h"

#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "asterisk/cli.h"
#include "asterisk/logger.h"
#include "asterisk/event.h"
#include "asterisk/config.h"
#include "asterisk/linkedlists.h"
#include "asterisk/devicestate.h"

#ifndef AST_MODULE
/* XXX HACK */
#define AST_MODULE "res_ais"
#endif

SaEvtHandleT evt_handle;

void evt_channel_open_cb(SaInvocationT invocation, SaEvtChannelHandleT channel_handle,
	SaAisErrorT error);
void evt_event_deliver_cb(SaEvtSubscriptionIdT subscription_id,
	const SaEvtEventHandleT event_handle, const SaSizeT event_datalen);

static const SaEvtCallbacksT evt_callbacks = {
	.saEvtChannelOpenCallback  = evt_channel_open_cb,
	.saEvtEventDeliverCallback = evt_event_deliver_cb, 
};

static const struct {
	const char *str;
	enum ast_event_type type;
} supported_event_types[] = {
	{ "mwi", AST_EVENT_MWI },
	{ "device_state", AST_EVENT_DEVICE_STATE_CHANGE },
};

/*! Used to provide unique id's to egress subscriptions */
static int unique_id;

struct subscribe_event {
	AST_LIST_ENTRY(subscribe_event) entry;
	/*! This is a unique identifier to identify this subscription in the event
	 *  channel through the different API calls, subscribe, unsubscribe, and
	 *  the event deliver callback. */
	SaEvtSubscriptionIdT id;
	enum ast_event_type type;
};

struct publish_event {
	AST_LIST_ENTRY(publish_event) entry;
	/*! We subscribe to events internally so that we can publish them
	 *  on this event channel. */
	struct ast_event_sub *sub;
	enum ast_event_type type;
};

struct event_channel {
	AST_RWLIST_ENTRY(event_channel) entry;
	AST_LIST_HEAD_NOLOCK(, subscribe_event) subscribe_events;
	AST_LIST_HEAD_NOLOCK(, publish_event) publish_events;
	SaEvtChannelHandleT handle;
	char name[1];
};

static AST_RWLIST_HEAD_STATIC(event_channels, event_channel);

void evt_channel_open_cb(SaInvocationT invocation, SaEvtChannelHandleT channel_handle,
	SaAisErrorT error)
{

}

static void queue_event(struct ast_event *ast_event)
{
	ast_event_queue_and_cache(ast_event);
}

void evt_event_deliver_cb(SaEvtSubscriptionIdT sub_id,
	const SaEvtEventHandleT event_handle, const SaSizeT event_datalen)
{
	/* It is important to note that this works because we *know* that this
	 * function will only be called by a single thread, the dispatch_thread.
	 * If this module gets changed such that this is no longer the case, this
	 * should get changed to a thread-local buffer, instead. */
	static unsigned char buf[4096];
	struct ast_event *event_dup, *event = (void *) buf;
	SaAisErrorT ais_res;
	SaSizeT len = sizeof(buf);

	if (event_datalen > len) {
		ast_log(LOG_ERROR, "Event received with size %u, which is too big\n"
			"for the allocated size %u. Change the code to increase the size.\n",
			(unsigned int) event_datalen, (unsigned int) len);
		return;
	}

	ais_res = saEvtEventDataGet(event_handle, event, &len);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error retrieving event payload: %s\n", 
			ais_err2str(ais_res));
		return;
	}

	if (!ast_eid_cmp(&g_eid, ast_event_get_ie_raw(event, AST_EVENT_IE_EID))) {
		/* Don't feed events back in that originated locally. */
		return;
	}

	if (!(event_dup = ast_malloc(len)))
		return;
	
	memcpy(event_dup, event, len);

	queue_event(event_dup);
}

static const char *type_to_filter_str(enum ast_event_type type)
{
	const char *filter_str = NULL;
	int i;

	for (i = 0; i < ARRAY_LEN(supported_event_types); i++) {
		if (supported_event_types[i].type == type) {
			filter_str = supported_event_types[i].str;
			break;
		}
	}

	return filter_str;
}

static void ast_event_cb(const struct ast_event *ast_event, void *data)
{
	SaEvtEventHandleT event_handle;
	SaAisErrorT ais_res;
	struct event_channel *event_channel = data;
	SaClmClusterNodeT local_node;
	SaEvtEventPatternArrayT pattern_array;
	SaEvtEventPatternT pattern;
	SaSizeT len;
	const char *filter_str;
	SaEvtEventIdT event_id;

	ast_log(LOG_DEBUG, "Got an event to forward\n");

	if (ast_eid_cmp(&g_eid, ast_event_get_ie_raw(ast_event, AST_EVENT_IE_EID))) {
		/* If the event didn't originate from this server, don't send it back out. */
		ast_log(LOG_DEBUG, "Returning here\n");
		return;
	}

	ais_res = saEvtEventAllocate(event_channel->handle, &event_handle);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error allocating event: %s\n", ais_err2str(ais_res));
		ast_log(LOG_DEBUG, "Returning here\n");
		return;
	}
	
	ais_res = saClmClusterNodeGet(clm_handle, SA_CLM_LOCAL_NODE_ID, 
		SA_TIME_ONE_SECOND, &local_node);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error getting local node name: %s\n", ais_err2str(ais_res));
		goto return_event_free;
	}

	filter_str = type_to_filter_str(ast_event_get_type(ast_event));
	len = strlen(filter_str) + 1;
	pattern.pattern = (SaUint8T *) filter_str;
	pattern.patternSize = len;
	pattern.allocatedSize = len;

	pattern_array.allocatedNumber = 1;
	pattern_array.patternsNumber = 1;
	pattern_array.patterns = &pattern;

	/*! 
	 * /todo Make retention time configurable 
	 * /todo Make event priorities configurable
	 */
	ais_res = saEvtEventAttributesSet(event_handle, &pattern_array,
		SA_EVT_LOWEST_PRIORITY, SA_TIME_ONE_MINUTE, &local_node.nodeName);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error setting event attributes: %s\n", ais_err2str(ais_res));
		goto return_event_free;
	}

	ais_res = saEvtEventPublish(event_handle, 
		ast_event, ast_event_get_size(ast_event), &event_id);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error publishing event: %s\n", ais_err2str(ais_res));
		goto return_event_free;
	}

return_event_free:
	ais_res = saEvtEventFree(event_handle);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error freeing allocated event: %s\n", ais_err2str(ais_res));
	}
	ast_log(LOG_DEBUG, "Returning here (event_free)\n");
}

static char *ais_evt_show_event_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	struct event_channel *event_channel;

	switch (cmd) {
	case CLI_INIT:
		e->command = "ais evt show event channels";
		e->usage =
			"Usage: ais evt show event channels\n"
			"       List configured event channels for the (EVT) Eventing service.\n";
		return NULL;

	case CLI_GENERATE:
		return NULL;	/* no completion */
	}

	if (a->argc != e->args)
		return CLI_SHOWUSAGE;

	ast_cli(a->fd, "\n"
	            "=============================================================\n"
	            "=== Event Channels ==========================================\n"
	            "=============================================================\n"
	            "===\n");

	AST_RWLIST_RDLOCK(&event_channels);
	AST_RWLIST_TRAVERSE(&event_channels, event_channel, entry) {
		struct publish_event *publish_event;
		struct subscribe_event *subscribe_event;

		ast_cli(a->fd, "=== ---------------------------------------------------------\n"
		               "=== Event Channel Name: %s\n", event_channel->name);

		AST_LIST_TRAVERSE(&event_channel->publish_events, publish_event, entry) {
			ast_cli(a->fd, "=== ==> Publishing Event Type: %s\n", 
				type_to_filter_str(publish_event->type));
		}
		
		AST_LIST_TRAVERSE(&event_channel->subscribe_events, subscribe_event, entry) {
			ast_cli(a->fd, "=== ==> Subscribing to Event Type: %s\n", 
				type_to_filter_str(subscribe_event->type));
		}

		ast_cli(a->fd, "=== ---------------------------------------------------------\n"
		               "===\n");
	}
	AST_RWLIST_UNLOCK(&event_channels);

	ast_cli(a->fd, "=============================================================\n"
	               "\n");

	return CLI_SUCCESS;
}

static struct ast_cli_entry ais_cli[] = {
	AST_CLI_DEFINE(ais_evt_show_event_channels, "Show configured event channels"),
};

static void add_publish_event(struct event_channel *event_channel, const char *event_type)
{
	int i;
	enum ast_event_type type = -1;
	struct publish_event *publish_event;

	for (i = 0; i < ARRAY_LEN(supported_event_types); i++) {
		if (!strcasecmp(event_type, supported_event_types[i].str)) {
			type = supported_event_types[i].type;
			break;
		}
	}

	if (type == -1) {
		ast_log(LOG_WARNING, "publish_event option given with invalid value '%s'\n", event_type);
		return;
	}

	if (type == AST_EVENT_DEVICE_STATE_CHANGE && ast_enable_distributed_devstate()) {
		return;
	}

	if (!(publish_event = ast_calloc(1, sizeof(*publish_event)))) {
		return;
	}

	publish_event->type = type;
	ast_log(LOG_DEBUG, "Subscribing to event type %d\n", type);
	publish_event->sub = ast_event_subscribe(type, ast_event_cb, event_channel,
		AST_EVENT_IE_END);
	ast_event_dump_cache(publish_event->sub);

	AST_LIST_INSERT_TAIL(&event_channel->publish_events, publish_event, entry);
}

static SaAisErrorT set_egress_subscription(struct event_channel *event_channel,
	struct subscribe_event *subscribe_event)
{
	SaAisErrorT ais_res;
	SaEvtEventFilterArrayT filter_array;
	SaEvtEventFilterT filter;
	const char *filter_str = NULL;
	SaSizeT len;

	/* We know it's going to be valid.  It was checked earlier. */
	filter_str = type_to_filter_str(subscribe_event->type);

	filter.filterType = SA_EVT_EXACT_FILTER;
	len = strlen(filter_str) + 1;
	filter.filter.allocatedSize = len;
	filter.filter.patternSize = len;
	filter.filter.pattern = (SaUint8T *) filter_str;

	filter_array.filtersNumber = 1;
	filter_array.filters = &filter;

	ais_res = saEvtEventSubscribe(event_channel->handle, &filter_array, 
		subscribe_event->id);

	return ais_res;
}

static void add_subscribe_event(struct event_channel *event_channel, const char *event_type)
{
	int i;
	enum ast_event_type type = -1;
	struct subscribe_event *subscribe_event;
	SaAisErrorT ais_res;

	for (i = 0; i < ARRAY_LEN(supported_event_types); i++) {
		if (!strcasecmp(event_type, supported_event_types[i].str)) {
			type = supported_event_types[i].type;
			break;
		}
	}

	if (type == -1) {
		ast_log(LOG_WARNING, "subscribe_event option given with invalid value '%s'\n", event_type);
		return;
	}

	if (type == AST_EVENT_DEVICE_STATE_CHANGE && ast_enable_distributed_devstate()) {
		return;
	}

	if (!(subscribe_event = ast_calloc(1, sizeof(*subscribe_event)))) {
		return;
	}

	subscribe_event->type = type;
	subscribe_event->id = ast_atomic_fetchadd_int(&unique_id, +1);

	ais_res = set_egress_subscription(event_channel, subscribe_event);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error setting up egress subscription: %s\n",
			ais_err2str(ais_res));
		free(subscribe_event);
		return;
	}

	AST_LIST_INSERT_TAIL(&event_channel->subscribe_events, subscribe_event, entry);
}

static void build_event_channel(struct ast_config *cfg, const char *cat)
{
	struct ast_variable *var;
	struct event_channel *event_channel;
	SaAisErrorT ais_res;
	SaNameT sa_name = { 0, };

	AST_RWLIST_WRLOCK(&event_channels);
	AST_RWLIST_TRAVERSE(&event_channels, event_channel, entry) {
		if (!strcasecmp(event_channel->name, cat))
			break;
	}
	AST_RWLIST_UNLOCK(&event_channels);
	if (event_channel) {
		ast_log(LOG_WARNING, "Event channel '%s' was specified twice in "
			"configuration.  Second instance ignored.\n", cat);
		return;
	}

	if (!(event_channel = ast_calloc(1, sizeof(*event_channel) + strlen(cat))))
		return;

	strcpy(event_channel->name, cat);
	ast_copy_string((char *) sa_name.value, cat, sizeof(sa_name.value));
	sa_name.length = strlen((char *) sa_name.value);
	ais_res = saEvtChannelOpen(evt_handle, &sa_name, 
		SA_EVT_CHANNEL_PUBLISHER | SA_EVT_CHANNEL_SUBSCRIBER | SA_EVT_CHANNEL_CREATE,
		SA_TIME_MAX, &event_channel->handle);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error opening event channel: %s\n", ais_err2str(ais_res));
		free(event_channel);
		return;
	}

	for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
		if (!strcasecmp(var->name, "type")) {
			continue;
		} else if (!strcasecmp(var->name, "publish_event")) {
			add_publish_event(event_channel, var->value);
		} else if (!strcasecmp(var->name, "subscribe_event")) {
			add_subscribe_event(event_channel, var->value);
		} else {
			ast_log(LOG_WARNING, "Event channel '%s' contains invalid option '%s'\n",
				event_channel->name, var->name);
		}
	}

	AST_RWLIST_WRLOCK(&event_channels);
	AST_RWLIST_INSERT_TAIL(&event_channels, event_channel, entry);
	AST_RWLIST_UNLOCK(&event_channels);
}

static void load_config(void)
{
	static const char filename[] = "ais.conf";
	struct ast_config *cfg;
	const char *cat = NULL;
	struct ast_flags config_flags = { 0 };

	if (!(cfg = ast_config_load(filename, config_flags)))
		return;

	while ((cat = ast_category_browse(cfg, cat))) {
		const char *type;

		if (!strcasecmp(cat, "general"))
			continue;

		if (!(type = ast_variable_retrieve(cfg, cat, "type"))) {
			ast_log(LOG_WARNING, "Invalid entry in %s defined with no type!\n",
				filename);
			continue;
		}

		if (!strcasecmp(type, "event_channel")) {
			build_event_channel(cfg, cat);
		} else {
			ast_log(LOG_WARNING, "Entry in %s defined with invalid type '%s'\n", 
				filename, type);
		}
	}

	ast_config_destroy(cfg);
}

static void publish_event_destroy(struct publish_event *publish_event)
{
	ast_event_unsubscribe(publish_event->sub);

	free(publish_event);
}

static void subscribe_event_destroy(const struct event_channel *event_channel,
	struct subscribe_event *subscribe_event)
{
	SaAisErrorT ais_res;

	/* saEvtChannelClose() will actually do this automatically, but it just
	 * feels cleaner to go ahead and do it manually ... */
	ais_res = saEvtEventUnsubscribe(event_channel->handle, subscribe_event->id);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error unsubscribing: %s\n", ais_err2str(ais_res));
	}

	free(subscribe_event);
}

static void event_channel_destroy(struct event_channel *event_channel)
{
	struct publish_event *publish_event;
	struct subscribe_event *subscribe_event;
	SaAisErrorT ais_res;

	while ((publish_event = AST_LIST_REMOVE_HEAD(&event_channel->publish_events, entry)))
		publish_event_destroy(publish_event);
	while ((subscribe_event = AST_LIST_REMOVE_HEAD(&event_channel->subscribe_events, entry)))
		subscribe_event_destroy(event_channel, subscribe_event);

	ais_res = saEvtChannelClose(event_channel->handle);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Error closing event channel '%s': %s\n",
			event_channel->name, ais_err2str(ais_res));
	}

	free(event_channel);
}

static void destroy_event_channels(void)
{
	struct event_channel *event_channel;

	AST_RWLIST_WRLOCK(&event_channels);
	while ((event_channel = AST_RWLIST_REMOVE_HEAD(&event_channels, entry)))
		event_channel_destroy(event_channel);
	AST_RWLIST_UNLOCK(&event_channels);
}

int ast_ais_evt_load_module(void)
{
	SaAisErrorT ais_res;

	ais_res = saEvtInitialize(&evt_handle, &evt_callbacks, &ais_version);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Could not initialize eventing service: %s\n",
			ais_err2str(ais_res));
		return -1;
	}
	
	load_config();

	ast_cli_register_multiple(ais_cli, ARRAY_LEN(ais_cli));

	return 0;
}

int ast_ais_evt_unload_module(void)
{
	SaAisErrorT ais_res;

	destroy_event_channels();

	ais_res = saEvtFinalize(evt_handle);
	if (ais_res != SA_AIS_OK) {
		ast_log(LOG_ERROR, "Problem stopping eventing service: %s\n", 
			ais_err2str(ais_res));
		return -1;
	}

	return 0;	
}