home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / dnssdresolve.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  4.4 KB  |  132 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Copyright (C) 2010 Red Hat, Inc.
  4. ## Authors:
  5. ##  Tim Waugh <twaugh@redhat.com>
  6.  
  7. ## This program is free software; you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation; either version 2 of the License, or
  10. ## (at your option) any later version.
  11.  
  12. ## This program is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ## GNU General Public License for more details.
  16.  
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with this program; if not, write to the Free Software
  19. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. import dbus
  22. from debug import *
  23.  
  24. class DNSSDHostNamesResolver:
  25.     def __init__ (self, devices):
  26.         self._devices = devices
  27.         self._unresolved = len (devices)
  28.         self._device_uri_by_name = {}
  29.         debugprint ("+%s" % self)
  30.  
  31.     def __del__ (self):
  32.         debugprint ("-%s" % self)
  33.  
  34.     def resolve (self, reply_handler):
  35.         self._reply_handler = reply_handler
  36.  
  37.         bus = dbus.SystemBus ()
  38.         if not bus:
  39.             reply_handler ([])
  40.             del self._devices
  41.             del self._reply_handler
  42.             return
  43.  
  44.         for uri, device in self._devices.iteritems ():
  45.             if not uri.startswith ("dnssd://"):
  46.                 self._unresolved -= 1
  47.                 continue
  48.  
  49.             # We need to resolve the DNS-SD hostname in order to
  50.             # compare with other network devices.
  51.             p = uri[8:].find ("/")
  52.             if p == -1:
  53.                 hostname = uri[8:]
  54.             else:
  55.                 hostname = uri[8:8+p]
  56.  
  57.             elements = hostname.split (".")
  58.             if len (elements) != 4:
  59.                 self._resolved ()
  60.                 continue
  61.  
  62.             name, stype, protocol, domain = elements
  63.             stype += "." + protocol #  e.g. _printer._tcp
  64.  
  65.             try:
  66.                 obj = bus.get_object ("org.freedesktop.Avahi", "/")
  67.                 server = dbus.Interface (obj,
  68.                                          "org.freedesktop.Avahi.Server")
  69.                 self._device_uri_by_name[(name, stype, domain)] = uri
  70.                 debugprint ("Resolving address for %s" % hostname)
  71.                 server.ResolveService (-1, -1,
  72.                                         name, stype, domain,
  73.                                         -1, 0,
  74.                                         reply_handler=self._reply,
  75.                                         error_handler=lambda e:
  76.                                             self._error (uri, e))
  77.             except dbus.DBusException, e:
  78.                 debugprint ("Failed to resolve address: %s" % e)
  79.                 self._resolved ()
  80.  
  81.     def _resolved (self):
  82.         self._unresolved -= 1
  83.         if self._unresolved == 0:
  84.             debugprint ("All addresses resolved")
  85.             self._reply_handler (self._devices)
  86.             del self._devices
  87.             del self._reply_handler
  88.  
  89.     def _reply (self, interface, protocol, name, stype, domain,
  90.                 host, aprotocol, address, port, txt, flags):
  91.         uri = self._device_uri_by_name[(name, stype, domain)]
  92.         self._devices[uri].address = address
  93.         debugprint ("%s is at %s" % (uri, address))
  94.         self._resolved ()
  95.  
  96.     def _error (self, uri, error):
  97.         debugprint ("Error resolving %s: %s" % (uri, error))
  98.         self._resolved ()
  99.  
  100. if __name__ == '__main__':
  101.     class Device:
  102.         def __repr__ (self):
  103.             try:
  104.                 return "<Device @ %s>" % self.address
  105.             except:
  106.                 return "<Device>"
  107.  
  108.     devices = {"dnssd://dlk-08E206-P1._printer._tcp.local/": Device(),
  109.                "dnssd://foo._printer._tcp.local/": Device()}
  110.     from dbus.glib import DBusGMainLoop
  111.     DBusGMainLoop (set_as_default=True)
  112.  
  113.     class Test:
  114.         def __init__ (self, loop, devices):
  115.             self._loop = loop
  116.             self._devices = devices
  117.  
  118.         def run (self):
  119.             r = DNSSDHostNamesResolver (self._devices)
  120.             r.resolve (reply_handler=self.reply)
  121.             return False
  122.  
  123.         def reply (self, *args):
  124.             print args
  125.             self._loop.quit ()
  126.  
  127.     import gobject
  128.     loop = gobject.MainLoop ()
  129.     set_debugging (True)
  130.     gobject.idle_add (Test (loop, devices).run)
  131.     loop.run ()
  132.