From 0ef865bb301954df86b87cd1978ca711c01e71d5 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 21 Aug 2010 01:09:12 +0200 Subject: Added the basics for local control commands. --- SConstruct | 1 + control_commands.c | 14 +++++++++++++ control_commands.h | 8 ++++++++ control_service.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ control_service.h | 9 +++++++++ main.c | 5 +++++ 6 files changed, 96 insertions(+) create mode 100644 control_commands.c create mode 100644 control_commands.h create mode 100644 control_service.c create mode 100644 control_service.h diff --git a/SConstruct b/SConstruct index f85f2a3..44efee8 100644 --- a/SConstruct +++ b/SConstruct @@ -15,6 +15,7 @@ else: env.ParseConfig('pkg-config --cflags --libs glib-2.0') env.ParseConfig('pkg-config --cflags --libs gio-2.0') +env.ParseConfig('pkg-config --cflags --libs gio-unix-2.0') env.ParseConfig('pkg-config --cflags --libs libmpg123') env.ParseConfig('pkg-config --cflags --libs id3tag') diff --git a/control_commands.c b/control_commands.c new file mode 100644 index 0000000..1ee261c --- /dev/null +++ b/control_commands.c @@ -0,0 +1,14 @@ +#include "control_commands.h" + +#include +#include + +void control_commands_handle(const gchar *cmd) { + gchar *args = strchr(cmd, ' '); + if(args != NULL) { + *args = '\0'; + args++; + } + + g_debug("handling command %s with arguments %s", cmd, args); +} diff --git a/control_commands.h b/control_commands.h new file mode 100644 index 0000000..03d557b --- /dev/null +++ b/control_commands.h @@ -0,0 +1,8 @@ +#ifndef CONTROL_COMMANDS_H +#define CONTROL_COMMANDS_H + +#include + +void control_commands_handle(const gchar *cmd); + +#endif diff --git a/control_service.c b/control_service.c new file mode 100644 index 0000000..a6f035d --- /dev/null +++ b/control_service.c @@ -0,0 +1,59 @@ +#include "control_service.h" +#include "control_commands.h" + +#include +#include +#include +#include + +static GSocketService *ss = NULL; +static GSocketAddress *address; + +static gboolean service_incoming(GSocketService *service, + GSocketConnection *connection, GObject *source_object, + gpointer user_data) { + g_debug("local service got incoming connection"); + + GSocket *socket = g_socket_connection_get_socket(connection); + gchar buffer[0x400]; + gssize size = g_socket_receive(socket, buffer, 0x400, NULL, NULL); + + gchar *pos = g_strstr_len(buffer, size, "\r"); + if(pos == NULL) { + pos = g_strstr_len(buffer, size, "\n"); + } + if(pos == NULL) { + g_warning("EOL not found"); + return FALSE; + } + *pos = '\0'; + + control_commands_handle(buffer); + + return FALSE; +} + +gboolean control_service_start() { + address = g_unix_socket_address_new("audist.sock"); + + ss = g_threaded_socket_service_new(2); + + if(g_socket_listener_add_address((GSocketListener*)ss, address, + G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL, + NULL, NULL) == FALSE) { + g_error("g_socket_listener_add_socket() failed"); + } + + g_signal_connect(ss, "incoming", (GCallback)service_incoming, NULL); + + g_socket_service_start((GSocketService*)ss); + + return TRUE; +} + +void control_service_stop() { + g_socket_service_stop((GSocketService*)ss); + g_unlink(g_unix_socket_address_get_path((GUnixSocketAddress*)address)); + g_object_unref(ss); + g_object_unref(address); +} diff --git a/control_service.h b/control_service.h new file mode 100644 index 0000000..70cadc5 --- /dev/null +++ b/control_service.h @@ -0,0 +1,9 @@ +#ifndef CONTROL_SERVICE_H +#define CONTROL_SERVICE_H + +#include + +gboolean control_service_start(); +void control_service_stop(); + +#endif diff --git a/main.c b/main.c index 6b40e86..1697c6a 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include "command_service.h" #include "conf.h" #include "servers.h" +#include "control_service.h" #include #include @@ -21,6 +22,8 @@ int main(int argc, char **argv) { conf_load(); + control_service_start(); + servers_init(); music_init(); @@ -41,6 +44,8 @@ int main(int argc, char **argv) { servers_free(); + control_service_stop(); + conf_free(); return 0; -- cgit v1.2.3