def run(self): # Register on bonjour chat self.id = random.randint(100000000000, 999999999999) txt = {} txt['1st'] = self.fullname txt['last'] = "" txt['status'] = 'avail' txt['port.p2pj'] = self.port txt['nick'] = self.fullname txt['jid'] = self.node txt['email'] = self.node txt['version'] = 1 txt['txtvers'] = 1 txt = pybonjour.TXTRecord(txt, strict=True) self.sdRef = pybonjour.DNSServiceRegister(name=self.node, regtype='_presence._tcp', port=self.port, txtRecord=txt, callBack=self._register_callback ) # Bonjour chat handcheck self.must_run = True while self.must_run: ready = select.select([self.sdRef], [], [], 1) if self.sdRef in ready[0]: pybonjour.DNSServiceProcessResult(self.sdRef)
def registerAirTunes(targetPort): params = { "txtvers": "1", "am": config.model, "ch": "2", "cn": "0,1,2,3", "da": "true", "et": "0,1,3", "md": "0,1,2", "pw": "false" if config.password is None else "true", "rhd": config.rhd, "rmodel": config.rmodel, "sf": "0x4", "sr": "44100", "ss": "16", "sv": "false", "tp": "UDP", "vn": "65537", "vs": config.server_version, "vv": "1" } sdRef = pybonjour.DNSServiceRegister( name="%s@%s" % ("".join(getHWAddress()), config.service_name), regtype="_raop._tcp", port=targetPort, txtRecord=buildTxtRecord(params)) atexit.register(sdRef.close)
def __init__(self, name, port): self.rsock = pybonjour.DNSServiceRegister(name=name, regtype="_hopper._tcp", port=port, callBack=self.register_callback, domain="local.") reactor.addReader(self)
def register_service(self, name, regtype, port): def register_callback(sdRef, flags, errorCode, name, regtype, domain): if errorCode == pb.kDNSServiceErr_NoError: logging.info('Registered service:') logging.info(' name = ' + name) logging.info(' regtype = ' + regtype) logging.info(' domain = ' + domain) else: logging.error('Failed to register service: {}'. format(errorCode)) #logging.debug('remove sd in sub: ' + str(sdRef)) self._done = True self._done = False self._lock.acquire() sd = pb.DNSServiceRegister(name=name, regtype=regtype, port=port, callBack=register_callback) self._input.append(sd) self._lock.release() while not self._stoped and not self._done: R, W, E = select(self._input, [], [], self.TIMEOUT) if not (R or W or E): # timeout continue for sd in R: pb.DNSServiceProcessResult(sd) self._lock.acquire() self._input = [] self._lock.release()
def publish(self): import pybonjour # records as in mt-daapd txtRecord = pybonjour.TXTRecord() txtRecord['txtvers'] = '1' txtRecord['iTSh Version'] = '131073' #'196609' txtRecord['Machine Name'] = self.name txtRecord['Password'] = '******' # 'False' ? #txtRecord['Database ID'] = '' # 16 hex digits #txtRecord['Version'] = '196616' #txtRecord['iTSh Version'] = #txtRecord['Machine ID'] = '' # 12 hex digits #txtRecord['Media Kinds Shared'] = '0' #txtRecord['OSsi'] = '0x1F6' #? #txtRecord['MID'] = '0x3AA6175DD7155BA7', = database id - 2 ? #txtRecord['dmv'] = '131077' def register_callback(sdRef, flags, errorCode, name, regtype, domain): pass self.sdRef = pybonjour.DNSServiceRegister( name=self.name, regtype="_daap._tcp", port=self.port, callBack=register_callback, txtRecord=txtRecord) while True: ready = select.select([self.sdRef], [], []) if self.sdRef in ready[0]: pybonjour.DNSServiceProcessResult(self.sdRef) break
def zeroconf_register(self, reg_type, name=None, port=None, txt_record=None): """ Registers a new service with Zeroconf/Bonjour/Avahi. :param reg_type: type of service to register, e.g. "_gntp._tcp" :param name: displayable name of the service, if not given defaults to the OctoPrint instance name :param port: port to register for the service, if not given defaults to OctoPrint's (public) port :param txt_record: optional txt record to attach to the service, dictionary of key-value-pairs """ if not pybonjour: return if not name: name = self.get_instance_name() if not port: port = self.port params = dict( name=name, regtype=reg_type, port=port ) if txt_record: params["txtRecord"] = pybonjour.TXTRecord(txt_record) key = (reg_type, port) self._sd_refs[key] = pybonjour.DNSServiceRegister(**params) self._logger.info("Registered {name} for {reg_type}".format(**locals()))
def __init__(self, port, xsize, ysize): gtk.Window.__init__(self) self.meme = Meme(port, self) self.meme_service = pybonjour.DNSServiceRegister(name = NAME_FORMAT % port, regtype = REGTYPE, port = port, callBack = None) self.set_title(NAME_FORMAT % port) self.xsize = xsize self.ysize = ysize self.buttons = [[gtk.Button() for y in range(ysize)] for x in range(xsize)] table = gtk.Table(homogeneous=True) self.add(table) table.props.row_spacing = 6 table.props.column_spacing = 6 table.props.border_width = 6 for x in range(xsize): for y in range(ysize): table.attach(self.buttons[x][y], x, x+1, y, y+1) b = self.buttons[x][y] b.set_size_request(32,32) b.connect("clicked", self.button_clicked, x, y) self.unlight_button(x, y) gobject.timeout_add(10, self.idle)
def __init__(self): self.sdRef = pybonjour.DNSServiceRegister( name=self.name, regtype=self.regtype, port=self.port, callBack=self._register_callback) self.tornado_callback = None
def create_service(self): txt = {} #remove empty keys for key, val in self.txt: if val: txt[key] = val txt['port.p2pj'] = self.port txt['version'] = 1 txt['txtvers'] = 1 # replace gajim's show messages with compatible ones if 'status' in self.txt: txt['status'] = self.replace_show(self.txt['status']) else: txt['status'] = 'avail' self.txt = pybonjour.TXTRecord(txt, strict=True) try: sdRef = pybonjour.DNSServiceRegister( name=self.name, regtype=self.stype, port=self.port, txtRecord=self.txt, callBack=self.service_added_callback) self.service_sdRef = sdRef except pybonjour.BonjourError, e: self.service_add_fail_callback(e)
def browser(self, sdRef, flags, ifidx, err, svc, rtype, dom): ''' Invoked when a new instance of the browsed service is identified. Attempt to repeat the service with the new service type, modified TXT record and altered service name. ''' # Do nothing if there was a browse error if err != mdns.kDNSServiceErr_NoError: return # Generate a unique key to identify the service to be repeated rptkey = ','.join(repr(s) for s in [svc, rtype, dom, ifidx]) # If the key already exists in the repeater dictionary, then # either the identified service has been removed or the service # has changed. Either way, stop repeating the old service. try: # Attempt to deregister the repeated service self.repeater[rptkey].close() # Attempt to eliminate the service name from the repeat list del self.repeater[rptkey] print('Stopped repeating', svc) except KeyError: pass # Nothing to do if the service is noted as removed if not (flags & mdns.kDNSServiceFlagsAdd): return # Add the prefix (and a space) to the existing service name rptname = self.prefix + ' ' + svc # Attempt to resolve the advertised service on the interface resref = mdns.DNSServiceResolve(0, ifidx, svc, rtype, dom, self.resolver) try: # Wait for the resolution to finish and return the record data rec = self.wait(resref) # If the resolution attempt yielded no useful result, # throw an exception to skip advertisement and ensure # the lookup is closed if rec is None: raise mdns.BonjourError(mdns.kDNSServiceErr_Unknown) # Register the new service on the same interface regref = mdns.DNSServiceRegister(0, ifidx, rptname, self.rpttype, dom, rec[0], rec[1], rec[2], self.register) try: # Copy the finished registration if successful if self.wait(regref): self.repeater[rptkey] = regref else: raise mdns.BonjourError(mdns.kDNSServiceErr_Unknown) except mdns.BonjourError: # Only close the reference in the event of a failure regref.close() print('Failed to register service', rptname) except mdns.BonjourError: print('Service', svc, 'not repeated') finally: resref.close()
def _announce(self): self._register_ref = pybonjour.DNSServiceRegister( name=self._name, regtype=self._regtype, port=self._port, callBack=self._register_callback) while True: ready = gevent.select.select([self._register_ref], [], []) if self._register_ref in ready[0]: pybonjour.DNSServiceProcessResult(self._register_ref)
def button1Click(self): #print "button1Click event handler" self.button1["background"] = "yellow" import select import sys import pybonjour name = 'gamepanel' regtype = '_gamepanel._tcp' port = 3333 ##name = sys.argv[1] ##regtype = sys.argv[2] ####port = int(sys.argv[3]) def register_callback(sdRef, flags, errorCode, name, regtype, domain): if errorCode == pybonjour.kDNSServiceErr_NoError: print 'Registered service:' print ' name =', name print ' regtype =', regtype print ' domain =', domain sdRef = pybonjour.DNSServiceRegister(name=name, regtype=regtype, port=port, callBack=register_callback) class IphoneChat(Protocol): def connectionMade(self): self.factory.clients.append(self) #print "clients are ", self.factory.clients def connectionLost(self, reason): self.factory.clients.remove(self) def dataReceived(self, data): a = data #print a if a == data: # import SendKeys SendKeys.SendKeys(a) time.sleep(1) # reactor.run(installSignallHandlers=True) def message(self, message): self.transport.write(message) factory = Factory() factory.protocol = IphoneChat factory.clients = [] reactor.listenTCP(3333, factory) self.button1["background"] = "green"
def __bonjour_register(self): print 'registering bonjour listener...' bjservice = pybonjour.DNSServiceRegister(name=self.bj_name, regtype="_delugefs._tcp", port=self.bt_port, callBack=self.__bonjour_register_callback) try: while True: ready = select.select([bjservice], [], []) if bjservice in ready[0]: pybonjour.DNSServiceProcessResult(bjservice) except KeyboardInterrupt: pass
def main(self): self.sdRef = sdRef = pybonjour.DNSServiceRegister(name = self.name, regtype = self.regtype, port = self.port, callBack = self.callback) try: try: while True: ready = select.select([sdRef], [], []) if sdRef in ready[0]: pybonjour.DNSServiceProcessResult(self.sdRef) except KeyboardInterrupt: pass finally: sdRef.close()
def add_virtual(self, name, xsize, ysize, port=0): device = Virtual(name, xsize, ysize, port) self.devices[name] = device sphost, spport = device.server_address service_name = '%s-%s' % (GRIDDLE_SERVICE_PREFIX, name) self.services[name] = pybonjour.DNSServiceRegister(name=service_name, regtype=REGTYPE, port=port, callBack=None) print "creating %s (%d)" % (name, spport) device.app_callback = self.route
def __init__(self, master_player): super(MasterServerThread, self).__init__() self.master_player = master_player self.watch_id = self.master_player.connect("finished", self.on_finished) self.running = False self.sdref = pybonjour.DNSServiceRegister(name='pi-wall', regtype=regtype, port=self.master_player.port) self.context = zmq.Context() self.socket = self.context.socket(zmq.PUB) self.socket.bind("tcp://*:%d" % self.master_player.port)
def start(self, uuid, name): logging.info('Starting Bonjour server') records_string = self.get_records_string(uuid.replace('-', ''), name) self.start_dns_service(name) self.bonjour_server = pybonjour.DNSServiceRegister( name = name, host = '%s.local' % name, domain='local', regtype = '_googlecast._tcp', port = 8009, txtRecord = records_string, callBack = self.register_callback) return
def bonjour_register_service(name, regtype, port, callback): try: callback_obj = BonjourCallbacks(callback) ref = pybonjour.DNSServiceRegister( name=name, regtype=regtype, port=port, callBack=callback_obj.register_callback) callback_obj.add_ref(ref) return callback_obj except pybonjour.BonjourError, e: logging.debug('bonjour_register_service: %s', str(e)) return None
def register(self, type, name, port, properties=None): def callback(sdref, flags, error, name, regtype, domain): self.logger.info( 'Registered service {0} (regtype {1}, domain {2})'.format( name, regtype, domain)) regtype = '_{0}._tcp'.format(type) txt = pybonjour.TXTRecord(items=(properties or {})) sdref = pybonjour.DNSServiceRegister(name=name, regtype=regtype, port=port, callBack=callback, txtRecord=txt) self.event_loop.register(sdref)
def register(self, name, regtype, port, txtRecord=None, callback=None): if callback is None: callback = register_callback if txtRecord is None: txtRecord = {} if not isinstance(txtRecord, pybonjour.TXTRecord): txtRecord = pybonjour.TXTRecord(txtRecord) sdRef = pybonjour.DNSServiceRegister(name=name, regtype=regtype, port=port, callBack=callback, txtRecord=txtRecord) self.rlist.append([sdRef, pybonjour.DNSServiceProcessResult, name, regtype]) return sdRef
def broadcast(self, regtype, port, name): def _callback(sdref, flags, errorCode, name, regtype, domain): if errorCode == pybonjour.kDNSServiceErr_NoError: d.callback((sdref, name, regtype, domain)) else: d.errback(errorCode) d = Deferred() sdref = pybonjour.DNSServiceRegister(name=name, regtype=regtype, port=port, callBack=_callback) reactor.addReader(Advertiser.ServiceDescriptor(sdref)) return d
def register(self): """ An instance of DNSServiceRef (sdRef) represents an active connection to mdnsd. """ mDNSServices = {} for srv in ServiceType: mDNSServices.update({srv.name: {}}) self.service = srv.name self.regtype, self.port = ServiceType[self.service].value if not self._is_running(): continue txtrecord, interfaceIndex = self._generate_txtRecord() if txtrecord is None: continue port = self._get_port() self.logger.trace( 'Registering mDNS service host: %s, regtype: %s, port: %s, interface: %s, TXTRecord: %s', self.hostname, self.regtype, port, interfaceIndex, txtrecord) for i in interfaceIndex: mDNSServices[srv.name].update({ i: { 'sdRef': None, 'interfaceIndex': i, 'regtype': self.regtype, 'port': port, 'txtrecord': txtrecord, 'name': self.hostname } }) mDNSServices[ srv.name][i]['sdRef'] = pybonjour.DNSServiceRegister( name=self.hostname, regtype=self.regtype, interfaceIndex=i, port=port, txtRecord=txtrecord, callBack=None) self.finished.wait() for srv in mDNSServices.keys(): for i in mDNSServices[srv].keys(): self.logger.trace('Unregistering %s %s.', mDNSServices[srv][i]['name'], mDNSServices[srv][i]['regtype']) mDNSServices[srv][i]['sdRef'].close()
def register(self, name, regtype, port): def register_callback(sdRef, flags, errorCode, name, regtype, domain): if errorCode == pybonjour.kDNSServiceErr_NoError: log.debug('Registered service : name = {}, regtype = {}, domain = {}'.format(name, regtype, domain)) self.sdRef = pybonjour.DNSServiceRegister(name = name, regtype = regtype, port = port, callBack = register_callback) while True: ready = select.select([self.sdRef], [], []) if self.sdRef in ready[0]: pybonjour.DNSServiceProcessResult(self.sdRef) break;
def run(self): name = 'TellStick' # TODO self.sdRef = pybonjour.DNSServiceRegister( name=name, regtype='_hap._tcp', port=self.port, txtRecord=pybonjour.TXTRecord(self.txt), callBack=self.register) try: while True: ready = select.select([self.sdRef], [], []) if self.sdRef in ready[0]: pybonjour.DNSServiceProcessResult(self.sdRef) finally: self.sdRef.close()
def register(name, regtype, port): sdRef = pybonjour.DNSServiceRegister(name=name, regtype=regtype, port=port, callBack=None) try: try: while True: ready = select.select([sdRef], [], []) if sdRef in ready[0]: pybonjour.DNSServiceProcessResult(sdRef) except KeyboardInterrupt: pass finally: sdRef.close()
def initialize_bonjour(): sdRef = pybonjour.DNSServiceRegister(name=_service_name, regtype="_linconnect._tcp", port=int(parser.get('connection', 'port')), callBack=register_callback) try: try: while True: ready = select.select([sdRef], [], []) if sdRef in ready[0]: pybonjour.DNSServiceProcessResult(sdRef) except KeyboardInterrupt: pass finally: sdRef.close()
def register_service(name, regtype, port): def connection_ready(service, fd, events): pybonjour.DNSServiceProcessResult(service) def register_callback(sdRef, flags, errorCode, name, regtype, domain): if errorCode == pybonjour.kDNSServiceErr_NoError: logger.debug('Registered bonjour service %s.%s', name, regtype) service = pybonjour.DNSServiceRegister(name=name, regtype=regtype, port=port, callBack=register_callback) io_loop = ioloop.IOLoop.instance() callback = functools.partial(connection_ready, service) io_loop.add_handler(service.fileno(), callback, io_loop.READ)
def start(self): self.registered = False register = pybonjour.DNSServiceRegister( name=self.name, regtype=self.type, port=self.port, callBack=self.register_callback ) while not self.registered: readable, writable, exceptional = select.select([register], [], []) if register in readable: pybonjour.DNSServiceProcessResult(register) self.register = register
def register(name, regtype, port): if not pybonjour: return sdRef = pybonjour.DNSServiceRegister(name=name, regtype=regtype, port=port, callBack=registration_callback) try: while 1: ready = select([sdRef], [], []) if sdRef in ready[0]: pybonjour.DNSServiceProcessResult(sdRef) return state.announce finally: state.announce = None atexit.register(sdRef.close)
def register(self): self.close() self.__sdref = pybonjour.DNSServiceRegister( name=self.__name, regtype=self.__type, port=self.__port, txtRecord=encode_txt_record(self.__record), callBack=self.__callback) ready = select.select([self.__sdref], [], [], self.timeout) if self.__sdref not in ready[0]: self.close() else: pybonjour.DNSServiceProcessResult(self.__sdref)