Revision d97e0296

View differences:

events.c
17 17

  
18 18
static gboolean eventsenabled = FALSE;
19 19
static GHashTable *eventhandlers = NULL;
20
void janus_events_init(gboolean enabled, GHashTable *handlers) {
21
	eventsenabled = enabled;
20

  
21
static GAsyncQueue *events = NULL;
22
static json_t exit_event;
23

  
24
static GThread *events_thread;
25
void *janus_events_thread(void *data);
26

  
27
int janus_events_init(gboolean enabled, GHashTable *handlers) {
28
	/* We setup a thread for passing events to the handlers */
29
	GError *error = NULL;
30
	events_thread = g_thread_try_new("janus events thread", janus_events_thread, NULL, &error);
31
	if(error != NULL) {
32
		JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the Events handler thread...\n", error->code, error->message ? error->message : "??");
33
		return -1;
34
	}
35
	events = g_async_queue_new();
22 36
	eventhandlers = handlers;
37
	eventsenabled = enabled;
38
	return 0;
39
}
40

  
41
void janus_events_deinit(void) {
42
	eventsenabled = FALSE;
43

  
44
	g_async_queue_push(events, &exit_event);
45
	if(events_thread != NULL) {
46
		g_thread_join(events_thread);
47
		events_thread = NULL;
48
	}
23 49
}
24 50

  
25 51
gboolean janus_events_is_enabled(void) {
......
103 129
	json_object_set_new(event, "event", body);
104 130
	va_end(args);
105 131

  
106
	/* Notify all interested handlers, increasing the event reference to make sure it's not lost because of errors */
107
	GHashTableIter iter;
108
	gpointer value;
109
	g_hash_table_iter_init(&iter, eventhandlers);
110
	json_incref(event);
111
	while(g_hash_table_iter_next(&iter, NULL, &value)) {
112
		janus_eventhandler *e = value;
113
		if(e == NULL)
114
			continue;
115
		if(!janus_flags_is_set(&e->events_mask, type))
132
	/* Enqueue the event */
133
	g_async_queue_push(events, event);
134
}
135

  
136
void *janus_events_thread(void *data) {
137
	JANUS_LOG(LOG_VERB, "Joining Events handler thread\n");
138
	json_t *event = NULL;
139

  
140
	while(eventsenabled) {
141
		/* Any event in queue? */
142
		event = g_async_queue_pop(events);
143
		if(event == NULL)
116 144
			continue;
117
		e->incoming_event(event);
145
		if(event == &exit_event)
146
			break;
147

  
148
		/* Notify all interested handlers, increasing the event reference to make sure it's not lost because of errors */
149
		int type = json_integer_value(json_object_get(event, "type"));
150
		GHashTableIter iter;
151
		gpointer value;
152
		g_hash_table_iter_init(&iter, eventhandlers);
153
		json_incref(event);
154
		while(g_hash_table_iter_next(&iter, NULL, &value)) {
155
			janus_eventhandler *e = value;
156
			if(e == NULL)
157
				continue;
158
			if(!janus_flags_is_set(&e->events_mask, type))
159
				continue;
160
			e->incoming_event(event);
161
		}
162
		json_decref(event);
163

  
164
		/* Unref the final event reference, interested handlers will have their own reference */
165
		json_decref(event);
118 166
	}
119
	json_decref(event);
120
	/* Unref the final event reference, interested handlers will have their own reference */
121
	json_decref(event);
167

  
168
	JANUS_LOG(LOG_VERB, "Leaving EchoTest handler thread\n");
169
	return NULL;
122 170
}
events.h
18 18

  
19 19
/*! \brief Initialize the event handlers broadcaster
20 20
 * @param[in] enabled Whether broadcasting events should be supported at all
21
 * @param[in] handlers Map of all registered event handlers */
22
void janus_events_init(gboolean enabled, GHashTable *handlers);
21
 * @param[in] handlers Map of all registered event handlers
22
 * @returns 0 on success, a negative integer otherwise */
23
int janus_events_init(gboolean enabled, GHashTable *handlers);
24

  
25
/*! \brief De-initialize the event handlers broadcaster */
26
void janus_events_deinit(void);
23 27

  
24 28
/*! \brief Quick method to check whether event handlers are enabled at all or not
25 29
 * @returns TRUE if they're enabled, FALSE if not */
janus.c
3800 3800
			g_strfreev(disabled_eventhandlers);
3801 3801
		disabled_eventhandlers = NULL;
3802 3802
		/* Initialize the event broadcaster */
3803
		janus_events_init(enable_events, eventhandlers);
3803
		if(janus_events_init(enable_events, eventhandlers) < 0) {
3804
			JANUS_LOG(LOG_FATAL, "Error initializing the Event handlers mechanism...\n");
3805
			exit(1);
3806
		}
3804 3807
	}
3805 3808

  
3806 3809
	/* Load plugins */
......
4125 4128
	}
4126 4129

  
4127 4130
	JANUS_LOG(LOG_INFO, "Closing event handlers:\n");
4131
	janus_events_deinit();
4128 4132
	if(eventhandlers != NULL) {
4129 4133
		g_hash_table_foreach(eventhandlers, janus_eventhandler_close, NULL);
4130 4134
		g_hash_table_destroy(eventhandlers);

Also available in: Unified diff