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

#include <gio/gio.h>

GSList *servers = NULL;

gboolean server_add(const gchar *name, 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->name = g_strdup(name);
	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]);
		gchar *host = conf_get_string(section, "host");
		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], host, http_port, command_port);
		g_free(host);
	}

	if(s != NULL) {
		g_strfreev(s);
	}
}

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