def OnExpandItem(self,event):
    
        item = event.GetItem()
        if item == self.tree.GetRootItem():
            return
            
        
        data = self.tree.GetPyData(item)
        if not data:    
            return
        self.tree.DeleteChildren(item)
        name,regtype,replyDomain = data
        
        self.resolvingItem = item
        sdRef2 = bonjour.AllocateDNSServiceRef()
        ret = bonjour.pyDNSServiceResolve(sdRef2,
                                          0,
                                          0,
                                          name,
                                          regtype,
                                          replyDomain,
                                          ResolveCallback,
                                          None)

        bonjour.DNSServiceProcessResult(sdRef2)
        def __init__(self, serviceName, regtype, url, port=9999):

            if port == 0:
                raise PublisherError(
                    'Service registered with invalid port %d' % (port, ))

            self.serviceRef = None
            self.registerFlag = threading.Event()

            # Create a text record
            txtRecordTxt = 'url=' + url
            txtRecord = chr(len(txtRecordTxt)) + txtRecordTxt
            txtRecordLen = len(txtRecord)

            # Allocate a service discovery reference and register the specified service
            self.serviceRef = bonjour.AllocateDNSServiceRef()
            ret = bonjour.pyDNSServiceRegister(self.serviceRef, 0, 0,
                                               serviceName, regtype, 'local.',
                                               None, port, txtRecordLen,
                                               txtRecord,
                                               self.__RegisterCallback, None)

            if ret != bonjour.kDNSServiceErr_NoError:
                raise PublisherError(ret)

            # Get the socket and loop
            fd = bonjour.DNSServiceRefSockFD(self.serviceRef)
            ret = select.select([fd], [], [])
            if ret[0]:
                ret = bonjour.DNSServiceProcessResult(self.serviceRef)
        def Run(self):

            self.running = 1

            # Allocate a service discovery ref and browse for the specified service type
            self.serviceRef = bonjour.AllocateDNSServiceRef()
            ret = bonjour.pyDNSServiceBrowse(self.serviceRef, 0, 0,
                                             self.serviceType, 'local.',
                                             self.__BrowseCallback, None)
            if ret != bonjour.kDNSServiceErr_NoError:
                print "ret = %d; exiting" % ret
                raise BrowserError('browse', ret)

            # Get socket descriptor
            fd = bonjour.DNSServiceRefSockFD(self.serviceRef)

            if fd <= 0:
                raise BrowserError('fd', fd)

            # Loop
            while self.IsRunning():
                # print "do select"
                ret = select.select([fd], [], [], self.timeout)
                if ret[0]:
                    #print "do process result"
                    ret = bonjour.DNSServiceProcessResult(self.serviceRef)
                    if ret != bonjour.kDNSServiceErr_NoError:
                        raise BrowserError('processresult', ret)
Esempio n. 4
0
def BrowseCallback(sdRef, flags, interfaceIndex, errorCode, serviceName,
                   regtype, replyDomain, userdata):
    if flags & bonjour.kDNSServiceFlagsAdd:
        sdRef2 = bonjour.AllocateDNSServiceRef()
        ret = bonjour.pyDNSServiceResolve(sdRef2, 0, 0, serviceName, regtype,
                                          replyDomain, ResolveCallback, None)

        bonjour.DNSServiceProcessResult(sdRef2)

    elif flags == 0:
        print "Service removed: ", serviceName, regtype
Esempio n. 5
0
def BrowseCallback(sdRef, flags, interfaceIndex, errorCode, serviceName,
                   regtype, replyDomain, userdata):
    if serviceName == servicename and flags & bonjour.kDNSServiceFlagsAdd:
        sys.stderr.write("Found service; resolving...\n")

        waitEvent.set()

        sdRef2 = bonjour.AllocateDNSServiceRef()
        ret = bonjour.pyDNSServiceResolve(sdRef2, 0, 0, serviceName, regtype,
                                          replyDomain, ResolveCallback, None)

        resolveEvent.clear()
        while not resolveEvent.isSet():
            bonjour.DNSServiceProcessResult(sdRef2)
def BrowseLoop():
    """
    Loop selecting file descriptors to process browse events
    """
    global serviceRefDict
    while 1:
        #print "serviceRefDict = ", serviceRefDict.keys()
        if not serviceRefDict:
            time.sleep(1)
            continue
        ret = select.select(serviceRefDict.keys(),[],[],1)
        #print "ret = ", ret
        for fd in ret[0]:
            serviceRef = serviceRefDict[fd]
            bonjour.DNSServiceProcessResult(serviceRef)
        def __BrowseCallback(self, sdRef, flags, interfaceIndex, errorCode,
                             serviceName, regtype, replyDomain, userdata):

            if flags & bonjour.kDNSServiceFlagsAdd:
                sdRef2 = bonjour.AllocateDNSServiceRef()
                ret = bonjour.pyDNSServiceResolve(sdRef2, 0, 0, serviceName,
                                                  regtype, replyDomain,
                                                  self.__ResolveCallback,
                                                  serviceName)

                bonjour.DNSServiceProcessResult(sdRef2)
                bonjour.DNSServiceRefDeallocate(sdRef2)

            elif flags == 0:
                self.lock.acquire()
                del self.serviceUrls[serviceName]
                self.lock.release()

                # Call callback (if registered) to signal delete
                if self.browseCallback:
                    self.browseCallback(self.DELETE, serviceName)
def BrowseServices():

    # Allocate a service discovery ref and browse for the specified service type
    serviceRef = bonjour.AllocateDNSServiceRef()
    ret   = bonjour.pyDNSServiceQueryRecord(serviceRef,
                                        0,  # no flags
                                        0,  # all network interfaces
                                        "_services._dns-sd._udp.local.",  # meta-query record name
                                        PTR_RECORD_TYPE,
                                        INTERNET_CLASS_TYPE, 
                                        QueryCallback,  # callback function ptr
                                        None)
    if ret != bonjour.kDNSServiceErr_NoError:
        print "ret = %d; exiting" % ret
        sys.exit(1)

    # Get socket descriptor and loop                       
    fd = bonjour.DNSServiceRefSockFD(serviceRef)
    while 1:
        ret = select.select([fd],[],[])
        ret = bonjour.DNSServiceProcessResult(serviceRef)

    # Deallocate the service discovery ref
    bonjour.DNSServiceRefDeallocate(serviceRef)
hostname = socket.gethostname()

# Allocate a service discovery reference and register the specified service
serviceRef = bonjour.AllocateDNSServiceRef()
ret = bonjour.pyDNSServiceRegister(
    serviceRef,  # DNSServiceRef           *sdRef,
    0,  # DNSServiceFlags         flags,         /* may be 0 */
    0,  # uint32_t                interfaceIndex,/* may be 0 */
    servicename,  # const char              *name,         /* may be NULL */
    regtype,  # const char              *regtype,
    'local.',  # const char              *domain,       /* may be NULL */
    hostname,  # const char              *host,         /* may be NULL */
    port,  # uint16_t                port,
    0,  # uint16_t                txtLen,
    "",  # const void              *txtRecord,    /* may be NULL */
    RegisterCallback,  # DNSServiceRegisterReply callBack,      /* may be NULL */
    userdata)

if ret != bonjour.kDNSServiceErr_NoError:
    print "error %d returned; exiting" % ret
    sys.exit(ret)

# Get the socket and loop
fd = bonjour.DNSServiceRefSockFD(serviceRef)
while 1:
    ret = select.select([fd], [], [])
    ret = bonjour.DNSServiceProcessResult(serviceRef)

# Deallocate the service discovery ref
bonjour.DNSServiceRefDeallocate(serviceRef)
Esempio n. 10
0
 def doRead(self):
     """Called by the Twited event loop when the fd is ready to read."""
     ret = bonjour.DNSServiceProcessResult(self.sdRef)