summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-23 18:36:29 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-23 18:36:29 +0200
commitd295f0a4cbc5f96b0aa65a3d005f98bc31511068 (patch)
tree8057c0b98d5186bc17e8887002d3f65808cc5d59
parent0baaa8c068f57e44efffa7c1f6806faefdd655e4 (diff)
Implemented a communication event source for the main loop.
This source will deal with communication between servers. More to be added and cleaned up soon.
-rw-r--r--communication_source.c46
-rw-r--r--communication_source.h16
-rw-r--r--main.c3
-rw-r--r--servers.c50
4 files changed, 114 insertions, 1 deletions
diff --git a/communication_source.c b/communication_source.c
new file mode 100644
index 0000000..b0a03cb
--- /dev/null
+++ b/communication_source.c
@@ -0,0 +1,46 @@
+#include "communication_source.h"
+#include "servers.h"
+
+static GQueue *jobqueue = NULL;
+
+void communication_job_add(struct server *server) {
+ g_queue_push_head(jobqueue, server);
+}
+
+gboolean comsrc_prepare(GSource *source, gint *timeout) {
+ g_debug("comsrc_prepare()");
+ *timeout = 2000;
+ return g_queue_get_length(jobqueue) > 0 ? TRUE : FALSE;
+}
+
+gboolean comsrc_check(GSource *source) {
+ g_debug("comsrc_check()");
+ return g_queue_get_length(jobqueue) > 0 ? TRUE : FALSE;
+}
+
+gboolean comsrc_dispatch(GSource *source, GSourceFuncs callback, gpointer user_data) {
+ g_debug("comsrc_dispatch(), callback is %x, user_data is %x", callback, user_data);
+ struct server *server = g_queue_pop_tail(jobqueue);
+ server_sync(server);
+ return TRUE;
+}
+
+void comsrc_finalize(GSource *source) {
+}
+
+static const GSourceFuncs com_src_funcs = {
+ .prepare = comsrc_prepare,
+ .check = comsrc_check,
+ .dispatch = comsrc_dispatch,
+ .finalize = comsrc_finalize,
+};
+
+void communication_source_init(GMainLoop *mainloop) {
+ jobqueue = g_queue_new();
+
+ GMainContext *context = g_main_loop_get_context(mainloop);
+ GSource *source = g_source_new(&com_src_funcs, sizeof(GSource));
+ g_source_attach(source, context);
+
+ communication_job_add(servers->data);
+}
diff --git a/communication_source.h b/communication_source.h
new file mode 100644
index 0000000..0856cf4
--- /dev/null
+++ b/communication_source.h
@@ -0,0 +1,16 @@
+#ifndef COMMUNICATION_SOURCE_H
+#define COMMUNICATION_SOURCE_H
+
+#include "servers.h"
+
+#include <glib.h>
+
+struct communication_job {
+ struct server *server;
+};
+
+void communication_job_add(struct server *server);
+
+void communication_source_init(GMainLoop *mainloop);
+
+#endif
diff --git a/main.c b/main.c
index 1697c6a..0e846d7 100644
--- a/main.c
+++ b/main.c
@@ -35,6 +35,9 @@ int main(int argc, char **argv) {
signal(SIGINT, sig_handler);
main_loop = g_main_loop_new(NULL, FALSE);
+
+ communication_source_init(main_loop);
+
g_main_loop_run(main_loop);
g_main_loop_unref(main_loop);
diff --git a/servers.c b/servers.c
index 22e431d..94bbaf4 100644
--- a/servers.c
+++ b/servers.c
@@ -1,8 +1,56 @@
#include "servers.h"
#include "conf.h"
+#include <gio/gio.h>
+
GSList *servers = NULL;
+gboolean server_sync(struct server *server) {
+ GError *error = NULL;
+ GSocketConnectable *addr;
+ GSocketAddressEnumerator *enumerator;
+ GSocketAddress *sockaddr;
+ GSocketConnection *conn = NULL;
+ GSocketClient *client;
+ GTimer *timer = g_timer_new();
+
+ addr = g_network_address_new(server->host, server->port);
+ enumerator = g_socket_connectable_enumerate(addr);
+ g_object_unref(addr);
+
+ client = g_socket_client_new();
+
+ while(conn == NULL && (sockaddr = g_socket_address_enumerator_next(enumerator, NULL, &error))) {
+ conn = g_socket_client_connect(client, (GSocketConnectable*)sockaddr, NULL, &error);
+ g_object_unref(sockaddr);
+ }
+
+ if(conn == NULL) {
+ g_warning(error->message);
+ g_error_free(error);
+ return FALSE;
+ }
+
+ GSocket *socket = g_socket_connection_get_socket(conn);
+
+ g_debug("connected");
+
+ g_socket_send(socket, "ping\n", 5, NULL, NULL);
+ gchar buffer[0x400];
+ g_socket_receive(socket, buffer, 0x400, NULL, NULL);
+ if(buffer[4] == '\n') {
+ buffer[4] = '\0';
+ g_debug("got response: %s", buffer);
+ g_debug("time: %f ms", g_timer_elapsed(timer, NULL) * 1000);
+ }
+
+ g_timer_destroy(timer);
+
+ g_socket_close(socket, NULL);
+
+ return TRUE;
+}
+
gboolean server_add(const gchar *host, const guint16 port) {
struct server *server = g_new0(struct server, 1);
@@ -35,7 +83,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);
+ server_add(s[i], 7681);
g_debug(" server %d: %s", i, s[i]);
}
}