From: Lennart Poettering Date: Fri, 29 Jul 2005 00:21:11 +0000 (+0000) Subject: * DBUS: implement service type browsing X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=42226b6a47b0e067cbf2a327fcd83b0b3fe9f48e;p=catta * DBUS: implement service type browsing git-svn-id: file:///home/lennart/svn/public/avahi/trunk@186 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-daemon/DBUS-API b/avahi-daemon/DBUS-API index 9e9e293..651af66 100644 --- a/avahi-daemon/DBUS-API +++ b/avahi-daemon/DBUS-API @@ -10,7 +10,7 @@ org.freedesktop.Avahi.Server -- Accessible through /org/freedeskto TODO: [int32 interface, int32 protocol, string name, string type, string domain, string host, int32 aprotocol, string address, uint16 port, string txt[]] ResolveService(int32 interface, int32 protocol, string name, string type, string domain, int32 aprotocol) path EntryGroupNew() -- Creates a new org.freedesktop.Avahi.EntryGroup object path DomainBrowserNew(int32 interface, int32 protocol, string domain, int32 btype) -TODO: path ServiceTypeBrowserNew(int32 interface, int32 protocol, string domain) + path ServiceTypeBrowserNew(int32 interface, int32 protocol, string domain) TODO: path ServiceBrowserNew(int32 interface, int32 protocol, string type, string domain) SIGNAL StateChanged(int32 state) @@ -27,14 +27,12 @@ org.freedesktop.Avahi.DomainBrowser SIGNAL ItemNew(int32 interface, int32 protocol, string domain) SIGNAL ItemRemove(int32 interface, int32 protocol, string domain) -TODO: org.freedesktop.Avahi.ServiceTypeBrowser -TODO: void Free() - -TODO: SIGNAL ItemNew(int32 interface, int32 protocol, string type, string domain) -TODO: SIGNAL ItemRemove(int32 interface, int32 protocol, string type, string domain) +org.freedesktop.Avahi.ServiceTypeBrowser + void Free() + SIGNAL ItemNew(int32 interface, int32 protocol, string type, string domain) + SIGNAL ItemRemove(int32 interface, int32 protocol, string type, string domain) TODO: org.freedesktop.Avahi.ServiceBrowser TODO: void Free() - TODO: SIGNAL ItemNew(int32 interface, int32 protocol, string name, string type, string domain) TODO: SIGNAL ItemRemove(int32 interface, int32 protocol, string name, string type, string domain) diff --git a/avahi-daemon/dbus-protocol.c b/avahi-daemon/dbus-protocol.c index 14f550b..cde791d 100644 --- a/avahi-daemon/dbus-protocol.c +++ b/avahi-daemon/dbus-protocol.c @@ -47,7 +47,6 @@ /* Needs wrapping: - AvahiServiceResolver - - AvahiDomainBrowser - AvahiServiceTypeBrowser - AvahiServiceBrowser */ @@ -310,6 +309,17 @@ static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) { return DBUS_HANDLER_RESULT_HANDLED; } +static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const gchar *path) { + DBusMessage *reply; + + reply = dbus_message_new_method_return(m); + dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID); + dbus_connection_send(c, reply, NULL); + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; +} + static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) { GMainLoop *loop = userdata; DBusError error; @@ -661,6 +671,72 @@ static void domain_browser_callback(AvahiDomainBrowser *b, AvahiIfIndex interfac dbus_message_unref(m); } +static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) { + DBusError error; + ServiceTypeBrowserInfo *i = userdata; + + g_assert(c); + g_assert(m); + g_assert(i); + + dbus_error_init(&error); + + avahi_log_debug("dbus: interface=%s, path=%s, member=%s", + dbus_message_get_interface(m), + dbus_message_get_path(m), + dbus_message_get_member(m)); + + /* Access control */ + if (strcmp(dbus_message_get_sender(m), i->client->name)) + return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL); + + if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) { + + if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) { + avahi_log_warn("Error parsing ServiceTypeBrowser::Free message"); + goto fail; + } + + service_type_browser_free(i); + return respond_ok(c, m); + + } + + avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m)); + +fail: + if (dbus_error_is_set(&error)) + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +static void service_type_browser_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *type, const gchar *domain, gpointer userdata) { + ServiceTypeBrowserInfo *i = userdata; + DBusMessage *m; + gint32 i_interface, i_protocol; + + g_assert(b); + g_assert(type); + g_assert(domain); + g_assert(i); + + i_interface = (gint32) interface; + i_protocol = (gint32) protocol; + + m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove"); + dbus_message_append_args( + m, + DBUS_TYPE_INT32, &i_interface, + DBUS_TYPE_INT32, &i_protocol, + DBUS_TYPE_STRING, &type, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INVALID); + dbus_message_set_destination(m, i->client->name); + dbus_connection_send(server->bus, m, NULL); + dbus_message_unref(m); +} + static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) { DBusError error; @@ -718,7 +794,6 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void NULL, NULL }; - DBusMessage *reply; if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) { avahi_log_warn("Error parsing Server::EntryGroupNew message"); @@ -740,12 +815,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void } dbus_connection_register_object_path(c, i->path, &vtable, i); - reply = dbus_message_new_method_return(m); - dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &i->path, DBUS_TYPE_INVALID); - dbus_connection_send(c, reply, NULL); - dbus_message_unref(reply); - - return DBUS_HANDLER_RESULT_HANDLED; + return respond_path(c, m, i->path); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) { Client *client; @@ -817,7 +887,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void return DBUS_HANDLER_RESULT_HANDLED; - } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) { + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) { Client *client; DomainBrowserInfo *i; static const DBusObjectPathVTable vtable = { @@ -828,7 +898,6 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void NULL, NULL }; - DBusMessage *reply; gint32 interface, protocol, type; gchar *domain; @@ -863,14 +932,54 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void } dbus_connection_register_object_path(c, i->path, &vtable, i); - reply = dbus_message_new_method_return(m); - dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &i->path, DBUS_TYPE_INVALID); - dbus_connection_send(c, reply, NULL); - dbus_message_unref(reply); + return respond_path(c, m, i->path); + + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew")) { + Client *client; + ServiceTypeBrowserInfo *i; + static const DBusObjectPathVTable vtable = { + NULL, + msg_service_type_browser_impl, + NULL, + NULL, + NULL, + NULL + }; + gint32 interface, protocol; + gchar *domain; - return DBUS_HANDLER_RESULT_HANDLED; - } - + if (!dbus_message_get_args( + m, &error, + DBUS_TYPE_INT32, &interface, + DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INVALID)) { + avahi_log_warn("Error parsing Server::ServiceTypeBrowserNew message"); + goto fail; + } + + client = client_get(dbus_message_get_sender(m), TRUE); + + if (!*domain) + domain = NULL; + + i = g_new(ServiceTypeBrowserInfo, 1); + i->id = ++client->current_id; + i->client = client; + i->path = g_strdup_printf("/org/freedesktop/Avahi/Client%u/ServiceTypeBrowser%u", client->id, i->id); + + AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i); + + if (!(i->service_type_browser = avahi_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) { + avahi_log_warn("Failed to create service type browser"); + service_type_browser_free(i); + goto fail; + } + + dbus_connection_register_object_path(c, i->path, &vtable, i); + return respond_path(c, m, i->path); + } + avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m)); diff --git a/avahi-daemon/dbus-test.py b/avahi-daemon/dbus-test.py index 11dd5ae..753c7d0 100755 --- a/avahi-daemon/dbus-test.py +++ b/avahi-daemon/dbus-test.py @@ -5,8 +5,6 @@ import gobject try: import dbus.glib except ImportError, e: pass -from time import sleep - bus = dbus.SystemBus() server = dbus.Interface(bus.get_object("org.freedesktop.Avahi", '/org/freedesktop/Avahi/Server'), 'org.freedesktop.Avahi.Server') @@ -41,10 +39,16 @@ def domain_browser_callback(a, interface, protocol, domain): print "DOMAIN_BROWSER: %s %i %i %s" % (a, interface, protocol, domain) db = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.DomainBrowserNew(0, 0, "", 2)), 'org.freedesktop.Avahi.DomainBrowser') - db.connect_to_signal('ItemNew', lambda interface, protocol, domain: domain_browser_callback("NEW", interface, protocol, domain)) db.connect_to_signal('ItemRemove', lambda interface, protocol, domain: domain_browser_callback("REMOVE", interface, protocol, domain)) +def service_type_browser_callback(a, interface, protocol, type, domain): + print "SERVICE_TYPE_BROWSER: %s %i %i %s %s" % (a, interface, protocol, type, domain) + +stb = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.ServiceTypeBrowserNew(0, 0, "")), 'org.freedesktop.Avahi.ServiceTypeBrowser') +stb.connect_to_signal('ItemNew', lambda interface, protocol, type, domain: service_type_browser_callback("NEW", interface, protocol, type, domain)) +stb.connect_to_signal('ItemRemove', lambda interface, protocol, type, domain: service_type_browser_callback("REMOVE", interface, protocol, type, domain)) + try: gobject.MainLoop().run() except KeyboardInterrupt, k: @@ -52,5 +56,6 @@ except KeyboardInterrupt, k: g.Free() db.Free() +stb.Free() print "Quit"