summaryrefslogtreecommitdiff
path: root/servers.c
blob: 21013952efd99159d1768bd59ee71951a0f84963 (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
#include "servers.h"
#include "conf.h"

#include <gio/gio.h>

GSList *servers = NULL;

gboolean server_add(const gchar *host, const guint16 http_port,
		const guint16 command_port) {
	struct server *server = g_new0(struct server, 1);

	if(server == NULL) {
		g_warning("server_add: g_new0 returned NULL");
		return FALSE;
	}

	server->host = g_strdup(host);
	server->http_port = http_port;
	server->command_port = command_port;

	servers = g_slist_prepend(servers, server);

	return TRUE;
}

gboolean server_remove(struct server *server) {
	gint pos = g_slist_index(servers, server);

	if(pos == -1) {
		return FALSE;
	}

	servers = g_slist_remove(servers, server);

	return TRUE;
}

void servers_init() {
	gsize length;
	gchar **s = conf_get_string_list("audist", "servers", &length);
	for(int i = 0; i < length; i++) {
		gchar *section = g_strdup_printf("server/%s", s[i]);
		guint16 http_port = conf_get_int(section, "http_port");
		guint16 command_port = conf_get_int(section, "command_port");
		g_free(section);

		server_add(s[i], http_port, command_port);
		g_debug("  server %d: %s (%d, %d)", i, s[i], http_port, command_port);
	}
}

void servers_free() {
	for(GSList *node = servers; node; node = g_slist_next(node)) {
		struct server *s = node->data;
		g_free(s->host);
	}
	g_slist_free(servers);
	servers = NULL;
}