5

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?

Community
  • 1
  • 1
Martin L.
  • 3,006
  • 6
  • 36
  • 60

2 Answers2

4

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();
Martin L.
  • 3,006
  • 6
  • 36
  • 60
  • DEFAULT_MDNS_CACHE seems to be protected static field inside MulticastDNSCache class and I'm not able to access it as you showed above. – Krishnaswamy Subramanian Jun 09 '15 at 14:51
  • Yes, you need to either change it to public if you use the sources, or simply change the accessibility & access it via Reflection – Martin L. Jun 09 '15 at 19:11
  • Thank you Martin L. I will be picking the project back up, fixing the reported bugs and apply suggested fixes, such as the code you have provide. – posicks Sep 12 '15 at 15:05
  • Instead of accessing the Default Cache there should be a getCache() method available for you to gain direct access to the cache. The Default cache is not always used and can be swapped out at runtime for a new cache. – posicks Sep 12 '15 at 15:07
  • By chance, does anyone have a complete sample of how to use this library? I'm having no luck getting it to work! Also, if you can, see my question http://stackoverflow.com/questions/37353835/mdnsjava-hello-world-is-that-a-thing-i-can-do – teryret May 22 '16 at 21:03
1

I switched from JmDNS to mdnsjava because JmDNS simply didn't seem to work properly. Sometimes it would not detect anything.

I have very good experience with mdnsjava and have contributed slightly to it, by adding a pom.xml and pointed at a few simple bug-fixes. The only thing about mdnsjava is that it doesn't seem to be able to completely restart itself from scratch for some reason (reset exactly all state of the running program), but as long as it's up it works flawlessly for me. I didn't notice any other problems with my Android background-services using it. I pointed about the reset problem to the author, and he said he had experienced the same issue, and that if he had time, he might look at it some day.

JohnyTex
  • 3,323
  • 5
  • 29
  • 52
  • 1
    Hi J., thanks for your answer. I meanly also implemented mdnsjava into my project and it's working flawless in most times. I have developed a way to reset the whole discovery, and also start/stop the discovery in a "proper" way. I will try to place some code here.. – Martin L. Mar 12 '15 at 12:11