4 This file is part of avahi.
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.
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.
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
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>
34 /* Callback for Avahi API Timeout Event */
36 avahi_timeout_event (AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata)
38 g_message ("Avahi API Timeout reached!");
41 /* Callback for GLIB API Timeout Event */
43 avahi_timeout_event_glib (void *userdata)
45 GMainLoop *loop = userdata;
47 g_message ("GLIB API Timeout reached, quitting main loop!");
49 /* Quit the application */
50 g_main_loop_quit (loop);
52 return FALSE; /* Don't re-schedule timeout event */
55 /* Callback for state changes on the Client */
57 avahi_client_callback (AVAHI_GCC_UNUSED AvahiClient *client, AvahiClientState state, void *userdata)
59 GMainLoop *loop = userdata;
61 g_message ("Avahi Client State Change: %d", state);
63 if (state == AVAHI_CLIENT_FAILURE)
65 /* We we're disconnected from the Daemon */
66 g_message ("Disconnected from the Avahi Daemon: %s", avahi_strerror(avahi_client_errno(client)));
68 /* Quit the application */
69 g_main_loop_quit (loop);
74 main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[])
76 GMainLoop *loop = NULL;
77 const AvahiPoll *poll_api;
78 AvahiGLibPoll *glib_poll;
84 /* Optional: Tell avahi to use g_malloc and g_free */
85 avahi_set_allocator (avahi_glib_allocator ());
87 /* Create the GLIB main loop */
88 loop = g_main_loop_new (NULL, FALSE);
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);
94 /* Example, schedule a timeout event with the Avahi API */
95 avahi_elapse_time (&tv, /* timeval structure */
97 0); /* "jitter" - Random additional delay from 0 to this value */
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 */
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 */
109 /* Create a new AvahiClient instance */
110 client = avahi_client_new (poll_api, /* AvahiPoll object from above */
112 avahi_client_callback, /* Callback function for Client state changes */
113 loop, /* User data */
114 &error); /* Error return */
116 /* Check the error return code */
119 /* Print out the error string */
120 g_warning ("Error initializing Avahi: %s", avahi_strerror (error));
125 /* Make a call to get the version string from the daemon */
126 version = avahi_client_get_version_string (client);
128 /* Check if the call suceeded */
131 g_warning ("Error getting version string: %s", avahi_strerror (avahi_client_errno (client)));
136 g_message ("Avahi Server Version: %s", version);
138 /* Start the GLIB Main Loop */
139 g_main_loop_run (loop);
143 g_main_loop_unref (loop);
144 avahi_client_free (client);
145 avahi_glib_poll_free (glib_poll);