From 6bb829340ace002bcf92d94135d2e2d9b9a0cc22 Mon Sep 17 00:00:00 2001 From: Sebastien Estienne Date: Sun, 31 Jul 2005 01:04:52 +0000 Subject: [PATCH] * progress on python port of avahi-discover basic things are in place * it mays need to be clean and polished as i'm new to python * modified avahi-discover.glade to add a signal (cursor_changed) on the TreeView git-svn-id: file:///home/lennart/svn/public/avahi/trunk@206 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-discover/avahi-discover.glade | 9 +++ avahi-utils/avahi-discover | 117 ++++++++++++++++++++++++++-- 2 files changed, 120 insertions(+), 6 deletions(-) diff --git a/avahi-discover/avahi-discover.glade b/avahi-discover/avahi-discover.glade index 852f2ee..ca6b51f 100644 --- a/avahi-discover/avahi-discover.glade +++ b/avahi-discover/avahi-discover.glade @@ -18,6 +18,7 @@ False GDK_WINDOW_TYPE_HINT_NORMAL GDK_GRAVITY_NORTH_WEST + True @@ -43,6 +44,10 @@ False False True + False + False + False + @@ -67,6 +72,10 @@ 0.5 0 0 + PANGO_ELLIPSIZE_NONE + -1 + False + 0 0 diff --git a/avahi-utils/avahi-discover b/avahi-utils/avahi-discover index 3e720e8..78d440a 100755 --- a/avahi-utils/avahi-discover +++ b/avahi-utils/avahi-discover @@ -1,11 +1,23 @@ #!/usr/bin/python2.4 # -*-python-*- +# $Id$ import os import gtk import gobject from SimpleGladeApp import SimpleGladeApp +import avahi, dbus, gobject, sys + +try: + import dbus.glib +except ImportError, e: + pass + +service_type_browsers = {} +service_browsers = {} + + glade_dir = "../avahi-discover/" class Main_window(SimpleGladeApp): @@ -13,20 +25,93 @@ class Main_window(SimpleGladeApp): path = os.path.join(glade_dir, path) SimpleGladeApp.__init__(self, path, root, domain, **kwargs) + 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: + 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) + + + 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 print_error(err): + 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 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)] + parent = self.treemodel.iter_parent(treeiter) + self.treemodel.remove(treeiter) + del self.services_browsed[(interface, protocol, name, type, domain)] + if self.treemodel.iter_has_child(parent) == False: + self.treemodel.remove(parent) + del self.service_type[type] + + def new_service_type(self, interface, protocol, type, domain): + global service_browsers + + # Are we already browsing this domain for this type? + if service_browsers.has_key((interface, protocol, type, domain)): + return + + print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (type, 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.connect_to_signal('ItemNew', self.new_service) + b.connect_to_signal('ItemRemove', self.remove_service) + + service_browsers[(interface, protocol, type, domain)] = b + + def browse_domain(self, interface, protocol, domain): + global service_type_browsers + + # 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 + + def new_domain(self,interface, protocol, domain): + + # We browse for .local anyway... + if domain != "local": + browse_domain(interface, protocol, domain) + def update_label(self,interface, protocol, name, type, domain, host, aprotocol, address, port, txt): infos = "Service Type: %s\nService Name: %s\nDomain Name: %s\nInterface: %i.%i\nAddress: %s/%s:%i\nTXT Data: %s" % (type, name, domain, interface, protocol, host, address, port, str(txt)) self.info_label.set_markup(infos) def insert_row(self, model,parent, - firstcolumn,secondcolumn): + name, interface,protocol,type,domain): myiter=model.insert_after(parent,None) - model.set_value(myiter,0,firstcolumn) - model.set_value(myiter,1,secondcolumn) + model.set_value(myiter,0,name) + model.set_value(myiter,1,interface) + model.set_value(myiter,2,protocol) + model.set_value(myiter,3,type) + model.set_value(myiter,4,domain) return myiter def new(self): print "A new main_window has been created" - self.treemodel=gtk.TreeStore(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) self.tree_view.set_model(self.treemodel) #creating the columns headers @@ -42,9 +127,29 @@ class Main_window(SimpleGladeApp): self.tree_view.append_column(column) # testing with fake data + self.service_type = {} + self.services_browsed = {} + model=self.treemodel - self.insert_row(model,None,'Service Name', 'Interface') - self.update_label(1, 1, "My Web Server", "_http._tcp.", ".local", "xenon", "aprotocol", "172.16.100.2", 42, "txt") + + 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: + # 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.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() -- 2.39.5