Esempio n. 1
0
    def test_register_browse_resolve(self):
        browse_done = threading.Event()
        resolve_done = threading.Event()

        def browse_cb(sdRef, flags, interfaceIndex, errorCode, serviceName,
                      regtype, replyDomain):
            self.assertEqual(errorCode, pybonjour.kDNSServiceErr_NoError)
            self.assertEqual(sdRef, browse_sdRef)
            self.assert_(flags & pybonjour.kDNSServiceFlagsAdd)
            self.assert_(isinstance(serviceName, unicode))
            self.assertEqual(serviceName, self.service_name)
            self.assert_(isinstance(regtype, unicode))
            self.assertEqual(regtype, self.regtype)
            self.assert_(isinstance(replyDomain, unicode))

            def resolve_cb(sdRef, flags, interfaceIndex, errorCode,
                           fullname, hosttarget, port, txtRecord):
                self.assertEqual(errorCode, pybonjour.kDNSServiceErr_NoError)
                self.assertEqual(sdRef, resolve_sdRef)
                self.assert_(isinstance(fullname, unicode))
                self.assertEqual(fullname, self.fullname)
                self.assert_(isinstance(hosttarget, unicode))
                self.assertEqual(port, self.port)
                self.assert_(isinstance(txtRecord, bytes))
                txt = TXTRecord.parse(txtRecord)
                self.assertEqual(txt['foo'], u'foobar')
                self.assert_(len(txtRecord) > 0)
                resolve_done.set()

            resolve_sdRef = DNSServiceResolve(0, interfaceIndex,
                                              serviceName, regtype,
                                              replyDomain, resolve_cb)

            try:
                self.wait_on_event(resolve_sdRef, resolve_done)
            finally:
                resolve_sdRef.close()

            browse_done.set()

        register_done, register_sdRef = self.register_record()

        try:
            self.wait_on_event(register_sdRef, register_done)

            browse_sdRef = DNSServiceBrowse(regtype=self.regtype,
                                            callBack=browse_cb)

            try:
                self.wait_on_event(browse_sdRef, browse_done)
            finally:
                browse_sdRef.close()
        finally:
            register_sdRef.close()
Esempio n. 2
0
    def test_register_browse_resolve(self):
        browse_done = threading.Event()
        resolve_done = threading.Event()

        def browse_cb(sdRef, flags, interfaceIndex, errorCode, serviceName,
                      regtype, replyDomain):
            self.assertEqual(errorCode, kDNSServiceErr_NoError)
            self.assertEqual(sdRef, browse_sdRef)
            self.assertTrue(flags & kDNSServiceFlagsAdd)
            self.assertTrue(isinstance(serviceName, str))
            self.assertEqual(serviceName, self.service_name)
            self.assertTrue(isinstance(regtype, str))
            self.assertEqual(regtype, self.regtype)
            self.assertTrue(isinstance(replyDomain, str))

            def resolve_cb(sdRef, flags, interfaceIndex, errorCode, fullname,
                           hosttarget, port, txtRecord):
                self.assertEqual(errorCode, kDNSServiceErr_NoError)
                self.assertEqual(sdRef, resolve_sdRef)
                self.assertTrue(isinstance(fullname, str))
                self.assertEqual(fullname, self.fullname)
                self.assertTrue(isinstance(hosttarget, str))
                self.assertEqual(port, self.port)
                self.assertTrue(isinstance(txtRecord, str))
                txt = TXTRecord.parse(txtRecord)
                self.assertEqual(txt['foo'], 'foobar')
                self.assertTrue(len(txtRecord) > 0)
                resolve_done.set()

            resolve_sdRef = DNSServiceResolve(0, interfaceIndex, serviceName,
                                              regtype, replyDomain, resolve_cb)

            try:
                self.wait_on_event(resolve_sdRef, resolve_done)
            finally:
                resolve_sdRef.close()

            browse_done.set()

        register_done, register_sdRef = self.register_record()

        try:
            self.wait_on_event(register_sdRef, register_done)

            browse_sdRef = DNSServiceBrowse(regtype=self.regtype,
                                            callBack=browse_cb)

            try:
                self.wait_on_event(browse_sdRef, browse_done)
            finally:
                browse_sdRef.close()
        finally:
            register_sdRef.close()
def fetch_bonjour(_service, alias='Bonjour', timeout=0.1):
    results = set()
    try:
        from pybonjour import DNSServiceBrowse, DNSServiceProcessResult
        from select import select
        bj_callback = lambda s, f, i, e, n, t, d: results.add('%s.%s' % (n.lower(), d[:-1]))
        bj_browser = DNSServiceBrowse(regtype=_service, callBack=bj_callback)
        select([bj_browser], [], [], timeout)
        DNSServiceProcessResult(bj_browser)
        bj_browser.close()
    except ImportError:
        pass
    return results
Esempio n. 4
0
def fetch_bonjour(_service, alias='Bonjour', timeout=0.1):
    cache = path.join(alfred.work(volatile=True), 'bonjour.1.json')
    if path.isfile(cache) and (time() - path.getmtime(cache) < 60):
        return (json.load(open(cache, 'r')), alias)
    results = set()
    try:
        from pybonjour import DNSServiceBrowse, DNSServiceProcessResult
        from select import select
        bj_callback = lambda s, f, i, e, n, t, d: results.add('%s.%s' % (n.lower(), d[:-1]))
        bj_browser = DNSServiceBrowse(regtype=_service, callBack=bj_callback)
        select([bj_browser], [], [], timeout)
        DNSServiceProcessResult(bj_browser)
        bj_browser.close()
    except ImportError:
        pass
    json.dump(list(results), open(cache, 'w'))
    return (results, alias)
Esempio n. 5
0
def fetch_bonjour(_service, alias='Bonjour', timeout=0.1):
    cache = path.join(alfred.work(volatile=True), 'bonjour.1.json')
    if path.isfile(cache) and (time() - path.getmtime(cache) < 60):
        return (json.load(open(cache, 'r')), alias)
    results = set()
    try:
        from pybonjour import DNSServiceBrowse, DNSServiceProcessResult
        from select import select
        bj_callback = lambda s, f, i, e, n, t, d: results.add('%s.%s' % (
            n.lower(), d[:-1]))
        bj_browser = DNSServiceBrowse(regtype=_service, callBack=bj_callback)
        select([bj_browser], [], [], timeout)
        DNSServiceProcessResult(bj_browser)
        bj_browser.close()
    except ImportError:
        pass
    json.dump(list(results), open(cache, 'w'))
    return (results, alias)
Esempio n. 6
0
def fetch_bonjour(_service='_ssh._tcp', alias='Bonjour', timeout=0.1):
    if int(os.getenv('alfredssh_bonjour', 1)) != 1:
        return (alias, ())
    cache = cache_file('bonjour.1.json')
    if os.path.isfile(cache) and (time() - os.path.getmtime(cache) < 60):
        return (alias, json.load(open(cache, 'r')))
    results = set()
    try:
        from pybonjour import DNSServiceBrowse, DNSServiceProcessResult
        from select import select
        bj_callback = lambda s, f, i, e, n, t, d: results.add('{}.{}'.format(n.lower(), d[:-1]))
        bj_browser = DNSServiceBrowse(regtype=_service, callBack=bj_callback)
        select([bj_browser], [], [], timeout)
        DNSServiceProcessResult(bj_browser)
        bj_browser.close()
    except ImportError:
        pass
    json.dump(list(results), open(cache, 'w'))
    return (alias, results)
Esempio n. 7
0
 def browse_domain(self, domain=None):
     try:
         self._browse_sdref = DNSServiceBrowse(
             regtype=self.stype,
             domain=domain,
             callBack=self._browse_callback)
         log.info('Starting to browse .local')
         return True
     except BonjourError as error:
         if error.errorCode == kDNSServiceErr_ServiceNotRunning:
             log.info('Service not running')
         else:
             log.error('Error while browsing for services. %s', error)
         return False