DNS-SD:有"mdnsjava"的经验吗? [英] DNS-SD: Experience with "mdnsjava"?

查看:187
本文介绍了DNS-SD:有"mdnsjava"的经验吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在将DNS-DS库" mdnsjava "集成到我的数据库中正如在多个位置提到的Android项目一样,例如在此处:

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:

是否还有其他Java除了JMDNS之外,还有bonjour/zeroconf的库吗?.

在实施时,我想知道这种实施是否真的在使用任何缓存和/或执行的稳定性如何.

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

现在我在过去两年中一直在使用jmDNS,但是该库在暂停发现(后台应用程序)时无法保留高速缓存.

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).

此外,jmDNS运行缓慢,发现设备后变得不稳定.

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

那么,有没有人对 mdnsjava 有任何经验?

So, has anyone any experience with mdnsjava?

推荐答案

同时,我可以说mdnsjava在大多数情况下都非常好且稳定.好多了比jMDNS更快.

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();

这篇关于DNS-SD:有"mdnsjava"的经验吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆