From 9f0dd75f02f5eceb758436de07f18fcc892c12f0 Mon Sep 17 00:00:00 2001 From: Trent Lloyd Date: Sun, 14 Aug 2005 10:56:45 +0000 Subject: * Add service browser support to C client API * Small fix to avahi-common to get make distcheck further git-svn-id: file:///home/lennart/svn/public/avahi/trunk@317 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-client/browser.c | 109 +++++++++++++++++++++++++++++++++++++++++++++ avahi-client/client-test.c | 12 +++++ avahi-client/client.c | 7 ++- avahi-client/client.h | 17 +++++++ avahi-client/internal.h | 10 +++++ 5 files changed, 154 insertions(+), 1 deletion(-) (limited to 'avahi-client') diff --git a/avahi-client/browser.c b/avahi-client/browser.c index 3e30110..bcd9914 100644 --- a/avahi-client/browser.c +++ b/avahi-client/browser.c @@ -238,3 +238,112 @@ out: dbus_error_free (&error); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } + +/* AvahiServiceBrowser */ + +AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *type, char *domain, AvahiServiceBrowserCallback callback, void *user_data) +{ + AvahiServiceBrowser *tmp = NULL; + DBusMessage *message = NULL, *reply; + DBusError error; + char *path; + + if (client == NULL) + return NULL; + + dbus_error_init (&error); + + message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, + AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew"); + + if (!dbus_message_append_args (message, + DBUS_TYPE_INT32, &interface, + DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_STRING, &type, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INVALID)) + goto dbus_error; + + reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); + + if (dbus_error_is_set (&error) || reply == NULL) + goto dbus_error; + + if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) + goto dbus_error; + + if (dbus_error_is_set (&error) || path == NULL) + goto dbus_error; + + tmp = malloc (sizeof (AvahiServiceBrowser)); + tmp->client = client; + tmp->callback = callback; + tmp->user_data = user_data; + tmp->path = strdup (path); + + AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp); + + return tmp; + +dbus_error: + dbus_error_free (&error); + avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); + + return NULL; +} + +char* +avahi_service_browser_path (AvahiServiceBrowser *b) +{ + return b->path; +} + +DBusHandlerResult +avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) +{ + AvahiServiceBrowser *n, *db = NULL; + DBusError error; + const char *path; + char *name, *type, *domain; + int interface, protocol; + + dbus_error_init (&error); + + path = dbus_message_get_path (message); + + if (path == NULL) + goto out; + + for (n = client->service_browsers; n != NULL; n = n->service_browsers_next) + { + printf ("cmp: %s, %s\n", n->path, path); + if (strcmp (n->path, path) == 0) { + db = n; + break; + } + } + + if (db == NULL) + goto out; + + dbus_message_get_args (message, &error, + DBUS_TYPE_INT32, &interface, + DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_STRING, &type, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INVALID); + + if (dbus_error_is_set (&error)) + goto out; + + db->callback (db, interface, protocol, event, name, type, domain, db->user_data); + + return DBUS_HANDLER_RESULT_HANDLED; + +out: + dbus_error_free (&error); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + + diff --git a/avahi-client/client-test.c b/avahi-client/client-test.c index 18997d5..cd30b48 100644 --- a/avahi-client/client-test.c +++ b/avahi-client/client-test.c @@ -46,6 +46,12 @@ avahi_domain_browser_callback (AvahiDomainBrowser *b, AvahiIfIndex interface, Av printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), domain (%s), data (%s)\n", avahi_domain_browser_path (b), interface, protocol, event, domain, (char*)user_data); } +void +avahi_service_browser_callback (AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, char *name, char *type, char *domain, void *user_data) +{ + printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), name (%s), type (%s), domain (%s), data (%s)\n", avahi_service_browser_path (b), interface, protocol, event, name, type, domain, (char*)user_data); +} + void avahi_service_type_browser_callback (AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, char *type, char *domain, void *user_data) { @@ -59,6 +65,7 @@ main (int argc, char *argv[]) AvahiEntryGroup *group; AvahiStringList *txt; AvahiDomainBrowser *domain; + AvahiServiceBrowser *sb; AvahiServiceTypeBrowser *st; char *ret; @@ -111,6 +118,11 @@ main (int argc, char *argv[]) else printf ("Sucessfully created service type browser, path %s\n", avahi_service_type_browser_path (st)); + sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AF_UNSPEC, "_http._tcp", "", avahi_service_browser_callback, "omghai3u"); + if (sb == NULL) + printf ("Failed to create service browser object\n"); + else + printf ("Sucessfully created service browser, path %s\n", avahi_service_browser_path (sb)); g_main_loop_run (loop); diff --git a/avahi-client/client.c b/avahi-client/client.c index 49c82d5..bed2515 100644 --- a/avahi-client/client.c +++ b/avahi-client/client.c @@ -183,6 +183,10 @@ filter_func (DBusConnection *bus, DBusMessage *message, void *data) return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message); } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) { return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message); + } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew")) { + return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message); + } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove")) { + return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message); } return DBUS_HANDLER_RESULT_HANDLED; @@ -204,7 +208,8 @@ avahi_client_new (AvahiClientCallback callback, void *user_data) AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, tmp->groups); AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, tmp->domain_browsers); - AVAHI_LLIST_HEAD_INIT(AvahiServieTypeBrowser, tmp->service_type_browsers); + AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, tmp->service_browsers); + AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, tmp->service_type_browsers); tmp->bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); diff --git a/avahi-client/client.h b/avahi-client/client.h index f3716eb..a242124 100644 --- a/avahi-client/client.h +++ b/avahi-client/client.h @@ -41,6 +41,8 @@ typedef struct _AvahiEntryGroup AvahiEntryGroup; typedef struct _AvahiDomainBrowser AvahiDomainBrowser; +typedef struct _AvahiServiceBrowser AvahiServiceBrowser; + typedef struct _AvahiServiceTypeBrowser AvahiServiceTypeBrowser; /** States of a client object, note that AvahiServerStates are also emitted */ @@ -58,6 +60,9 @@ typedef void (*AvahiEntryGroupCallback) (AvahiEntryGroup *g, AvahiEntryGroupStat /** The function prototype for the callback of an AvahiDomainBrowser */ typedef void (*AvahiDomainBrowserCallback) (AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, char *domain, void *user_data); +/** The function prototype for the callback of an AvahiServiceBrowser */ +typedef void (*AvahiServiceBrowserCallback) (AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, char *name, char *type, char *domain, void *user_data); + /** The function prototype for the callback of an AvahiServiceTypeBrowser */ typedef void (*AvahiServiceTypeBrowserCallback) (AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, char *type, char *domain, void *user_data); @@ -136,6 +141,18 @@ AvahiServiceTypeBrowser* avahi_service_type_browser_new ( AvahiServiceTypeBrowserCallback callback, void *user_data); +/** Get the D-Bus path of an AvahiServiceBrowser object, for debugging purposes only. */ +char* avahi_service_browser_path (AvahiServiceBrowser *); + +/** Browse for services of a type on the local network */ +AvahiServiceBrowser* avahi_service_browser_new ( + AvahiClient *client, + AvahiIfIndex interface, + AvahiProtocol protocol, + char *type, + char *domain, + AvahiServiceBrowserCallback callback, + void *user_data); #ifndef DOXYGEN_SHOULD_SKIP_THIS AVAHI_C_DECL_END diff --git a/avahi-client/internal.h b/avahi-client/internal.h index ad39af3..722da2d 100644 --- a/avahi-client/internal.h +++ b/avahi-client/internal.h @@ -32,6 +32,7 @@ struct _AvahiClient void *user_data; AVAHI_LLIST_HEAD(AvahiEntryGroup, groups); AVAHI_LLIST_HEAD(AvahiDomainBrowser, domain_browsers); + AVAHI_LLIST_HEAD(AvahiServiceBrowser, service_browsers); AVAHI_LLIST_HEAD(AvahiServiceTypeBrowser, service_type_browsers); }; @@ -51,6 +52,14 @@ struct _AvahiDomainBrowser { AVAHI_LLIST_FIELDS(AvahiDomainBrowser, domain_browsers); }; +struct _AvahiServiceBrowser { + char *path; + AvahiClient *client; + AvahiServiceBrowserCallback callback; + void *user_data; + AVAHI_LLIST_FIELDS(AvahiServiceBrowser, service_browsers); +}; + struct _AvahiServiceTypeBrowser { char *path; AvahiClient *client; @@ -67,4 +76,5 @@ DBusHandlerResult avahi_domain_browser_event (AvahiClient *client, AvahiBrowserE DBusHandlerResult avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message); +DBusHandlerResult avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message); #endif -- cgit