From 723e4d07d4782c866ffee5329a373d8cbed2e5ff Mon Sep 17 00:00:00 2001 From: Trent Lloyd Date: Mon, 22 Aug 2005 19:07:08 +0000 Subject: * Add glib integration example. git-svn-id: file:///home/lennart/svn/public/avahi/trunk@394 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-client/client.h | 3 + examples/Makefile.am | 10 ++++ examples/glib-integration.c | 141 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 examples/glib-integration.c diff --git a/avahi-client/client.h b/avahi-client/client.h index 56672e3..20054ad 100644 --- a/avahi-client/client.h +++ b/avahi-client/client.h @@ -39,6 +39,9 @@ /** \example client-browse-services.c Example how to browse for DNS-SD * services using the client interface to avahi-daemon. */ + +/** \example glib-integration.c Example of how to integrate + * avahi use with GLIB/GTK applications */ #ifndef DOXYGEN_SHOULD_SKIP_THIS diff --git a/examples/Makefile.am b/examples/Makefile.am index fa26456..137dd55 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -46,3 +46,13 @@ client_browse_services_CFLAGS = $(AM_CFLAGS) client_browse_services_LDADD = $(AM_LDADD) ../avahi-client/libavahi-client.la ../avahi-common/libavahi-common.la endif + +if HAVE_GTK + +noinst_PROGRAMS += \ + glib-integration +glib_integration_SOURCES = glib-integration.c +glib_integration_CFLAGS = $(AM_CFLAGS) $(GLIB20_CFLAGS) +glib_integration_LDADD = $(AM_LDADD) $(GLIB20_LIBS) ../avahi-client/libavahi-client.la ../avahi-common/libavahi-common.la ../avahi-glib/libavahi-glib.la + +endif diff --git a/examples/glib-integration.c b/examples/glib-integration.c new file mode 100644 index 0000000..4c17ebf --- /dev/null +++ b/examples/glib-integration.c @@ -0,0 +1,141 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include + +#include +#include +#include +#include +#include + +/* Callback for Avahi API Timeout Event */ +static void +avahi_timeout_event (AvahiTimeout *timeout, void *userdata) +{ + g_message ("Avahi API Timeout reached!"); +} + +/* Callback for GLIB API Timeout Event */ +static gboolean +avahi_timeout_event_glib (void *userdata) +{ + GMainLoop *loop = userdata; + + g_message ("GLIB API Timeout reached, quitting main loop!"); + + /* Quit the application */ + g_main_loop_quit (loop); + + return FALSE; /* Don't re-schedule timeout event */ +} + +/* Callback for state changes on the Client */ +static void +avahi_client_callback (AvahiClient *client, AvahiClientState state, void *userdata) +{ + GMainLoop *loop = userdata; + + g_message ("Avahi Client State Change: %d", state); + + if (state == AVAHI_CLIENT_DISCONNECTED) + { + /* We we're disconnected from the Daemon */ + g_message ("Disconnected from the Avahi Daemon"); + + /* Quit the application */ + g_main_loop_quit (loop); + } +} + +int +main (int argc, char *argv[]) +{ + GMainLoop *loop = NULL; + const AvahiPoll *poll_api; + AvahiGLibPoll *glib_poll; + AvahiClient *client; + struct timeval tv; + const char *version; + int error; + + /* Optional: Tell avahi to use g_malloc and g_free */ + avahi_set_allocator (avahi_glib_allocator ()); + + /* Create the GLIB main loop */ + loop = g_main_loop_new (NULL, FALSE); + + /* Create the GLIB Adaptor */ + glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT); + poll_api = avahi_glib_poll_get (glib_poll); + + /* Example, schedule a timeout event with the Avahi API */ + avahi_elapse_time (&tv, /* timeval structure */ + 1000, /* 1 second */ + 0); /* "jitter" - Random additional delay from 0 to this value */ + + poll_api->timeout_new (poll_api, /* The AvahiPoll object */ + &tv, /* struct timeval indicating when to go activate */ + avahi_timeout_event, /* Pointer to function to call */ + NULL); /* User data to pass to function */ + + /* Schedule a timeout event with the glib api */ + g_timeout_add (5000, /* 5 seconds */ + avahi_timeout_event_glib, /* Pointer to function callback */ + loop); /* User data to pass to function */ + + /* Create a new AvahiClient instance */ + client = avahi_client_new (poll_api, /* AvahiPoll object from above */ + avahi_client_callback, /* Callback function for Client state changes */ + loop, /* User data */ + &error); /* Error return */ + + /* Check the error return code */ + if (client == NULL) + { + /* Print out the error string */ + g_warning ("Error initializing Avahi: %s", avahi_strerror (error)); + + goto fail; + } + + /* Make a call to get the version string from the daemon */ + version = avahi_client_get_version_string (client); + + /* Check if the call suceeded */ + if (version == NULL) + { + g_warning ("Error getting version string: %s", avahi_strerror (avahi_client_errno (client))); + + goto fail; + } + + g_message ("Avahi Server Version: %s", version); + + /* Start the GLIB Main Loop */ + g_main_loop_run (loop); + +fail: + /* Clean up */ + g_main_loop_unref (loop); + avahi_client_free (client); + avahi_glib_poll_free (glib_poll); +} -- cgit