]> git.meshlink.io Git - catta/blobdiff - avahi-utils/avahi-discover.in
limit allowed flags to avahi_server_add_service_subtype()
[catta] / avahi-utils / avahi-discover.in
index 404d2287ed71967c9405fee25f75fee459779ade..77ce72f07cc206dc8811b8d45e4c0218fd31f35f 100755 (executable)
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 # USA.
 
-import os
-import gtk
-import gobject
+import os, sys
 
-import avahi, dbus, gobject, sys
-from avahi.SimpleGladeApp import SimpleGladeApp
+try:
+    import avahi, gtk, gobject, dbus, avahi.ServiceTypeDatabase
+    from avahi.SimpleGladeApp import SimpleGladeApp
+except ImportError:
+    print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus."
+    sys.exit(1)
 
 try:
     import dbus.glib
@@ -34,9 +36,10 @@ except ImportError, e:
 service_type_browsers = {}
 service_browsers = {}
 
-
 glade_dir = "@interfacesdir@"
 
+service_type_db = avahi.ServiceTypeDatabase.ServiceTypeDatabase()
+
 class Main_window(SimpleGladeApp):
     def __init__(self, path="avahi-discover.glade", root="main_window", domain=None, **kwargs):
         path = os.path.join(glade_dir, path)
@@ -44,54 +47,105 @@ class Main_window(SimpleGladeApp):
 
     def on_tree_view_cursor_changed(self, widget, *args):
         (model, iter) = widget.get_selection().get_selected()
-        (name,interface,protocol,type,domain) = self.treemodel.get(iter,0,1,2,3,4)
-        if type == None:
+        (name,interface,protocol,stype,domain) = self.treemodel.get(iter,1,2,3,4,5)
+        if stype == None:
             self.info_label.set_markup("<i>No service currently selected.</i>")
             return
         #Asynchronous resolving
-        self.server.ResolveService( int(interface), int(protocol), name, type, domain, avahi.PROTO_UNSPEC, reply_handler=self.service_resolved, error_handler=self.print_error)
+        self.server.ResolveService( int(interface), int(protocol), name, stype, domain, avahi.PROTO_UNSPEC, 0, reply_handler=self.service_resolved, error_handler=self.print_error)
 
 
-    def service_resolved(self, interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
-        print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, type, domain, interface, protocol)
-        print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt))
-        self.update_label(interface, protocol, name, type, domain, host, aprotocol, address, port, str(txt))
+    def protoname(self,protocol):
+        if protocol == avahi.PROTO_INET:
+            return "IPv4"
+        if protocol == avahi.PROTO_INET6:
+            return "IPv6"
+            
+            
+    def siocgifname(self, interface):
+        if interface <= 0:
+            return "any"
+        else:
+            return self.server.GetNetworkInterfaceNameByIndex(interface)
+                        
+    def service_resolved(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
+        print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, stype, domain, interface, protocol)
+
+        print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
+
+        if len(txt) == 0:
+            txt_data = "<i>empty</i>"
+        else:
+            txt_data = str(avahi.txt_array_to_string_array(txt))
         
-    def print_error(err):
+        self.update_label(interface, protocol, name, stype, domain, host, aprotocol, address, port, txt_data)
+        
+    def print_error(self, err):
+        error_label = "<b>Error:</b> %s" % (err)
+        self.info_label.set_markup(error_label)
         print "Error:", str(err)
-            
-    def new_service(self, interface, protocol, name, type, domain):
-        print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, type, domain, interface, protocol)
-        if self.service_type.has_key(type) == False:
-            self.service_type[type] = self.insert_row(self.treemodel,None,type, interface,None,None,None)
-        treeiter = self.insert_row(self.treemodel,self.service_type[type],name, interface,protocol,type,domain)
-        self.services_browsed[(interface, protocol, name, type, domain)] = treeiter
 
+    def lookup_type(self, stype):
+        global service_type_db
 
-    def remove_service(self, interface, protocol, name, type, domain):
-        print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, type, domain, interface, protocol)
-        treeiter=self.services_browsed[(interface, protocol, name, type, domain)]
+        try:
+            return service_type_db[stype]
+        except KeyError:
+            return stype
+            
+    def new_service(self, interface, protocol, name, stype, domain, flags):
+        print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, stype, domain, interface, protocol)
+        if self.zc_ifaces.has_key((interface,protocol)) == False:
+            self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, str(self.siocgifname(interface))+" "+str(self.protoname(protocol)),None,interface,protocol,None,domain)
+        if self.zc_domains.has_key((interface,protocol,domain)) == False:
+            self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
+        if self.zc_types.has_key((interface,protocol,stype,domain)) == False:
+            thisDomain = self.zc_domains[(interface,protocol,domain)]
+            self.zc_types[(interface,protocol,stype,domain)] = self.insert_row(self.treemodel, thisDomain, self.lookup_type(stype), name, interface,None,None,None)
+        treeiter = self.insert_row(self.treemodel,self.zc_types[(interface,protocol,stype,domain)], name, name, interface,protocol,stype,domain)
+        self.services_browsed[(interface, protocol, name, stype, domain)] = treeiter
+        # expand the tree of this path
+        self.tree_view.expand_to_path(self.treemodel.get_path(treeiter))
+
+
+    def remove_service(self, interface, protocol, name, stype, domain, flags):
+        print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, stype, domain, interface, protocol)
+        self.info_label.set_markup("")
+        treeiter=self.services_browsed[(interface, protocol, name, stype, domain)]
         parent = self.treemodel.iter_parent(treeiter)
         self.treemodel.remove(treeiter)
-        del self.services_browsed[(interface, protocol, name, type, domain)]
+        del self.services_browsed[(interface, protocol, name, stype, domain)]
         if self.treemodel.iter_has_child(parent) == False:
-            self.treemodel.remove(parent)
-            del self.service_type[type]
+            treeiter=self.zc_types[(interface,protocol,stype,domain)]
+            parent = self.treemodel.iter_parent(treeiter)
+            self.treemodel.remove(treeiter)
+            del self.zc_types[(interface,protocol,stype,domain)]
+            if self.treemodel.iter_has_child(parent) == False:
+                treeiter=self.zc_domains[(interface,protocol,domain)]
+                parent = self.treemodel.iter_parent(treeiter)
+                self.treemodel.remove(treeiter)
+                del self.zc_domains[(interface,protocol,domain)]
+                if self.treemodel.iter_has_child(parent) == False:
+                    treeiter=self.zc_ifaces[(interface,protocol)]
+                    parent = self.treemodel.iter_parent(treeiter)
+                    self.treemodel.remove(treeiter)
+                    del self.zc_ifaces[(interface,protocol)]
+
  
-    def new_service_type(self, interface, protocol, type, domain):
+    def new_service_type(self, interface, protocol, stype, domain, flags):
         global service_browsers
 
         # Are we already browsing this domain for this type? 
-        if service_browsers.has_key((interface, protocol, type, domain)):
+        if service_browsers.has_key((interface, protocol, stype, domain)):
             return
         
-        print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (type, domain, interface, protocol)
+        print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (stype, domain, interface, protocol)
         
-        b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(interface, protocol, type, domain)),  avahi.DBUS_INTERFACE_SERVICE_BROWSER)
+        b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(interface, protocol, stype, domain, 0)),  avahi.DBUS_INTERFACE_SERVICE_BROWSER)
         b.connect_to_signal('ItemNew', self.new_service)
         b.connect_to_signal('ItemRemove', self.remove_service)
 
-        service_browsers[(interface, protocol, type, domain)] = b
+        service_browsers[(interface, protocol, stype, domain)] = b
 
     def browse_domain(self, interface, protocol, domain):
         global service_type_browsers
@@ -99,77 +153,70 @@ class Main_window(SimpleGladeApp):
         # Are we already browsing this domain?
         if service_type_browsers.has_key((interface, protocol, domain)):
             return
-            
-        print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
-        
-        b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceTypeBrowserNew(interface, protocol, domain)),  avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
-        b.connect_to_signal('ItemNew', self.new_service_type)
 
-        service_type_browsers[(interface, protocol, domain)] = b
+        if self.stype is None:
+            print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
+        
+            b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceTypeBrowserNew(interface, protocol, domain, 0)),  avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
+            b.connect_to_signal('ItemNew', self.new_service_type)
 
-    def new_domain(self,interface, protocol, domain):
+            service_type_browsers[(interface, protocol, domain)] = b
+        else:
+            new_service_type(interface, protocol, stype, domain)
 
-        # We browse for .local anyway...
+    def new_domain(self,interface, protocol, domain, flags):
+        if self.zc_ifaces.has_key((interface,protocol)) == False:
+            self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, str(self.siocgifname(interface))+" "+str(self.protoname(protocol)),None,interface,protocol,None,domain)
+        if self.zc_domains.has_key((interface,protocol,domain)) == False:
+            self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
         if domain != "local":
-            browse_domain(interface, protocol, domain)
+            self.browse_domain(interface, protocol, domain)
 
-    def update_label(self,interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
-        if protocol == avahi.PROTO_INET:
-            protocol = "IPv4"
-        if protocol == avahi.PROTO_INET6:
-            protocol = "IPv6"
-        infos = "<b>Service Type:</b> %s\n<b>Service Name:</b> %s\n<b>Domain Name:</b> %s\n<b>Interface:</b>#%i %s\n<b>Address:</b> %s/%s:%i\n<b>TXT Data:</b> %s" % (type, name, domain, interface, protocol, host, address, port, str(txt))
+    def update_label(self,interface, protocol, name, stype, domain, host, aprotocol, address, port, txt):
+        infos = "<b>Service Type:</b> %s\n<b>Service Name:</b> %s\n<b>Domain Name:</b> %s\n<b>Interface:</b> %s %s\n<b>Address:</b> %s/%s:%i\n<b>TXT Data:</b> %s" % (stype, name, domain, self.siocgifname(interface), self.protoname(protocol), host, address, port, str(txt))
         self.info_label.set_markup(infos)
 
     def insert_row(self, model,parent,
-                   name, interface,protocol,type,domain):
+                   content, name, interface,protocol,stype,domain):
         myiter=model.insert_after(parent,None)
-        model.set(myiter,0,name,1,interface,2,protocol,3,type,4,domain)
+        model.set(myiter,0,content,1,name,2,interface,3,protocol,4,stype,5,domain)
         return myiter
-    
+
     def new(self):
         print "A new main_window has been created"
-        self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
+        self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
         self.tree_view.set_model(self.treemodel)
 
         #creating the columns headers
-        self.tree_view.set_headers_visible(True)
+        self.tree_view.set_headers_visible(False)
         renderer=gtk.CellRendererText()
-        column=gtk.TreeViewColumn("Name",renderer, text=0)
+        column=gtk.TreeViewColumn("",renderer, text=0)
         column.set_resizable(True)
         column.set_sizing("GTK_TREE_VIEW_COLUMN_GROW_ONLY");
         column.set_expand(True);
         self.tree_view.append_column(column)
-        renderer=gtk.CellRendererText()
-        column=gtk.TreeViewColumn("Interface",renderer,
-                                  text=1)
-        column.set_resizable(True)
-        self.tree_view.append_column(column)
-        
-        # testing with fake data
-        self.service_type = {}
+
+        self.domain = None
+        self.stype = None
+        self.zc_ifaces = {}
+        self.zc_domains = {}
+        self.zc_types = {}
         self.services_browsed = {}
         
-        model=self.treemodel
-
-        domain = None
-
         self.bus = dbus.SystemBus()
         self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
 
-        if domain is None:
+        if self.domain is None:
             # Explicitly browse .local
             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
-
+                        
             # Browse for other browsable domains
-            db = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
+            db = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE, 0)), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
             db.connect_to_signal('ItemNew', self.new_domain)
-
         else:
             # Just browse the domain the user wants us to browse
             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
 
-
         
 def main():
     main_window = Main_window()