#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <gio/gio.h>

gboolean
connection_cb (GThreadedSocketService *service,
               GSocketConnection *connection,
               GObject *source_object,
               gpointer user_data)
{
    printf("connected\n");
    sleep (30);
    printf("close connection\n");

    return FALSE;
}

gboolean
timeout_cb (gpointer user_data)
{
    GSocketService *service = user_data;

    printf("unref service\n");
    g_object_unref (service);

    return G_SOURCE_REMOVE;
}

int
main (int argc, char **argv)
{
    GSocketService *service;
    GMainLoop *loop;

    service = g_threaded_socket_service_new (1);
    g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service),
                                     8888,
                                     NULL,
                                     NULL);
    g_signal_connect (service, "run", G_CALLBACK (connection_cb), NULL);

    g_timeout_add_seconds (15, timeout_cb, service);

    loop = g_main_loop_new (NULL, FALSE);
    g_main_loop_run (loop);
    g_main_loop_unref (loop);

    return 0;
}
