summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-26 22:36:07 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-26 22:36:07 +0200
commit9c7db46be9933e36e79b4caac86a5bdc3183122d (patch)
tree7d31a4ed584d975befe634207ac84925b21ee592
parent3991db2df701b5b6a0401e6974811db3e15edb39 (diff)
Merged commands.c and control_commands.c.
-rw-r--r--command_service.c12
-rw-r--r--commands.c41
-rw-r--r--commands.h2
-rw-r--r--control_commands.c89
-rw-r--r--control_commands.h9
-rw-r--r--control_service.c15
6 files changed, 40 insertions, 128 deletions
diff --git a/command_service.c b/command_service.c
index 5d357da..efb350c 100644
--- a/command_service.c
+++ b/command_service.c
@@ -2,6 +2,7 @@
#include "commands.h"
#include <gio/gio.h>
+#include <string.h>
static GSocketService *ss = NULL;
@@ -30,7 +31,16 @@ static gboolean service_incoming(GSocketService *service,
while((eol = g_strstr_len(pos, size, "\n")) != NULL || (eol = g_strstr_len(pos, size, "\r")) != NULL) {
*eol = '\0';
- commands_handle(connection, pos);
+ commands_handle(connection, pos, &error);
+
+ if(error != NULL) {
+ g_warning(error->message);
+ gchar *error_buffer = g_strdup_printf("%s\n", error->message);
+ g_socket_send(socket, error_buffer, strlen(error_buffer), NULL, NULL);
+ g_free(error_buffer);
+ g_error_free(error);
+ error = NULL;
+ }
size -= eol - pos + 1;
pos = eol + 1;
diff --git a/commands.c b/commands.c
index 3ff250d..db7e133 100644
--- a/commands.c
+++ b/commands.c
@@ -3,8 +3,7 @@
#include <string.h>
-static void commands_list(GSocketConnection *connection, const gchar *cmd) {
- GError *error = NULL;
+static void commands_list(GSocketConnection *connection, const gchar *cmd, GError **error) {
gchar **data = g_strsplit(cmd, " ", 2);
for(gint i = 0; data[i]; i++) {
g_debug("\tdata[%d] = %s", i, data[i]);
@@ -46,16 +45,14 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd) {
GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL,
- &error) == FALSE) {
- g_warning(error->message);
- g_error_free(error);
+ error) == FALSE) {
+ return;
}
g_string_free(string, TRUE);
}
-static void commands_find(GSocketConnection *connection, const gchar *cmd) {
- GError *error = NULL;
+static void commands_find(GSocketConnection *connection, const gchar *cmd, GError **error) {
gchar **data = g_strsplit(cmd, " ", 3);
if(g_strv_length(data) != 3) {
@@ -88,42 +85,34 @@ static void commands_find(GSocketConnection *connection, const gchar *cmd) {
GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL,
- &error) == FALSE) {
- g_warning(error->message);
- g_error_free(error);
+ error) == FALSE) {
+ return;
}
g_string_free(string, TRUE);
}
-static void commands_ping(GSocketConnection *connection, const gchar *cmd) {
- GError *error = NULL;
+static void commands_ping(GSocketConnection *connection, const gchar *cmd, GError **error) {
GSocket *socket = g_socket_connection_get_socket(connection);
- if(g_socket_send(socket, "pong\n", 5, NULL, NULL) == -1) {
- g_warning(error->message);
- g_error_free(error);
- }
+ g_socket_send(socket, "pong\n", 5, NULL, error);
}
-static void commands_exit(GSocketConnection *connection, const gchar *cmd) {
+static void commands_exit(GSocketConnection *connection, const gchar *cmd, GError **error) {
GSocket *socket = g_socket_connection_get_socket(connection);
g_socket_close(socket, NULL);
}
-void commands_handle(GSocketConnection *connection, const gchar *cmd) {
+void commands_handle(GSocketConnection *connection, const gchar *cmd, GError **error) {
g_debug(cmd);
if(g_strncasecmp(cmd, "ping", 4) == 0) {
- commands_ping(connection, cmd);
+ commands_ping(connection, cmd, error);
} else if(g_strncasecmp(cmd, "list", 4) == 0) {
- commands_list(connection, cmd);
+ commands_list(connection, cmd, error);
} else if(g_strncasecmp(cmd, "find", 4) == 0) {
- commands_find(connection, cmd);
+ commands_find(connection, cmd, error);
} else if(g_strncasecmp(cmd, "exit", 4) == 0) {
- commands_exit(connection, cmd);
+ commands_exit(connection, cmd, error);
} 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);
+ *error = g_error_new(0, 0, "unknown command %s", cmd);
}
}
diff --git a/commands.h b/commands.h
index ec0e0b2..f774e72 100644
--- a/commands.h
+++ b/commands.h
@@ -4,6 +4,6 @@
#include <glib.h>
#include <gio/gio.h>
-void commands_handle(GSocketConnection *connection, const gchar *cmd);
+void commands_handle(GSocketConnection *connection, const gchar *cmd, GError **error);
#endif
diff --git a/control_commands.c b/control_commands.c
deleted file mode 100644
index 41e8965..0000000
--- a/control_commands.c
+++ /dev/null
@@ -1,89 +0,0 @@
-#include "control_commands.h"
-#include "servers.h"
-#include "music.h"
-#include "server_communication.h"
-
-#include <glib.h>
-#include <string.h>
-
-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);
- }
-}
-
-static void commands_find(GSocketConnection *connection, const gchar *cmd) {
- GError *error = NULL;
- gchar **data = g_strsplit(cmd, " ", 3);
-
- if(g_strv_length(data) != 3) {
- const gchar *buf = "syntax: find (artist) search\n";
- GSocket *socket = g_socket_connection_get_socket(connection);
- g_socket_send(socket, buf, strlen(buf), NULL, NULL);
- return;
- }
-
- GSList *list = NULL;
- g_debug("strlen(%s) == %d", data[1], strlen(data[1]));
- if(g_ascii_strcasecmp(data[1], "artist") == 0) {
- list = music_find_artist(data[2]);
- } else if(g_ascii_strcasecmp(data[1] , "title") == 0) {
- list = music_find_title(data[2]);
- } else if(g_ascii_strcasecmp(data[1], "album") == 0) {
- list = music_find_album(data[2]);
- } else {
- g_debug("unknown search");
- }
-
- GString *string = g_string_new(NULL);
- for(GSList *node = list; node; node = g_slist_next(node)) {
- struct file *f = node->data;
- gchar *relpath = g_build_filename(f->parent->path +
- strlen(music_root->path), f->name, NULL);
- g_string_append_printf(string, "%s\n", relpath);
- g_free(relpath);
- }
- g_slist_free(list);
-
- for(GSList *node = servers; node; node = g_slist_next(node)) {
- struct server *server = node->data;
- g_debug("fetching data from server %s", server->host);
- gchar **temp = server_find(server, data[1], data[2]);
- if(temp == NULL) {
- continue;
- }
- for(gint i = 0; i < g_strv_length(temp); i++) {
- if(strlen(temp[i]) == 0) {
- break;
- }
- g_string_append_printf(string, "%s:%s\n", server->host, temp[i]);
- }
- g_strfreev(temp);
- }
-
- GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
- if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL,
- &error) == FALSE) {
- g_warning(error->message);
- g_error_free(error);
- }
- g_string_free(string, TRUE);
-}
-
-void control_commands_handle(GSocketConnection *connection, const gchar *cmd) {
- if(g_strcmp0(cmd, "servers") == 0) {
- list_servers(connection, cmd);
- } else if(g_strncasecmp(cmd, "find", 4) == 0) {
- commands_find(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/control_commands.h b/control_commands.h
deleted file mode 100644
index 28fba6d..0000000
--- a/control_commands.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#ifndef CONTROL_COMMANDS_H
-#define CONTROL_COMMANDS_H
-
-#include <glib.h>
-#include <gio/gio.h>
-
-void control_commands_handle(GSocketConnection *connection, const gchar *cmd);
-
-#endif
diff --git a/control_service.c b/control_service.c
index 1dca7a9..150db6d 100644
--- a/control_service.c
+++ b/control_service.c
@@ -1,11 +1,12 @@
#include "control_service.h"
-#include "control_commands.h"
+#include "commands.h"
#include "conf.h"
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
+#include <string.h>
static GSocketService *ss = NULL;
static GSocketAddress *address;
@@ -13,6 +14,7 @@ static GSocketAddress *address;
static gboolean service_incoming(GSocketService *service,
GSocketConnection *connection, GObject *source_object,
gpointer user_data) {
+ GError *error = NULL;
g_debug("local service got incoming connection");
GSocket *socket = g_socket_connection_get_socket(connection);
@@ -30,7 +32,16 @@ static gboolean service_incoming(GSocketService *service,
}
*pos = '\0';
- control_commands_handle(connection, buffer);
+ commands_handle(connection, buffer, &error);
+
+ if(error != NULL) {
+ g_warning(error->message);
+ gchar *error_buffer = g_strdup_printf("%s\n", error->message);
+ g_socket_send(socket, error_buffer, strlen(error_buffer), NULL, NULL);
+ g_free(error_buffer);
+ g_error_free(error);
+ error = NULL;
+ }
}
return FALSE;