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