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
24 #include <avahi-client/client.h>
25 #include <avahi-common/error.h>
26 #include <avahi-common/timeval.h>
27 #include <avahi-glib/glib-watch.h>
28 #include <avahi-glib/glib-malloc.h>
30 /* Callback for Avahi API Timeout Event */
32 avahi_timeout_event (AvahiTimeout *timeout, void *userdata)
34 g_message ("Avahi API Timeout reached!");
37 /* Callback for GLIB API Timeout Event */
39 avahi_timeout_event_glib (void *userdata)
41 GMainLoop *loop = userdata;
43 g_message ("GLIB API Timeout reached, quitting main loop!");
45 /* Quit the application */
46 g_main_loop_quit (loop);
48 return FALSE; /* Don't re-schedule timeout event */
51 /* Callback for state changes on the Client */
53 avahi_client_callback (AvahiClient *client, AvahiClientState state, void *userdata)
55 GMainLoop *loop = userdata;
57 g_message ("Avahi Client State Change: %d", state);
59 if (state == AVAHI_CLIENT_DISCONNECTED)
61 /* We we're disconnected from the Daemon */
62 g_message ("Disconnected from the Avahi Daemon");
64 /* Quit the application */
65 g_main_loop_quit (loop);
70 main (int argc, char *argv[])
72 GMainLoop *loop = NULL;
73 const AvahiPoll *poll_api;
74 AvahiGLibPoll *glib_poll;
80 /* Optional: Tell avahi to use g_malloc and g_free */
81 avahi_set_allocator (avahi_glib_allocator ());
83 /* Create the GLIB main loop */
84 loop = g_main_loop_new (NULL, FALSE);
86 /* Create the GLIB Adaptor */
87 glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
88 poll_api = avahi_glib_poll_get (glib_poll);
90 /* Example, schedule a timeout event with the Avahi API */
91 avahi_elapse_time (&tv, /* timeval structure */
93 0); /* "jitter" - Random additional delay from 0 to this value */
95 poll_api->timeout_new (poll_api, /* The AvahiPoll object */
96 &tv, /* struct timeval indicating when to go activate */
97 avahi_timeout_event, /* Pointer to function to call */
98 NULL); /* User data to pass to function */
100 /* Schedule a timeout event with the glib api */
101 g_timeout_add (5000, /* 5 seconds */
102 avahi_timeout_event_glib, /* Pointer to function callback */
103 loop); /* User data to pass to function */
105 /* Create a new AvahiClient instance */
106 client = avahi_client_new (poll_api, /* AvahiPoll object from above */
107 avahi_client_callback, /* Callback function for Client state changes */
108 loop, /* User data */
109 &error); /* Error return */
111 /* Check the error return code */
114 /* Print out the error string */
115 g_warning ("Error initializing Avahi: %s", avahi_strerror (error));
120 /* Make a call to get the version string from the daemon */
121 version = avahi_client_get_version_string (client);
123 /* Check if the call suceeded */
126 g_warning ("Error getting version string: %s", avahi_strerror (avahi_client_errno (client)));
131 g_message ("Avahi Server Version: %s", version);
133 /* Start the GLIB Main Loop */
134 g_main_loop_run (loop);
138 g_main_loop_unref (loop);
139 avahi_client_free (client);
140 avahi_glib_poll_free (glib_poll);