I am trying to use Bonjour from Java. I found an example of how to do it and I think I understood it. But I still do not understand why it should be done in such a complicated way. May be I miss something.
So, the code I found works in the following way (the code is also given bellow).
Java program tries to find a service and if the service is found, the program tries to "resolve" the service (I think "to resolve a service" means "to use a service" or "to connect to a service").
To "resolve" the found service we need to call "DNSSD.resolve" method and as the last argument of this method we need to give an object.
"DNSSD.resolve" tries to resolve a given service. If "DNSSD.resolve" is able to resolve the service it calls "serviceResolved" method of the instance given as the last argument. If "DNSSD.resolve" is unable to resolve the service, ti calls "operationFailed" method of the above mentioned object.
Here is the code:
DNSSD.resolve(0, ifIndex, serviceName, regType, domain, new ResolveListener(){
public void serviceResolved(DNSSDService resolver, int flags, int ifIndex,
String fullname, String hostname, int port, TXTRecord txtRecord){
InetAddress theAddress;
try {
theAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
// ouch..
}
}
public void operationFailed(DNSSDService arg0, int arg1) {
// ouch, again!
}
});
Would it be not simpler to organize the code in the following way.
We call "DNSSD.resolve" method with information about the service we want to resolve.
We do not pass any object to the "DNSSD.resolve".
"DNSSD.resolve" do not call any method of any class.
"DNSSD.resolve" tries to "resolve" a given service and, in the case it was able to do it, "DNSSD.resolve" returns true. Otherwise it returns false.
The program runs either "serviceResolved" or "operationFailed" methods depending on the value returned by the "DNSSD.resolve".
Or I just did not get used to the OOP way of thinking?