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