1

我正在尝试将当前使用 Avahi 原始 dbus 接口的一些代码移植到使用 Avahi gobject 接口(通过 python 的 gobject 自省)。

我得到了 ServiceBrowser,但我无法让 ServiceResolver 工作。这是我的代码:

#!/usr/bin/env python

import logging

from gi.repository import Avahi

logging.basicConfig(level=logging.DEBUG)
ac_log = logging.getLogger('Avahi.Client')
sb_log = logging.getLogger('Avahi.ServiceBrowser')
sr_log = logging.getLogger('Avahi.ServiceResolver')

def ac_state_changed(client, state):
    ac_log.debug('state_changed: {}'.format(state))

avahi_client = Avahi.Client(flags=0,)
avahi_client.connect('state-changed', ac_state_changed)
avahi_client.start()

def sb_new_service(browser, interface, protocol, name, type, domain, flags ):
    sb_log.debug('new: {}'.format(name))
    if not (flags & Avahi.LookupResultFlags.GA_LOOKUP_RESULT_LOCAL):
        sb_log.debug('resolving: {}'.format(name))
        service_resolver = Avahi.ServiceResolver(
            interface=interface,
            protocol=protocol,
            name=name,
            type=type,
            domain=domain,
            aprotocol=Avahi.Protocol.GA_PROTOCOL_UNSPEC,
            flags=0,)
        service_resolver.connect('found', sr_found)
        service_resolver.connect('failure', sr_failure)
        service_resolver.attach(avahi_client)

def sb_all_for_now(*args):
    sb_log.debug('all_for_now')
    seen_all = True

def sb_failure(*args):
    sb_log.error('failure: {}'.format(args))

def sr_found(*args):
    sr_log.debug('found: {}'.format(args))

def sr_failure(*args):
    sr_log.error('failure: {}'.format(args))


service_browser = Avahi.ServiceBrowser(
    domain='local',
    flags=0,
    interface=-1,
    protocol=Avahi.Protocol.GA_PROTOCOL_UNSPEC,
    type='_workstation._tcp')

service_browser.connect("new_service", sb_new_service)
service_browser.connect("failure", sb_failure)
service_browser.attach(avahi_client)


from gi.repository import GObject

mainloop = GObject.MainLoop()
mainloop.run()

示例输出:

DEBUG:Avahi.Client:state_changed: <enum GA_CLIENT_STATE_S_RUNNING of type GaClientState>
DEBUG:Avahi.ServiceBrowser:new: Mobi [d8:d3:85:e8:26:3d]
DEBUG:Avahi.ServiceBrowser:resolving: Mobi [d8:d3:85:e8:26:3d]
DEBUG:Avahi.ServiceBrowser:new: garyvdm [00:1d:09:a5:10:54]
DEBUG:Avahi.ServiceBrowser:new: noddy [00:15:c5:c6:46:4b]
DEBUG:Avahi.ServiceBrowser:resolving: noddy [00:15:c5:c6:46:4b]
DEBUG:Avahi.ServiceBrowser:new: crack [00:1c:26:20:e0:1d]
DEBUG:Avahi.ServiceBrowser:resolving: crack [00:1c:26:20:e0:1d]
DEBUG:Avahi.ServiceBrowser:new: opium [00:1d:09:c1:ed:cf]
DEBUG:Avahi.ServiceBrowser:resolving: opium [00:1d:09:c1:ed:cf]
DEBUG:Avahi.ServiceBrowser:new: gerard-vm [00:0c:29:d0:2a:af]
DEBUG:Avahi.ServiceBrowser:resolving: gerard-vm [00:0c:29:d0:2a:af]
DEBUG:Avahi.ServiceBrowser:all_for_now
^CTraceback (most recent call last):
  File "pacshare/xfer.py", line 65, in <module>
    mainloop.run()
KeyboardInterrupt

在按下 ctrl-c 之前,我让它运行了大约 1 分钟。如您所见,我正在调用 ServiceResolver,但从未调用 sr_found 和 sr_failure 方法。我怎样才能让它工作?

(我在http://code.ohloh.net/上找不到任何其他代码,所以我怀疑我可能是第一个尝试这样做的人)

4

1 回答 1

0

基本上你的代码工作得很好。它唯一的问题是引用计数

当python解释器到达末尾时sb_new_service(),它将删除所有不再被引用的对象。由于您已指定service_resolver为局部变量,因此该对象也将被删除。为了防止这种情况发生,您可以service_resolver像这样添加到解析器的(全局)列表中:

resolvers = []

def sb_new_service(browser, interface, protocol, name, type, domain, flags ):
    sb_log.debug('new: {}'.format(name))
    if not (flags & Avahi.LookupResultFlags.GA_LOOKUP_RESULT_LOCAL):
        sb_log.debug('resolving: {}'.format(name))
        service_resolver = Avahi.ServiceResolver(
            interface=interface,
            protocol=protocol,
            name=name,
            type=type,
            domain=domain,
            aprotocol=Avahi.Protocol.GA_PROTOCOL_UNSPEC,
            flags=0,)
        service_resolver.connect('found', sr_found)
        service_resolver.connect('failure', sr_failure)
        service_resolver.attach(avahi_client)
        global resolvers
        resolvers.append(service_resolver)

那么你的代码应该可以正常工作。

于 2014-03-28T13:09:14.810 回答