javadns-sdmdnsjava

DNS-SD: Experience with "mdnsjava"?


I'm right now implementing the DNS-DS library "mdnsjava" into my Android-project as it's mentioned at several positions, for example here at SO:

Are there any other Java libraries for bonjour/zeroconf apart from JMDNS?.

While implementing, I wonder if this implementation is really using any cache and/or how stable it might perform.

Right now I'm using jmDNS for the last 2 years but this library wasn't able to keep the cache while pausing the discovery (app in background).

Additionally, jmDNS was slow & unstable with discovering the devices.

So, has anyone any experience with mdnsjava?


Solution

  • Meanwhile I can say, that mdnsjava is working very very good and stable in most situations. Much better & faster compared to jMDNS.

    Here's some code to restart the full discovery and to start/stop the discovery, maybe it helps someone:

    MulticastDNSService mDNSService = null;
    Browse browse = null;
    Object serviceDiscoveryInstance = null;
    
    public void stop() {
        try {
            if (serviceDiscoveryInstance != null && mDNSService != null) {
                mDNSService.stopServiceDiscovery(serviceDiscoveryInstance);
                mDNSService.close();
            }
    
            serviceDiscoveryInstance = null;
            //mDNSService = null;
            if (browse != null) {
                browse.close();
                // this is required, otherwise the listeners won't get called in next run
                browse = null;
            }
    
            Querier querier = MulticastDNSLookupBase.getDefaultQuerier();
            if (querier != null) {
                querier.close();
            }
            MulticastDNSLookupBase.setDefaultQuerier(null);
        } catch (Exception e) {
            Log(..)
        }
    }
    
    public void start() {
        try {
            Querier querier = MulticastDNSLookupBase.getDefaultQuerier();
            if (querier != null) {
                if (mDNSService == null) {
                    mDNSService = new MulticastDNSService();
                }
    
                if (browse == null) {
                    browse = new Browse(SERVICE_TYPE);
                }
    
                if (serviceDiscoveryInstance == null) {
                    serviceDiscoveryInstance = mDNSService.startServiceDiscovery(browse, this);
                }
    
                // add existing entries
                Lookup resolve = new Lookup(SERVICE_TYPE);
                resolve.setQuerier(mDNSService.getQuerier());
                ServiceInstance[] services = resolve.lookupServices();
                for (ServiceInstance service : services) {
                    addDevice(service);
                }
                resolve.close();
            } else {
                Log.e("Cannot start mDNS-discovery because querier is not set up!");
                resetDiscovery();
            }
        } catch (Exception e) {
            Log.e("Error while discovering network.", e);
            resetDiscovery();
        }
    }
    
    public void clearCaches() {
        if (MulticastDNSCache.DEFAULT_MDNS_CACHE != null) {
            MulticastDNSCache.DEFAULT_MDNS_CACHE.clearCache();
        }
        mDNSService = null;
        browse = null;
    }
    
    private void resetDiscovery(){
        stop();
        mDNSService = null;
        browse = null;
    }
    

    You can start/stop the discovery with the mentioned methods, and reset the whole discovery via

    stop();
    clearCaches();
    start();