Some days ago i wrote a post about avahi, here is the second part of it, the rant about the API it provides.
import dbus, gobject, avahi from dbus import DBusException from dbus.mainloop.glib import DBusGMainLoop # Looks for iTunes shares TYPE = '_daap._tcp' def service_resolved(*args): print 'service resolved' print 'name:', args[2] print 'address:', args[7] print 'port:', args[8] def print_error(*args): print 'error_handler' print args[0] def myhandler(interface, protocol, name, stype, domain, flags): print "Found service '%s' type '%s' domain '%s' " % (name, stype, domain) if flags & avahi.LOOKUP_RESULT_LOCAL: # local service, skip pass server.ResolveService(interface, protocol, name, stype, domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), reply_handler=service_resolved, error_handler=print_error) loop = DBusGMainLoop() bus = dbus.SystemBus(mainloop=loop) server = dbus.Interface( bus.get_object(avahi.DBUS_NAME, '/'), 'org.freedesktop.Avahi.Server') sbrowser = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, TYPE, 'local', dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_BROWSER) sbrowser.connect_to_signal("ItemNew", myhandler) gobject.MainLoop().run()
This is the example i cited in my earlier post as well. Let’s play a little game, whoever spots most retarded things in there wins
.
- Requires event loop too to scan network
- Depends on glib
- Requires the definition of 3 new objects (bus, server, sbrowser) and 2 functions (ignoring the error reporting one) before you can get the most basic “client” functionality!
- service_resolved get’s in a tuple (btw, it has 11 elements), leaving you without an idea what might be in there
- you pass one interface into another, WTF?!!?
But remember that is just “client” side, you’ll get to call lots of overcomplicated functions on the publish side as well!
Remeber the *args from above? It surely must make sense once you see it’s content, right? Guess again.
(dbus.Int32(4), dbus.Int32(0), dbus.String(u'b00'), dbus.String(u'_pydra._tcp'), dbus.String(u'local'), dbus.String(u'b00.local'), dbus.Int32(0), dbus.String(u'75.17.116.98'), dbus.UInt16(11890), dbus.Array([dbus.Array([dbus.Byte(102)], signature=dbus.Signature('y')), dbus.Array([dbus.Byte(116)], signature=dbus.Signature('y')), dbus.Array([dbus.Byte(119)], signature=dbus.Signature('y'))], signature=dbus.Signature('ay')), dbus.UInt32(13L))
Let’s play the same game as above with this piece of code
- it has very lousy integration with python types
- the TXT record is an array of arrays of bytes, just so that you can send a string (notice how avahi core developer says: “Yes indeed, python-dbus is a large pile of bugs. “) :/
- notice the dbus.Array([dbus.Array([dbus.Byte(102)], i dared to send a normal string and all i got was the last character!
- there must be a bright side? You can practice your function defining skills so that you can send strings!
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=b5aec771-36fd-476e-8f4a-54d71f0a2f28)