diff options
-rw-r--r-- | communication_source.c | 46 | ||||
-rw-r--r-- | communication_source.h | 16 | ||||
-rw-r--r-- | main.c | 3 | ||||
-rw-r--r-- | servers.c | 50 |
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 @@ -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); @@ -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]); } } |