Example #1
0
class ServiceBrowser(IClientCallback, IServiceBrowserCallback, IServiceResolverCallback):
    # Client maintains its own thread as daemon andd returns
    # One Client can only handle one servicetype reliably
    # Use separate instance for each service

    def __init__(self, servicetype):
        """ Matching services are delivered to the callback method 
            serviceCallback which then resolves the services.
        """
        self._services = {}
        self._resolvers = ArrayList()
        self._client = Client(self)
        self._browser = self._client.createServiceBrowser(
            self, Avahi4JConstants.AnyInterface, Protocol.ANY, servicetype, None, 0
        )
        self._client.start()

    def stop(self):
        # release the browser first so no more ServiceResolver can be added
        # to the list
        self._browser.release()
        # we can now safely release items in the list
        for s in self._resolvers:
            s.release()
        # stop and release the client
        self._client.stop()
        self._client.release()

    def getServices(self):
        return self._services

    def serviceCallback(self, interfaceNum, proto, browserEvent, name, jtype, domain, lookupResultFlag):
        if browserEvent == BrowserEvent.NEW or browserEvent == BrowserEvent.REMOVE:
            # store service details
            key = hashlib.md5(str(interfaceNum) + str(proto) + name + jtype + domain).hexdigest()
            self._services.update(
                {key: {"interface": interfaceNum, "protocol": proto, "name": name, "type": jtype, "domain": domain}}
            )
            # only if it's a new service, resolve it
            if browserEvent == BrowserEvent.NEW:
                try:
                    # ServiceResolvers are kept open and a reference is stored
                    # in a list so they can be freed upon exit
                    self._resolvers.add(
                        self._client.createServiceResolver(
                            self, interfaceNum, proto, name, jtype, domain, Protocol.ANY, 0
                        )
                    )
                except Avahi4JException, e:
                    print "error creating resolver"
                    print str(e)
            else:
                del self._services[key]
Example #2
0
 def __init__(self, servicetype):
     """ Matching services are delivered to the callback method 
         serviceCallback which then resolves the services.
     """
     self._services = {}
     self._resolvers = ArrayList()
     self._client = Client(self)
     self._browser = self._client.createServiceBrowser(
         self, Avahi4JConstants.AnyInterface, Protocol.ANY, servicetype, None, 0
     )
     self._client.start()
Example #3
0
class ServicePublisher(IClientCallback, IEntryGroupCallback):
    # Client maintains its own thread as daemon andd returns
    # One Client can only handle one servicetype reliably
    # Use separate instance for each service

    def __init__(self, servicename, servicetype, servicesubtype, port, txtlist=None):
        self._client = Client(self)
        self._client.start()
        self._group = self._client.createEntryGroup(self)
        try:
            self._group.addService(
                Avahi4JConstants.AnyInterface, Protocol.ANY, servicename, servicetype, None, None, port, txtlist
            )
            if servicesubtype:
                self._group.addServicesubType(
                    Avahi4JConstants.AnyInterface, Protocol.ANY, servicename, servicetype, None, servicesubtype
                )
            self._group.commit()
        except Avahi4JException, e:
            print "Error publishing service by avahi4j\n", e
Example #4
0
 def __init__(self, servicename, servicetype, servicesubtype, port, txtlist=None):
     self._client = Client(self)
     self._client.start()
     self._group = self._client.createEntryGroup(self)
     try:
         self._group.addService(
             Avahi4JConstants.AnyInterface, Protocol.ANY, servicename, servicetype, None, None, port, txtlist
         )
         if servicesubtype:
             self._group.addServicesubType(
                 Avahi4JConstants.AnyInterface, Protocol.ANY, servicename, servicetype, None, servicesubtype
             )
         self._group.commit()
     except Avahi4JException, e:
         print "Error publishing service by avahi4j\n", e