1/* $Id$ */
2
3/***
4  This file is part of avahi.
5
6  avahi is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Lesser General Public License as
8  published by the Free Software Foundation; either version 2.1 of the
9  License, or (at your option) any later version.
10
11  avahi is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14  Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public
17  License along with avahi; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  USA.
20***/
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
26#include <glib.h>
27
28#include <avahi-client/client.h>
29#include <avahi-common/error.h>
30#include <avahi-common/timeval.h>
31#include <avahi-glib/glib-watch.h>
32#include <avahi-glib/glib-malloc.h>
33
34/* Callback for Avahi API Timeout Event */
35static void
36avahi_timeout_event (AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata)
37{
38    g_message ("Avahi API Timeout reached!");
39}
40
41/* Callback for GLIB API Timeout Event */
42static gboolean
43avahi_timeout_event_glib (void *userdata)
44{
45    GMainLoop *loop = userdata;
46
47    g_message ("GLIB API Timeout reached, quitting main loop!");
48
49    /* Quit the application */
50    g_main_loop_quit (loop);
51
52    return FALSE; /* Don't re-schedule timeout event */
53}
54
55/* Callback for state changes on the Client */
56static void
57avahi_client_callback (AVAHI_GCC_UNUSED AvahiClient *client, AvahiClientState state, void *userdata)
58{
59    GMainLoop *loop = userdata;
60
61    g_message ("Avahi Client State Change: %d", state);
62
63    if (state == AVAHI_CLIENT_FAILURE)
64    {
65        /* We we're disconnected from the Daemon */
66        g_message ("Disconnected from the Avahi Daemon: %s", avahi_strerror(avahi_client_errno(client)));
67
68        /* Quit the application */
69        g_main_loop_quit (loop);
70    }
71}
72
73int
74main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[])
75{
76    GMainLoop *loop = NULL;
77    const AvahiPoll *poll_api;
78    AvahiGLibPoll *glib_poll;
79    AvahiClient *client;
80    struct timeval tv;
81    const char *version;
82    int error;
83
84    /* Optional: Tell avahi to use g_malloc and g_free */
85    avahi_set_allocator (avahi_glib_allocator ());
86
87    /* Create the GLIB main loop */
88    loop = g_main_loop_new (NULL, FALSE);
89
90    /* Create the GLIB Adaptor */
91    glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
92    poll_api = avahi_glib_poll_get (glib_poll);
93
94    /* Example, schedule a timeout event with the Avahi API */
95    avahi_elapse_time (&tv,                         /* timeval structure */
96            1000,                                   /* 1 second */
97            0);                                     /* "jitter" - Random additional delay from 0 to this value */
98
99    poll_api->timeout_new (poll_api,                /* The AvahiPoll object */
100                      &tv,                          /* struct timeval indicating when to go activate */
101                      avahi_timeout_event,          /* Pointer to function to call */
102                      NULL);                        /* User data to pass to function */
103
104    /* Schedule a timeout event with the glib api */
105    g_timeout_add (5000,                            /* 5 seconds */
106            avahi_timeout_event_glib,               /* Pointer to function callback */
107            loop);                                  /* User data to pass to function */
108
109    /* Create a new AvahiClient instance */
110    client = avahi_client_new (poll_api,            /* AvahiPoll object from above */
111                               0,
112            avahi_client_callback,                  /* Callback function for Client state changes */
113            loop,                                   /* User data */
114            &error);                                /* Error return */
115
116    /* Check the error return code */
117    if (client == NULL)
118    {
119        /* Print out the error string */
120        g_warning ("Error initializing Avahi: %s", avahi_strerror (error));
121
122        goto fail;
123    }
124
125    /* Make a call to get the version string from the daemon */
126    version = avahi_client_get_version_string (client);
127
128    /* Check if the call suceeded */
129    if (version == NULL)
130    {
131        g_warning ("Error getting version string: %s", avahi_strerror (avahi_client_errno (client)));
132
133        goto fail;
134    }
135
136    g_message ("Avahi Server Version: %s", version);
137
138    /* Start the GLIB Main Loop */
139    g_main_loop_run (loop);
140
141fail:
142    /* Clean up */
143    g_main_loop_unref (loop);
144    avahi_client_free (client);
145    avahi_glib_poll_free (glib_poll);
146
147    return 0;
148}
149