Ejemplo n.º 1
0
    def makeService(self, options,
                    broadcast_protocol=None,
                    discovery_protocol=None):
        """ Accepts options as a regular twistd plugin does. Also accepts
            keyword arguments 'broadcast_protocol' for a procotcol *instance*
            and 'discovery_protocol' for a protocol *instance*. Returns a
            twisted.application.service.IService implementor.

        """
        service_name = options.get('service-name')
        resolve = options.get('resolve-domains')
        port = options.get('port')
        registry = options.get('registration')
        service_name = options.get('service-name')

        s = MultiService()
        s.setName('txbonjour-%s' % (service_name,))

        logging_proto = LoggingProtocol()

        if broadcast_protocol is None:
            broadcast_protocol = logging_proto

        if discovery_protocol is None:
            discovery_protocol = logging_proto

        discover_service = discovery.listenBonjour(discovery_protocol,
                                                   registry,
                                                   resolve_ips=resolve,
                                                   )
        discover_service.setName('discovery')
        discover_service.setServiceParent(s)
        
        def broadcast():
            broadcast_service = discovery.connectBonjour(broadcast_protocol,
                                                         registry,
                                                         port,
                                                         service_name,
                                                         )
            broadcast_service.setName('broadcast')
            broadcast_service.setServiceParent(s)
            
        reactor.callWhenRunning(broadcast)
        return s
Ejemplo n.º 2
0
    txbonjour.discovery.make_discover_service
    
"""
import sys

from twisted.internet import reactor, task, defer
from twisted.python import log

from txbonjour import discovery, tap

if __name__ == "__main__":
    log.startLogging(sys.stdout)
    proto = tap.LoggingProtocol()

    # testing with services
    dservice = discovery.listenBonjour(proto, '_nodes._udp',)
    bservice = discovery.connectBonjour(proto,
                                       regtype='_nodes._udp',
                                       port=9994,
                                       name='ExampleUDPService',
                                       )
    reactor.callWhenRunning(dservice.startService)
    reactor.callWhenRunning(bservice.startService)
    d = task.deferLater(reactor, 5, bservice.stopService)

    def exit(service):
        rm(service)
        log.msg('broadcast service stopped, exiting now')
        reactor.stop()
        
    rm = proto.removeService