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
26 #include <avahi-client/client.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/llist.h>
29 #include <avahi-common/error.h>
34 #define DBUS_API_SUBJECT_TO_CHANGE
35 #include <dbus/dbus.h>
36 #include <dbus/dbus-glib-lowlevel.h>
43 /* AvahiDomainBrowser */
45 AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *user_data)
47 AvahiDomainBrowser *tmp = NULL;
48 DBusMessage *message = NULL, *reply;
55 dbus_error_init (&error);
57 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
58 AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew");
60 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INT32, &btype, DBUS_TYPE_INVALID))
63 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
65 if (dbus_error_is_set (&error) || reply == NULL)
68 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
71 if (dbus_error_is_set (&error) || path == NULL)
74 tmp = malloc (sizeof (AvahiDomainBrowser));
76 tmp->callback = callback;
77 tmp->user_data = user_data;
78 tmp->path = strdup (path);
80 AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, tmp);
85 dbus_error_free (&error);
86 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
92 avahi_domain_browser_path (AvahiDomainBrowser *b)
98 avahi_domain_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
100 AvahiDomainBrowser *n, *db = NULL;
104 int interface, protocol;
106 dbus_error_init (&error);
108 path = dbus_message_get_path (message);
113 for (n = client->domain_browsers; n != NULL; n = n->domain_browsers_next)
115 printf ("cmp: %s, %s\n", n->path, path);
116 if (strcmp (n->path, path) == 0) {
125 dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &interface,
126 DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID);
128 if (dbus_error_is_set (&error))
131 db->callback (db, interface, protocol, event, domain, db->user_data);
133 return DBUS_HANDLER_RESULT_HANDLED;
136 dbus_error_free (&error);
137 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
140 /* AvahiServiceTypeBrowser */
141 AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiServiceTypeBrowserCallback callback, void *user_data)
143 AvahiServiceTypeBrowser *tmp = NULL;
144 DBusMessage *message = NULL, *reply;
151 dbus_error_init (&error);
153 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
154 AVAHI_DBUS_PATH_SERVER,
155 AVAHI_DBUS_INTERFACE_SERVER,
156 "ServiceTypeBrowserNew");
158 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID))
161 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
163 if (dbus_error_is_set (&error) || reply == NULL)
166 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
169 if (dbus_error_is_set (&error) || path == NULL)
172 tmp = malloc (sizeof (AvahiServiceTypeBrowser));
173 tmp->client = client;
174 tmp->callback = callback;
175 tmp->user_data = user_data;
176 tmp->path = strdup (path);
178 AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, tmp);
183 dbus_error_free (&error);
184 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
190 avahi_service_type_browser_path (AvahiServiceTypeBrowser *b)
196 avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
198 AvahiServiceTypeBrowser *n, *db = NULL;
202 int interface, protocol;
204 dbus_error_init (&error);
206 path = dbus_message_get_path (message);
211 for (n = client->service_type_browsers; n != NULL; n = n->service_type_browsers_next)
213 printf ("cmp: %s, %s\n", n->path, path);
214 if (strcmp (n->path, path) == 0) {
223 dbus_message_get_args (message, &error,
224 DBUS_TYPE_INT32, &interface,
225 DBUS_TYPE_INT32, &protocol,
226 DBUS_TYPE_STRING, &type,
227 DBUS_TYPE_STRING, &domain,
230 if (dbus_error_is_set (&error))
233 db->callback (db, interface, protocol, event, type, domain, db->user_data);
235 return DBUS_HANDLER_RESULT_HANDLED;
238 dbus_error_free (&error);
239 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
242 /* AvahiServiceBrowser */
244 AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *type, char *domain, AvahiServiceBrowserCallback callback, void *user_data)
246 AvahiServiceBrowser *tmp = NULL;
247 DBusMessage *message = NULL, *reply;
254 dbus_error_init (&error);
256 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
257 AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew");
259 if (!dbus_message_append_args (message,
260 DBUS_TYPE_INT32, &interface,
261 DBUS_TYPE_INT32, &protocol,
262 DBUS_TYPE_STRING, &type,
263 DBUS_TYPE_STRING, &domain,
267 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
269 if (dbus_error_is_set (&error) || reply == NULL)
272 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
275 if (dbus_error_is_set (&error) || path == NULL)
278 tmp = malloc (sizeof (AvahiServiceBrowser));
279 tmp->client = client;
280 tmp->callback = callback;
281 tmp->user_data = user_data;
282 tmp->path = strdup (path);
284 AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp);
289 dbus_error_free (&error);
290 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
296 avahi_service_browser_path (AvahiServiceBrowser *b)
302 avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
304 AvahiServiceBrowser *n, *db = NULL;
307 char *name, *type, *domain;
308 int interface, protocol;
310 dbus_error_init (&error);
312 path = dbus_message_get_path (message);
317 for (n = client->service_browsers; n != NULL; n = n->service_browsers_next)
319 printf ("cmp: %s, %s\n", n->path, path);
320 if (strcmp (n->path, path) == 0) {
329 dbus_message_get_args (message, &error,
330 DBUS_TYPE_INT32, &interface,
331 DBUS_TYPE_INT32, &protocol,
332 DBUS_TYPE_STRING, &name,
333 DBUS_TYPE_STRING, &type,
334 DBUS_TYPE_STRING, &domain,
337 if (dbus_error_is_set (&error))
340 db->callback (db, interface, protocol, event, name, type, domain, db->user_data);
342 return DBUS_HANDLER_RESULT_HANDLED;
345 dbus_error_free (&error);
346 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;