From 9558cc7031e84afc00eea62de22a494b34d653e9 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 21 Aug 2010 13:18:36 +0200 Subject: Added a server list command to the control service. --- control_commands.c | 25 +++++++++++++++++++------ servers.c | 3 ++- servers.h | 2 ++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/control_commands.c b/control_commands.c index f5fbc64..61477ac 100644 --- a/control_commands.c +++ b/control_commands.c @@ -1,14 +1,27 @@ #include "control_commands.h" +#include "servers.h" #include #include -void control_commands_handle(GSocketConnection *connection, const gchar *cmd) { - gchar *args = strchr(cmd, ' '); - if(args != NULL) { - *args = '\0'; - args++; +static void list_servers(GSocketConnection *connection, const gchar *cmd) { + GSocket *socket = g_socket_connection_get_socket(connection); + + for(GSList *node = servers; node; node = g_slist_next(node)) { + struct server *s = node->data; + gchar *buffer = g_strdup_printf("%s:%d\n", s->host, s->port); + g_socket_send(socket, buffer, strlen(buffer), NULL, NULL); } +} - g_debug("handling command %s with arguments %s", cmd, args); +void control_commands_handle(GSocketConnection *connection, const gchar *cmd) { + if(g_strcmp0(cmd, "servers") == 0) { + list_servers(connection, cmd); + } else { + g_debug("unknown command"); + gchar *buf = g_strdup_printf("error: unknown command %s\n", cmd); + GSocket *socket = g_socket_connection_get_socket(connection); + g_socket_send(socket, buf, strlen(buf), NULL, NULL); + g_free(buf); + } } diff --git a/servers.c b/servers.c index f46eaae..22e431d 100644 --- a/servers.c +++ b/servers.c @@ -1,7 +1,7 @@ #include "servers.h" #include "conf.h" -static GSList *servers = NULL; +GSList *servers = NULL; gboolean server_add(const gchar *host, const guint16 port) { struct server *server = g_new0(struct server, 1); @@ -35,6 +35,7 @@ void servers_init() { gsize length; gchar **s = conf_get_string_list("audist", "servers", &length); for(int i = 0; i < length; i++) { + server_add(s[i], 0); g_debug(" server %d: %s", i, s[i]); } } diff --git a/servers.h b/servers.h index b6e809b..f727bbd 100644 --- a/servers.h +++ b/servers.h @@ -8,6 +8,8 @@ struct server { guint16 port; }; +extern GSList *servers; + gboolean server_add(const gchar *host, const guint16 port); gboolean server_remove(struct server *server); void servers_init(); -- cgit v1.2.3