]> git.meshlink.io Git - catta/commitdiff
DBUS: implement ServiceBrowser
authorLennart Poettering <lennart@poettering.net>
Fri, 29 Jul 2005 01:06:21 +0000 (01:06 +0000)
committerLennart Poettering <lennart@poettering.net>
Fri, 29 Jul 2005 01:06:21 +0000 (01:06 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@188 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-core/core.h
avahi-daemon/DBUS-API
avahi-daemon/dbus-protocol.c
avahi-daemon/dbus-test.py

index 8fa30e223885b771982b5666f67b8d3aad9d22bd..39a1db6d7f4cede07d8abfb02502059866cc610f 100644 (file)
@@ -501,9 +501,9 @@ typedef void (*AvahiServiceBrowserCallback)(
     AvahiIfIndex interface,
     AvahiProtocol protocol,
     AvahiBrowserEvent event,
-    const gchar *name,     /**< Service name, e.g. "Lennart's Files" */ 
-    const gchar *type,     /**< DNS-SD type, e.g. "_http._tcp" */
-    const gchar *domain,   /**< Domain of this service, e.g. "local" */
+    const gchar *name     /**< Service name, e.g. "Lennart's Files" */,
+    const gchar *type     /**< DNS-SD type, e.g. "_http._tcp" */,
+    const gchar *domain   /**< Domain of this service, e.g. "local" */,
     gpointer userdata);
 
 /** Create a new AvahiServiceBrowser object. */
@@ -511,7 +511,7 @@ AvahiServiceBrowser *avahi_service_browser_new(
     AvahiServer *server,
     AvahiIfIndex interface,
     AvahiProtocol protocol,
-    const gchar *service_type, /** DNS-SD service type, e.g. "_http._tcp" */
+    const gchar *service_type /** DNS-SD service type, e.g. "_http._tcp" */,
     const gchar *domain,
     AvahiServiceBrowserCallback callback,
     gpointer userdata);
index 651af661d033c3c68095164f9edeccfea69a8b57..4a8f561d90da33019ea72d81371768348ebe1ee8 100644 (file)
@@ -11,7 +11,7 @@ TODO:   [int32 interface, int32 protocol, string name, string type, string domai
         path EntryGroupNew()               -- Creates a new org.freedesktop.Avahi.EntryGroup object
         path DomainBrowserNew(int32 interface, int32 protocol, string domain, int32 btype)
         path ServiceTypeBrowserNew(int32 interface, int32 protocol, string domain)
-TODO:   path ServiceBrowserNew(int32 interface, int32 protocol, string type, string domain)
+        path ServiceBrowserNew(int32 interface, int32 protocol, string type, string domain)
         SIGNAL StateChanged(int32 state)
 
 org.freedesktop.Avahi.EntryGroup
@@ -32,7 +32,7 @@ org.freedesktop.Avahi.ServiceTypeBrowser
         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)
+org.freedesktop.Avahi.ServiceBrowser
+        void Free()
+        SIGNAL ItemNew(int32 interface, int32 protocol, string name, string type, string domain)
+        SIGNAL ItemRemove(int32 interface, int32 protocol, string name, string type, string domain)
index cde791d1a4a8797c8f2b56becdcb5d0b4db23d88..ab30a91cdbc657c9241d1a56c4d587d8651da8ec 100644 (file)
@@ -46,9 +46,7 @@
 #define AVAHI_DBUS_INTERFACE_SERVICE_BROWSER AVAHI_DBUS_NAME".ServiceBrowser"
 
 /* Needs wrapping:
-   - AvahiServiceResolver
-   - AvahiServiceTypeBrowser
-   - AvahiServiceBrowser */
+   - AvahiServiceResolver */
 
 typedef struct Server Server;
 typedef struct Client Client;
@@ -737,6 +735,74 @@ static void service_type_browser_callback(AvahiServiceTypeBrowser *b, AvahiIfInd
     dbus_message_unref(m);
 }
 
+static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
+    DBusError error;
+    ServiceBrowserInfo *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_BROWSER, "Free")) {
+
+        if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
+            avahi_log_warn("Error parsing ServiceBrowser::Free message");
+            goto fail;
+        }
+
+        service_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_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) {
+    ServiceBrowserInfo *i = userdata;
+    DBusMessage *m;
+    gint32 i_interface, i_protocol;
+    
+    g_assert(b);
+    g_assert(name);
+    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_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, &name,
+        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;
 
@@ -976,6 +1042,52 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
             goto fail;
         }
         
+        dbus_connection_register_object_path(c, i->path, &vtable, i);
+        return respond_path(c, m, i->path);
+        
+     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) {
+        Client *client;
+        ServiceBrowserInfo *i;
+        static const DBusObjectPathVTable vtable = {
+            NULL,
+            msg_service_browser_impl,
+            NULL,
+            NULL,
+            NULL,
+            NULL
+        };
+        gint32 interface, protocol;
+        gchar *domain, *type;
+        
+        if (!dbus_message_get_args(
+                m, &error,
+                DBUS_TYPE_INT32, &interface,
+                DBUS_TYPE_INT32, &protocol,
+                DBUS_TYPE_STRING, &type,
+                DBUS_TYPE_STRING, &domain,
+                DBUS_TYPE_INVALID) || !type || !*type) {
+            avahi_log_warn("Error parsing Server::ServiceBrowserNew message");
+            goto fail;
+        }
+
+        client = client_get(dbus_message_get_sender(m), TRUE);
+
+        if (!*domain)
+            domain = NULL;
+
+        i = g_new(ServiceBrowserInfo, 1);
+        i->id = ++client->current_id;
+        i->client = client;
+        i->path = g_strdup_printf("/org/freedesktop/Avahi/Client%u/ServiceBrowser%u", client->id, i->id);
+
+        AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i);
+
+        if (!(i->service_browser = avahi_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) {
+            avahi_log_warn("Failed to create service browser");
+            service_browser_free(i);
+            goto fail;
+        }
+        
         dbus_connection_register_object_path(c, i->path, &vtable, i);
         return respond_path(c, m, i->path);
      }
index 753c7d03acd04aa579224e5782ab2578c213d9e1..30bd261333d4734204aa84b007cc9671a1bfca3c 100755 (executable)
@@ -49,6 +49,13 @@ stb = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.ServiceTypeB
 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))
 
+def service_browser_callback(a, interface, protocol, name, type, domain):
+    print "SERVICE_BROWSER: %s %i %i %s %s %s" % (a, interface, protocol, name, type, domain)
+
+sb = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.ServiceBrowserNew(0, 0, "_http._tcp", "")), 'org.freedesktop.Avahi.ServiceBrowser')
+sb.connect_to_signal('ItemNew', lambda interface, protocol, name, type, domain: service_browser_callback("NEW", interface, protocol, name, type, domain))
+sb.connect_to_signal('ItemRemove', lambda interface, protocol, name, type, domain: service_browser_callback("REMOVE", interface, protocol, name, type, domain))
+
 try:
     gobject.MainLoop().run()
 except KeyboardInterrupt, k: