2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <avahi-client/client.h>
27 #include <avahi-common/error.h>
28 #include <avahi-common/timeval.h>
29 #include <avahi-glib/glib-watch.h>
30 #include <avahi-glib/glib-malloc.h>
32 /* Callback for Avahi API Timeout Event */
34 avahi_timeout_event (AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata)
36 g_message ("Avahi API Timeout reached!");
39 /* Callback for GLIB API Timeout Event */
41 avahi_timeout_event_glib (void *userdata)
43 GMainLoop *loop = userdata;
45 g_message ("GLIB API Timeout reached, quitting main loop!");
47 /* Quit the application */
48 g_main_loop_quit (loop);
50 return FALSE; /* Don't re-schedule timeout event */
53 /* Callback for state changes on the Client */
55 avahi_client_callback (AVAHI_GCC_UNUSED AvahiClient *client, AvahiClientState state, void *userdata)
57 GMainLoop *loop = userdata;
59 g_message ("Avahi Client State Change: %d", state);
61 if (state == AVAHI_CLIENT_FAILURE)
63 /* We we're disconnected from the Daemon */
64 g_message ("Disconnected from the Avahi Daemon: %s", avahi_strerror(avahi_client_errno(client)));
66 /* Quit the application */
67 g_main_loop_quit (loop);
72 main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[])
74 GMainLoop *loop = NULL;
75 const AvahiPoll *poll_api;
76 AvahiGLibPoll *glib_poll;
82 /* Optional: Tell avahi to use g_malloc and g_free */
83 avahi_set_allocator (avahi_glib_allocator ());
85 /* Create the GLIB main loop */
86 loop = g_main_loop_new (NULL, FALSE);
88 /* Create the GLIB Adaptor */
89 glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
90 poll_api = avahi_glib_poll_get (glib_poll);
92 /* Example, schedule a timeout event with the Avahi API */
93 avahi_elapse_time (&tv, /* timeval structure */
95 0); /* "jitter" - Random additional delay from 0 to this value */
97 poll_api->timeout_new (poll_api, /* The AvahiPoll object */
98 &tv, /* struct timeval indicating when to go activate */
99 avahi_timeout_event, /* Pointer to function to call */
100 NULL); /* User data to pass to function */
102 /* Schedule a timeout event with the glib api */
103 g_timeout_add (5000, /* 5 seconds */
104 avahi_timeout_event_glib, /* Pointer to function callback */
105 loop); /* User data to pass to function */
107 /* Create a new AvahiClient instance */
108 client = avahi_client_new (poll_api, /* AvahiPoll object from above */
110 avahi_client_callback, /* Callback function for Client state changes */
111 loop, /* User data */
112 &error); /* Error return */
114 /* Check the error return code */
117 /* Print out the error string */
118 g_warning ("Error initializing Avahi: %s", avahi_strerror (error));
123 /* Make a call to get the version string from the daemon */
124 version = avahi_client_get_version_string (client);
126 /* Check if the call suceeded */
129 g_warning ("Error getting version string: %s", avahi_strerror (avahi_client_errno (client)));
134 g_message ("Avahi Server Version: %s", version);
136 /* Start the GLIB Main Loop */
137 g_main_loop_run (loop);
141 g_main_loop_unref (loop);
142 avahi_client_free (client);
143 avahi_glib_poll_free (glib_poll);