~ubuntu-branches/ubuntu/lucid/exaile/lucid

« back to all changes in this revision

Viewing changes to plugins/daapserver/spydaap/zeroconf.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2010-02-12 19:51:01 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20100212195101-8jt3tculxcl92e6v
Tags: 0.3.1~b1-0ubuntu1
* New upstream release.
* Adjust exaile.install for new plugins.
* debian/control:
 - Drop unneeded python-dev Build-Dep.
 - Bump Standards-Version to 3.8.4 
* debian/rules: No empty po files to delete.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# adopted from http://stackp.online.fr/?p=35
 
2
 
 
3
__all__ = ["Zeroconf"]
 
4
 
 
5
import select, sys, traceback
 
6
 
 
7
class Zeroconf(object):
 
8
    """A simple class to publish a network service with zeroconf using
 
9
    avahi or pybonjour, preferring pybonjour.
 
10
    """
 
11
 
 
12
    class Helper(object):
 
13
        def __init__(self, name, port, **kwargs):
 
14
            self.name = name
 
15
            self.port = port
 
16
            self.stype = kwargs.get('stype', "_http._tcp")
 
17
            self.domain = kwargs.get('domain', '')
 
18
            self.host = kwargs.get('host', '')
 
19
            self.text = kwargs.get('text', '')
 
20
 
 
21
    class Pybonjour(Helper):
 
22
        def publish(self):
 
23
            import pybonjour
 
24
            #records as in mt-daapd
 
25
            txtRecord=pybonjour.TXTRecord()
 
26
            txtRecord['txtvers']            = '1'
 
27
            txtRecord['iTSh Version']       = '131073' #'196609'
 
28
            txtRecord['Machine Name']       = self.name
 
29
            txtRecord['Password']           = '0' # 'False' ?
 
30
            #txtRecord['Database ID']        = '' # 16 hex digits
 
31
            #txtRecord['Version']            = '196616'
 
32
            #txtRecord['iTSh Version']       =
 
33
            #txtRecord['Machine ID']         = '' # 12 hex digits
 
34
            #txtRecord['Media Kinds Shared'] = '0'
 
35
            #txtRecord['OSsi']               = '0x1F6' #?
 
36
            #txtRecord['MID']                = '0x3AA6175DD7155BA7', = database id - 2 ?
 
37
            #txtRecord['dmv']                = '131077'
 
38
 
 
39
            def register_callback(sdRef, flags, errorCode, name, regtype, domain):
 
40
                pass
 
41
 
 
42
            self.sdRef = pybonjour.DNSServiceRegister(name = self.name,
 
43
                                                      regtype = "_daap._tcp",
 
44
                                                      port = self.port,
 
45
                                                      callBack = register_callback,
 
46
                                                      txtRecord=txtRecord)
 
47
            
 
48
            while True:
 
49
                ready = select.select([self.sdRef], [], [])
 
50
                if self.sdRef in ready[0]:
 
51
                    pybonjour.DNSServiceProcessResult(self.sdRef)
 
52
                    break
 
53
 
 
54
        def unpublish(self):
 
55
            self.sdRef.close()
 
56
 
 
57
    class Avahi(Helper):
 
58
        def publish(self):
 
59
            import dbus, avahi
 
60
            bus = dbus.SystemBus()
 
61
            server = dbus.Interface(
 
62
                bus.get_object(
 
63
                    avahi.DBUS_NAME,
 
64
                    avahi.DBUS_PATH_SERVER),
 
65
                avahi.DBUS_INTERFACE_SERVER)
 
66
 
 
67
            self.group = dbus.Interface(
 
68
                bus.get_object(avahi.DBUS_NAME,
 
69
                               server.EntryGroupNew()),
 
70
                avahi.DBUS_INTERFACE_ENTRY_GROUP)
 
71
            
 
72
            self.group.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC,dbus.UInt32(0),
 
73
                         self.name, self.stype, self.domain, self.host,
 
74
                         dbus.UInt16(self.port), self.text)
 
75
            self.group.Commit()
 
76
 
 
77
        def unpublish(self):
 
78
            self.group.Reset()
 
79
 
 
80
    def __init__(self, *args, **kwargs):
 
81
        try:
 
82
            import pybonjour
 
83
            self.helper = Zeroconf.Pybonjour(*args, **kwargs)
 
84
        except:
 
85
            traceback.print_exc(file=sys.stdout)
 
86
            try:
 
87
                import avahi, dbus
 
88
                self.helper = Zeroconf.Avahi(*args, **kwargs)
 
89
            except:
 
90
                traceback.print_exc(file=sys.stdout)
 
91
                self.helper = None
 
92
                
 
93
    def publish(self):
 
94
        if self.helper != None:
 
95
            self.helper.publish()
 
96
 
 
97
    def unpublish(self):
 
98
        if self.helper != None:
 
99
            self.helper.unpublish()
 
100