Revision d97e0296 events.c

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
}

Also available in: Unified diff