views:

66

answers:

1

Here is a simple server application using Bonjour and written in Java. The main part of the code is given here:

public class ServiceAnnouncer implements IServiceAnnouncer, RegisterListener {
    private DNSSDRegistration serviceRecord;
    private boolean registered;

    public boolean isRegistered(){
        return registered;
    }

    public void registerService()  {
        try {
            serviceRecord = DNSSD.register(0,0,null,"_killerapp._tcp", null,null,1234,null,this);
        } catch (DNSSDException e) {
            // error handling here
        }
    }

    public void unregisterService(){
        serviceRecord.stop();
                registered = false;
    }

    public void serviceRegistered(DNSSDRegistration registration, int flags,String serviceName, String regType, String domain){
        registered = true;
    }

    public void operationFailed(DNSSDService registration, int error){
        // do error handling here if you want to.
    }
}

I understand it in the following way. We can try to register a service calling "registerService" method which, in its turn, calls "DNSSD.register" method. "DNSSD.register" try to register the service and, in general case, it can end up with two results: service was "successfully registered" and "registration failed". In both cases "DNSSD.register" calls a corresponding method (either "serviceRegistered" or "operationFailed") of the object which was given to the DNSSD.register as the last argument. And programmer decides what to put into "serviceRegistered" and "operationFailed". It is clear.

But should I try to register a service from the "operationFailed"? I am afraid that in this way my application will try to register the service too frequently. Should I put some "sleep" or "pause" into "operationFailed"? But in any case, it seems to me, that when the application is unable to register a service it will be also unable to do something else (for example to take care of GUI). Or may be DNSSD.register introduce some kind of parallelism? I mean it starts a new thread but that if I try to register service from "operation Failed", I could generate a huge number of the threads. Can it happen? If it is the case, should it be a problem? And if it is the case, how can I resolve this problem?

A: 

Yes, callbacks from the DNSSD APIs can come asynchronously from another thread. This exerpt from the O'Reilly book on ZeroConf networking gives some useful information.

I'm not sure retrying the registration from your operationFailed callback is a good idea. At least without some understanding of why the registration failed, is simply retrying it with the same parameters going to make sense?

David Gelhar