What is the best way to communicate with Bonjour?
There is a nice method provided by Bonjour: DNSSD.browse("_killerapp._tcp", this);
. As the first argument of this method I give type of service which potentially can be available in the network, and as the second argument I give a "callback object". The considered method "browse" for the services of the indicated type (first argument).
During the browsing it can "find" and then "lose" a service. If service is found (lost) bonjour call serviceFound (serviceLost) method of the callback object. The serviceFound is called with some parameters of the found service. In more details:
serviceFound(DNSSDService browser, int flags, int ifIndex, String serviceName, String regType, String domain)
But to get the IP address and port of the service we need to do additional operation (people call it "to resolve a service"). This is logic is kind of strange to me. Why this information cannot be given by serviceFound? I mean why Bonjour cannot resolve the service automatically whenever it finds a service.
Anyway, I just accept the given logic and try to use it. From the serviceFound
I call DNSSD.resolve(0, ifIndex, serviceName, regType, domain, this)
.
As before I give a callback object to the resolve
(the last argument). Unfortunately I need to use different classes to provide the callback objects for browse
and resolve
. The reason for that is that browse
and resolve
can call a operationFailed
method of the callback object and, if I use callback objects from the same class I will not know which method is calling the operationFailed
(browse
or resolve
).
So, I create a separate class to instantiate a callback object for the resolve
method. In this class I have a method called serviceResolved
which is called by Bonjour with IP address and port of the resolved service:
serviceResolved(DNSSDService resolver, int flags, int ifIndex, String fullname, String hostname, int port, TXTRecord txtRecord)
I think that the IP address and port should be fields of the objects which perform browsing. So, in the serviceResolved
I have IP and port and I want to set these values to the corresponding field of the instance which browse
the service. But how can I do it? This instance is not given as an argument of the serviceResolved
method. So, it is invisible!
Moreover, I see that serv开发者_StackOverflow中文版iceResolved and serviceFound take, as a first argument, DNSSDService resolver
. Does anybody know what is it? May be this object can be used to set parameters of the service? I know that an object of this type is returned by the browse
.
Not really an answer, but would like to point out that, besides the Bonjour library, you may want to try JmDNS, which is a pure Java, open sourced module.
精彩评论