1/*** 2 This file is part of avahi. 3 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. 8 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. 13 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 17 USA. 18***/ 19 20#ifdef HAVE_CONFIG_H 21#include <config.h> 22#endif 23 24#include <glib.h> 25 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> 31 32/* Callback for Avahi API Timeout Event */ 33static void 34avahi_timeout_event (AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata) 35{ 36 g_message ("Avahi API Timeout reached!"); 37} 38 39/* Callback for GLIB API Timeout Event */ 40static gboolean 41avahi_timeout_event_glib (void *userdata) 42{ 43 GMainLoop *loop = userdata; 44 45 g_message ("GLIB API Timeout reached, quitting main loop!"); 46 47 /* Quit the application */ 48 g_main_loop_quit (loop); 49 50 return FALSE; /* Don't re-schedule timeout event */ 51} 52 53/* Callback for state changes on the Client */ 54static void 55avahi_client_callback (AVAHI_GCC_UNUSED AvahiClient *client, AvahiClientState state, void *userdata) 56{ 57 GMainLoop *loop = userdata; 58 59 g_message ("Avahi Client State Change: %d", state); 60 61 if (state == AVAHI_CLIENT_FAILURE) 62 { 63 /* We we're disconnected from the Daemon */ 64 g_message ("Disconnected from the Avahi Daemon: %s", avahi_strerror(avahi_client_errno(client))); 65 66 /* Quit the application */ 67 g_main_loop_quit (loop); 68 } 69} 70 71int 72main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) 73{ 74 GMainLoop *loop = NULL; 75 const AvahiPoll *poll_api; 76 AvahiGLibPoll *glib_poll; 77 AvahiClient *client; 78 struct timeval tv; 79 const char *version; 80 int error; 81 82 /* Optional: Tell avahi to use g_malloc and g_free */ 83 avahi_set_allocator (avahi_glib_allocator ()); 84 85 /* Create the GLIB main loop */ 86 loop = g_main_loop_new (NULL, FALSE); 87 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); 91 92 /* Example, schedule a timeout event with the Avahi API */ 93 avahi_elapse_time (&tv, /* timeval structure */ 94 1000, /* 1 second */ 95 0); /* "jitter" - Random additional delay from 0 to this value */ 96 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 */ 101 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 */ 106 107 /* Create a new AvahiClient instance */ 108 client = avahi_client_new (poll_api, /* AvahiPoll object from above */ 109 0, 110 avahi_client_callback, /* Callback function for Client state changes */ 111 loop, /* User data */ 112 &error); /* Error return */ 113 114 /* Check the error return code */ 115 if (client == NULL) 116 { 117 /* Print out the error string */ 118 g_warning ("Error initializing Avahi: %s", avahi_strerror (error)); 119 120 goto fail; 121 } 122 123 /* Make a call to get the version string from the daemon */ 124 version = avahi_client_get_version_string (client); 125 126 /* Check if the call suceeded */ 127 if (version == NULL) 128 { 129 g_warning ("Error getting version string: %s", avahi_strerror (avahi_client_errno (client))); 130 131 goto fail; 132 } 133 134 g_message ("Avahi Server Version: %s", version); 135 136 /* Start the GLIB Main Loop */ 137 g_main_loop_run (loop); 138 139fail: 140 /* Clean up */ 141 g_main_loop_unref (loop); 142 avahi_client_free (client); 143 avahi_glib_poll_free (glib_poll); 144 145 return 0; 146} 147