From f54703aa88f6527729301c50505876bcebfb8e47 Mon Sep 17 00:00:00 2001 From: Hui Li Date: Tue, 26 Oct 2021 10:43:09 +0800 Subject: [PATCH 1/6] added publish and remove for dnssd in android platform --- src/platform/android/DnssdImpl.cpp | 92 ++++++++++++++++++- .../platform/NsdManagerServiceResolver.java | 10 ++ .../java/chip/platform/ServiceResolver.java | 22 +++++ 3 files changed, 120 insertions(+), 4 deletions(-) diff --git a/src/platform/android/DnssdImpl.cpp b/src/platform/android/DnssdImpl.cpp index 27f3fa26203ed8..b5893760affa44 100644 --- a/src/platform/android/DnssdImpl.cpp +++ b/src/platform/android/DnssdImpl.cpp @@ -17,6 +17,7 @@ #include "DnssdImpl.h" +#include #include #include #include @@ -39,6 +40,8 @@ namespace { jobject sResolverObject = nullptr; jobject sMdnsCallbackObject = nullptr; jmethodID sResolveMethod = nullptr; +jmethodID sPublishMethod = nullptr; +jmethodID sRemoveMethod = nullptr; } // namespace // Implemention of functions declared in lib/dnssd/platform/Dnssd.h @@ -59,17 +62,85 @@ CHIP_ERROR ChipDnssdShutdown() CHIP_ERROR ChipDnssdRemoveServices() { - return CHIP_ERROR_NOT_IMPLEMENTED; + VerifyOrReturnError(sResolverObject != nullptr && sRemoveMethod != nullptr, CHIP_ERROR_INCORRECT_STATE); + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); + + jboolean jret = env->CallBooleanMethod(sResolverObject, sRemoveMethod); + + if (env->ExceptionCheck()) + { + ChipLogError(Discovery, "Java exception in ChipDnssdRemoveServices"); + env->ExceptionDescribe(); + env->ExceptionClear(); + return CHIP_JNI_ERROR_EXCEPTION_THROWN; + } + + if(jret) { + return CHIP_NO_ERROR; + } else { + return CHIP_JNI_ERROR_JAVA_ERROR; + } } CHIP_ERROR ChipDnssdPublishService(const DnssdService * service) { - return CHIP_ERROR_NOT_IMPLEMENTED; + VerifyOrReturnError(service != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(sResolverObject != nullptr && sPublishMethod != nullptr, CHIP_ERROR_INCORRECT_STATE); + + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); + UtfString jniName(env, service->mName); + UtfString jniHostName(env, service->mHostName); + + std::string serviceType = service->mType; + serviceType += '.'; + serviceType += (service->mProtocol == DnssdServiceProtocol::kDnssdProtocolUdp ? "_udp" : "_tcp"); + UtfString jniServiceType(env, serviceType.c_str()); + + jclass stringClass = env->FindClass("java/lang/String"); + jobjectArray keys = env->NewObjectArray(service->mTextEntrySize, stringClass, nullptr); + + jclass arrayElemType = env->FindClass("[B"); + jobjectArray datas = env->NewObjectArray(service->mTextEntrySize, arrayElemType, nullptr); + + for(size_t i=0;i < service->mTextEntrySize;i++) { + UtfString jniKey(env, service->mTextEntries[i].mKey); + env->SetObjectArrayElement(keys,i,jniKey.jniValue()); + + ByteArray jniData(env, (const jbyte *)service->mTextEntries[i].mData, service->mTextEntries[i].mDataSize); + env->SetObjectArrayElement(datas,i,jniData.jniValue()); + } + + jobjectArray subTypes = env->NewObjectArray(service->mSubTypeSize, stringClass, nullptr); + for(size_t i=0; i< service->mSubTypeSize; i++) { + UtfString jniSubType(env, service->mSubTypes[i]); + env->SetObjectArrayElement(subTypes,i,jniSubType.jniValue()); + } + + jboolean jret = env->CallBooleanMethod(sResolverObject, sPublishMethod, jniName.jniValue(), jniHostName.jniValue() + , jniServiceType.jniValue(), service->mPort, keys, datas, subTypes); + + env->DeleteGlobalRef(keys); + env->DeleteGlobalRef(datas); + env->DeleteGlobalRef(subTypes); + + if (env->ExceptionCheck()) + { + ChipLogError(Discovery, "Java exception in ChipDnssdPublishService"); + env->ExceptionDescribe(); + env->ExceptionClear(); + return CHIP_JNI_ERROR_EXCEPTION_THROWN; + } + + if(jret) { + return CHIP_NO_ERROR; + } else { + return CHIP_JNI_ERROR_JAVA_ERROR; + } } CHIP_ERROR ChipDnssdFinalizeServiceUpdate() { - return CHIP_ERROR_NOT_IMPLEMENTED; + return CHIP_NO_ERROR; } CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Inet::IPAddressType addressType, @@ -120,12 +191,25 @@ void InitializeWithObjects(jobject resolverObject, jobject mdnsCallbackObject) sResolveMethod = env->GetMethodID(resolverClass, "resolve", "(Ljava/lang/String;Ljava/lang/String;JJLchip/platform/ChipMdnsCallback;)V"); - if (sResolveMethod == nullptr) { ChipLogError(Discovery, "Failed to access Resolver 'resolve' method"); env->ExceptionClear(); } + + sPublishMethod = env->GetMethodID(resolverClass, "publish", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I[Ljava/lang/String;[[B[Ljava/lang/String;)Z"); + if (sPublishMethod == nullptr) + { + ChipLogError(Discovery, "Failed to access Resolver 'publish' method"); + env->ExceptionClear(); + } + + sRemoveMethod = env->GetMethodID(resolverClass, "remove", "()Z"); + if (sRemoveMethod == nullptr) + { + ChipLogError(Discovery, "Failed to access Resolver 'remove' method"); + env->ExceptionClear(); + } } void HandleResolve(jstring instanceName, jstring serviceType, jstring address, jint port, jlong callbackHandle, jlong contextHandle) diff --git a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java index 76adab3cb3915b..177ff6ed444da1 100644 --- a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java +++ b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java @@ -112,4 +112,14 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) { }); mainThreadHandler.postDelayed(timeoutRunnable, RESOLVE_SERVICE_TIMEOUT); } + + @Override + public boolean publish(String serviceName, String hostName, String type, int port, String[] textEntriesKeys, byte[][] textEntriesDatas, String[] subTypes) { + return false; + } + + @Override + public boolean remove() { + return false; + } } diff --git a/src/platform/android/java/chip/platform/ServiceResolver.java b/src/platform/android/java/chip/platform/ServiceResolver.java index e9cf638b005929..4c2154176f22e6 100644 --- a/src/platform/android/java/chip/platform/ServiceResolver.java +++ b/src/platform/android/java/chip/platform/ServiceResolver.java @@ -30,4 +30,26 @@ void resolve( long callbackHandle, long contextHandle, ChipMdnsCallback chipMdnsCallback); + + /** + * Publishes a service via DNS-SD. + * + * Calling the function again with the same service name, type, protocol, + * interface and port but different text will update the text published. + * This function will NOT take the ownership of service->mTextEntries memory. + */ + boolean publish( + String serviceName, + String hostName, + String type, + int port, + String[] textEntriesKeys, + byte[][] textEntriesDatas, + String[] subTypes + ); + + /** + * Removes or marks all services being advertised for removal. + */ + boolean remove(); } From f36e0e604496370dfba95774428ede408cb80ac2 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Tue, 26 Oct 2021 20:16:06 +0800 Subject: [PATCH 2/6] fix CHIPTest AS build error --- src/android/CHIPTest/gradle.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/android/CHIPTest/gradle.properties b/src/android/CHIPTest/gradle.properties index d5dcc9461e28f2..3026069c70e98f 100644 --- a/src/android/CHIPTest/gradle.properties +++ b/src/android/CHIPTest/gradle.properties @@ -21,10 +21,10 @@ kotlin.code.style=official # Build SDK from source code and debug in Android Stduio. Must also set matterBuildSrcDir. matterSdkSourceBuild=false -# Point to the SDK build dir without quotes (../../../../out/android-arm64-chip-test for +# Point to the SDK build dir without quotes (out/android-arm64-chip-test for # example) to build SDK from source code and debug in Android Studio. # Set to blank to use the SDK prebuilt by scripts/build/build_examples.py. -matterBuildSrcDir=../../../../out/android-arm64-chip-test +matterBuildSrcDir=out/android-arm64-chip-test # Test libs to run matterUTestLib=libPlatformTests.a From 88c746d3eb6a391680e10e27c15027873a98b4dd Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Tue, 26 Oct 2021 20:16:50 +0800 Subject: [PATCH 3/6] fix crash issue in dnssdimpl, DeleteRef error --- src/platform/android/DnssdImpl.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/platform/android/DnssdImpl.cpp b/src/platform/android/DnssdImpl.cpp index b5893760affa44..ccfb3ce178c8a4 100644 --- a/src/platform/android/DnssdImpl.cpp +++ b/src/platform/android/DnssdImpl.cpp @@ -119,9 +119,9 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service) jboolean jret = env->CallBooleanMethod(sResolverObject, sPublishMethod, jniName.jniValue(), jniHostName.jniValue() , jniServiceType.jniValue(), service->mPort, keys, datas, subTypes); - env->DeleteGlobalRef(keys); - env->DeleteGlobalRef(datas); - env->DeleteGlobalRef(subTypes); + env->DeleteLocalRef(keys); + env->DeleteLocalRef(datas); + env->DeleteLocalRef(subTypes); if (env->ExceptionCheck()) { From dec7cb4d5622b573b8f3d4061d346d59ae0ad970 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Tue, 26 Oct 2021 20:17:22 +0800 Subject: [PATCH 4/6] added java imp for dnssd --- .../platform/NsdManagerServiceResolver.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java index 177ff6ed444da1..0757b65fcbed26 100644 --- a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java +++ b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java @@ -26,12 +26,15 @@ import android.os.Looper; import android.util.Log; +import java.util.*; + public class NsdManagerServiceResolver implements ServiceResolver { private static final String TAG = NsdManagerServiceResolver.class.getSimpleName(); private static final long RESOLVE_SERVICE_TIMEOUT = 30000; private final NsdManager nsdManager; private MulticastLock multicastLock; private Handler mainThreadHandler; + private List mRegistrationListeners = new ArrayList<>(); public NsdManagerServiceResolver(Context context) { this.nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE); @@ -115,11 +118,47 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) { @Override public boolean publish(String serviceName, String hostName, String type, int port, String[] textEntriesKeys, byte[][] textEntriesDatas, String[] subTypes) { - return false; + NsdServiceInfo serviceInfo = new NsdServiceInfo(); + serviceInfo.setServiceName(serviceName); + serviceInfo.setServiceType(type); + serviceInfo.setPort(port); + Log.i(TAG,"publish serviceName="+serviceName+" type="+type+" port="+port); + + NsdManager.RegistrationListener registrationListener = new NsdManager.RegistrationListener() { + @Override + public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { + Log.w(TAG, "service " + serviceInfo.getServiceName() + " onRegistrationFailed:" + errorCode); + } + + @Override + public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { + Log.w(TAG, "service " + serviceInfo.getServiceName() + " onUnregistrationFailed:" + errorCode); + } + + @Override + public void onServiceRegistered(NsdServiceInfo serviceInfo) { + Log.i(TAG, "service " + serviceInfo.getServiceName() + " onServiceRegistered:"); + } + + @Override + public void onServiceUnregistered(NsdServiceInfo serviceInfo) { + Log.i(TAG, "service " + serviceInfo.getServiceName() + " onServiceRegistered:"); + } + }; + mRegistrationListeners.add(registrationListener); + + nsdManager.registerService( + serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener); + + return true; } @Override public boolean remove() { - return false; + for (NsdManager.RegistrationListener l:mRegistrationListeners) { + nsdManager.unregisterService(l); + } + + return true; } } From 8a0a61506954d218181102007401bbca96ada100 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Tue, 26 Oct 2021 20:36:27 +0800 Subject: [PATCH 5/6] fix restyled errors --- src/platform/android/DnssdImpl.cpp | 42 +++++---- .../platform/NsdManagerServiceResolver.java | 86 +++++++++++-------- .../java/chip/platform/ServiceResolver.java | 13 ++- 3 files changed, 79 insertions(+), 62 deletions(-) diff --git a/src/platform/android/DnssdImpl.cpp b/src/platform/android/DnssdImpl.cpp index ccfb3ce178c8a4..f17cb32a188642 100644 --- a/src/platform/android/DnssdImpl.cpp +++ b/src/platform/android/DnssdImpl.cpp @@ -41,7 +41,7 @@ jobject sResolverObject = nullptr; jobject sMdnsCallbackObject = nullptr; jmethodID sResolveMethod = nullptr; jmethodID sPublishMethod = nullptr; -jmethodID sRemoveMethod = nullptr; +jmethodID sRemoveMethod = nullptr; } // namespace // Implemention of functions declared in lib/dnssd/platform/Dnssd.h @@ -75,9 +75,12 @@ CHIP_ERROR ChipDnssdRemoveServices() return CHIP_JNI_ERROR_EXCEPTION_THROWN; } - if(jret) { + if (jret) + { return CHIP_NO_ERROR; - } else { + } + else + { return CHIP_JNI_ERROR_JAVA_ERROR; } } @@ -97,27 +100,29 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service) UtfString jniServiceType(env, serviceType.c_str()); jclass stringClass = env->FindClass("java/lang/String"); - jobjectArray keys = env->NewObjectArray(service->mTextEntrySize, stringClass, nullptr); + jobjectArray keys = env->NewObjectArray(service->mTextEntrySize, stringClass, nullptr); jclass arrayElemType = env->FindClass("[B"); - jobjectArray datas = env->NewObjectArray(service->mTextEntrySize, arrayElemType, nullptr); + jobjectArray datas = env->NewObjectArray(service->mTextEntrySize, arrayElemType, nullptr); - for(size_t i=0;i < service->mTextEntrySize;i++) { + for (size_t i = 0; i < service->mTextEntrySize; i++) + { UtfString jniKey(env, service->mTextEntries[i].mKey); - env->SetObjectArrayElement(keys,i,jniKey.jniValue()); + env->SetObjectArrayElement(keys, i, jniKey.jniValue()); - ByteArray jniData(env, (const jbyte *)service->mTextEntries[i].mData, service->mTextEntries[i].mDataSize); - env->SetObjectArrayElement(datas,i,jniData.jniValue()); + ByteArray jniData(env, (const jbyte *) service->mTextEntries[i].mData, service->mTextEntries[i].mDataSize); + env->SetObjectArrayElement(datas, i, jniData.jniValue()); } jobjectArray subTypes = env->NewObjectArray(service->mSubTypeSize, stringClass, nullptr); - for(size_t i=0; i< service->mSubTypeSize; i++) { + for (size_t i = 0; i < service->mSubTypeSize; i++) + { UtfString jniSubType(env, service->mSubTypes[i]); - env->SetObjectArrayElement(subTypes,i,jniSubType.jniValue()); + env->SetObjectArrayElement(subTypes, i, jniSubType.jniValue()); } - jboolean jret = env->CallBooleanMethod(sResolverObject, sPublishMethod, jniName.jniValue(), jniHostName.jniValue() - , jniServiceType.jniValue(), service->mPort, keys, datas, subTypes); + jboolean jret = env->CallBooleanMethod(sResolverObject, sPublishMethod, jniName.jniValue(), jniHostName.jniValue(), + jniServiceType.jniValue(), service->mPort, keys, datas, subTypes); env->DeleteLocalRef(keys); env->DeleteLocalRef(datas); @@ -131,9 +136,12 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service) return CHIP_JNI_ERROR_EXCEPTION_THROWN; } - if(jret) { + if (jret) + { return CHIP_NO_ERROR; - } else { + } + else + { return CHIP_JNI_ERROR_JAVA_ERROR; } } @@ -197,7 +205,9 @@ void InitializeWithObjects(jobject resolverObject, jobject mdnsCallbackObject) env->ExceptionClear(); } - sPublishMethod = env->GetMethodID(resolverClass, "publish", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I[Ljava/lang/String;[[B[Ljava/lang/String;)Z"); + sPublishMethod = + env->GetMethodID(resolverClass, "publish", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I[Ljava/lang/String;[[B[Ljava/lang/String;)Z"); if (sPublishMethod == nullptr) { ChipLogError(Discovery, "Failed to access Resolver 'publish' method"); diff --git a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java index 0757b65fcbed26..3a84a3edd653d3 100644 --- a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java +++ b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java @@ -25,7 +25,6 @@ import android.os.Handler; import android.os.Looper; import android.util.Log; - import java.util.*; public class NsdManagerServiceResolver implements ServiceResolver { @@ -116,49 +115,60 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) { mainThreadHandler.postDelayed(timeoutRunnable, RESOLVE_SERVICE_TIMEOUT); } - @Override - public boolean publish(String serviceName, String hostName, String type, int port, String[] textEntriesKeys, byte[][] textEntriesDatas, String[] subTypes) { - NsdServiceInfo serviceInfo = new NsdServiceInfo(); - serviceInfo.setServiceName(serviceName); - serviceInfo.setServiceType(type); - serviceInfo.setPort(port); - Log.i(TAG,"publish serviceName="+serviceName+" type="+type+" port="+port); - - NsdManager.RegistrationListener registrationListener = new NsdManager.RegistrationListener() { - @Override - public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { - Log.w(TAG, "service " + serviceInfo.getServiceName() + " onRegistrationFailed:" + errorCode); - } + @Override + public boolean publish( + String serviceName, + String hostName, + String type, + int port, + String[] textEntriesKeys, + byte[][] textEntriesDatas, + String[] subTypes) { + NsdServiceInfo serviceInfo = new NsdServiceInfo(); + serviceInfo.setServiceName(serviceName); + serviceInfo.setServiceType(type); + serviceInfo.setPort(port); + Log.i(TAG, "publish serviceName=" + serviceName + " type=" + type + " port=" + port); - @Override - public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { - Log.w(TAG, "service " + serviceInfo.getServiceName() + " onUnregistrationFailed:" + errorCode); - } + NsdManager.RegistrationListener registrationListener = + new NsdManager.RegistrationListener() { + @Override + public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { + Log.w( + TAG, + "service " + serviceInfo.getServiceName() + " onRegistrationFailed:" + errorCode); + } - @Override - public void onServiceRegistered(NsdServiceInfo serviceInfo) { - Log.i(TAG, "service " + serviceInfo.getServiceName() + " onServiceRegistered:"); - } + @Override + public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { + Log.w( + TAG, + "service " + serviceInfo.getServiceName() + " onUnregistrationFailed:" + errorCode); + } - @Override - public void onServiceUnregistered(NsdServiceInfo serviceInfo) { - Log.i(TAG, "service " + serviceInfo.getServiceName() + " onServiceRegistered:"); - } - }; - mRegistrationListeners.add(registrationListener); + @Override + public void onServiceRegistered(NsdServiceInfo serviceInfo) { + Log.i(TAG, "service " + serviceInfo.getServiceName() + " onServiceRegistered:"); + } - nsdManager.registerService( - serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener); + @Override + public void onServiceUnregistered(NsdServiceInfo serviceInfo) { + Log.i(TAG, "service " + serviceInfo.getServiceName() + " onServiceRegistered:"); + } + }; + mRegistrationListeners.add(registrationListener); - return true; - } + nsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener); - @Override - public boolean remove() { - for (NsdManager.RegistrationListener l:mRegistrationListeners) { - nsdManager.unregisterService(l); - } + return true; + } - return true; + @Override + public boolean remove() { + for (NsdManager.RegistrationListener l : mRegistrationListeners) { + nsdManager.unregisterService(l); } + + return true; + } } diff --git a/src/platform/android/java/chip/platform/ServiceResolver.java b/src/platform/android/java/chip/platform/ServiceResolver.java index 4c2154176f22e6..5a34cc8ad8f57d 100644 --- a/src/platform/android/java/chip/platform/ServiceResolver.java +++ b/src/platform/android/java/chip/platform/ServiceResolver.java @@ -34,9 +34,9 @@ void resolve( /** * Publishes a service via DNS-SD. * - * Calling the function again with the same service name, type, protocol, - * interface and port but different text will update the text published. - * This function will NOT take the ownership of service->mTextEntries memory. + *

Calling the function again with the same service name, type, protocol, interface and port + * but different text will update the text published. This function will NOT take the ownership of + * service->mTextEntries memory. */ boolean publish( String serviceName, @@ -45,11 +45,8 @@ boolean publish( int port, String[] textEntriesKeys, byte[][] textEntriesDatas, - String[] subTypes - ); + String[] subTypes); - /** - * Removes or marks all services being advertised for removal. - */ + /** Removes or marks all services being advertised for removal. */ boolean remove(); } From c830217c0242d2a41a33238b7e73d645b10a9f93 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Wed, 27 Oct 2021 11:33:20 +0800 Subject: [PATCH 6/6] fix review issues: 1. publish and remove return void 2. remove -> removeServices 3. import for detail packages 4. naming consistent 5. remove DeleteLocalRef --- src/platform/android/DnssdImpl.cpp | 48 ++++++------------- .../platform/NsdManagerServiceResolver.java | 17 +++---- .../java/chip/platform/ServiceResolver.java | 7 ++- 3 files changed, 25 insertions(+), 47 deletions(-) diff --git a/src/platform/android/DnssdImpl.cpp b/src/platform/android/DnssdImpl.cpp index f17cb32a188642..8d5b5c4e2c81d2 100644 --- a/src/platform/android/DnssdImpl.cpp +++ b/src/platform/android/DnssdImpl.cpp @@ -37,11 +37,11 @@ namespace Dnssd { using namespace chip::Platform; namespace { -jobject sResolverObject = nullptr; -jobject sMdnsCallbackObject = nullptr; -jmethodID sResolveMethod = nullptr; -jmethodID sPublishMethod = nullptr; -jmethodID sRemoveMethod = nullptr; +jobject sResolverObject = nullptr; +jobject sMdnsCallbackObject = nullptr; +jmethodID sResolveMethod = nullptr; +jmethodID sPublishMethod = nullptr; +jmethodID sRemoveServicesMethod = nullptr; } // namespace // Implemention of functions declared in lib/dnssd/platform/Dnssd.h @@ -62,10 +62,10 @@ CHIP_ERROR ChipDnssdShutdown() CHIP_ERROR ChipDnssdRemoveServices() { - VerifyOrReturnError(sResolverObject != nullptr && sRemoveMethod != nullptr, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(sResolverObject != nullptr && sRemoveServicesMethod != nullptr, CHIP_ERROR_INCORRECT_STATE); JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - jboolean jret = env->CallBooleanMethod(sResolverObject, sRemoveMethod); + env->CallVoidMethod(sResolverObject, sRemoveServicesMethod); if (env->ExceptionCheck()) { @@ -75,14 +75,7 @@ CHIP_ERROR ChipDnssdRemoveServices() return CHIP_JNI_ERROR_EXCEPTION_THROWN; } - if (jret) - { - return CHIP_NO_ERROR; - } - else - { - return CHIP_JNI_ERROR_JAVA_ERROR; - } + return CHIP_NO_ERROR; } CHIP_ERROR ChipDnssdPublishService(const DnssdService * service) @@ -121,12 +114,8 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service) env->SetObjectArrayElement(subTypes, i, jniSubType.jniValue()); } - jboolean jret = env->CallBooleanMethod(sResolverObject, sPublishMethod, jniName.jniValue(), jniHostName.jniValue(), - jniServiceType.jniValue(), service->mPort, keys, datas, subTypes); - - env->DeleteLocalRef(keys); - env->DeleteLocalRef(datas); - env->DeleteLocalRef(subTypes); + env->CallVoidMethod(sResolverObject, sPublishMethod, jniName.jniValue(), jniHostName.jniValue(), jniServiceType.jniValue(), + service->mPort, keys, datas, subTypes); if (env->ExceptionCheck()) { @@ -136,14 +125,7 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service) return CHIP_JNI_ERROR_EXCEPTION_THROWN; } - if (jret) - { - return CHIP_NO_ERROR; - } - else - { - return CHIP_JNI_ERROR_JAVA_ERROR; - } + return CHIP_NO_ERROR; } CHIP_ERROR ChipDnssdFinalizeServiceUpdate() @@ -207,17 +189,17 @@ void InitializeWithObjects(jobject resolverObject, jobject mdnsCallbackObject) sPublishMethod = env->GetMethodID(resolverClass, "publish", - "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I[Ljava/lang/String;[[B[Ljava/lang/String;)Z"); + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I[Ljava/lang/String;[[B[Ljava/lang/String;)V"); if (sPublishMethod == nullptr) { ChipLogError(Discovery, "Failed to access Resolver 'publish' method"); env->ExceptionClear(); } - sRemoveMethod = env->GetMethodID(resolverClass, "remove", "()Z"); - if (sRemoveMethod == nullptr) + sRemoveServicesMethod = env->GetMethodID(resolverClass, "removeServices", "()V"); + if (sRemoveServicesMethod == nullptr) { - ChipLogError(Discovery, "Failed to access Resolver 'remove' method"); + ChipLogError(Discovery, "Failed to access Resolver 'removeServices' method"); env->ExceptionClear(); } } diff --git a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java index 3a84a3edd653d3..b08686580fac02 100644 --- a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java +++ b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java @@ -25,7 +25,8 @@ import android.os.Handler; import android.os.Looper; import android.util.Log; -import java.util.*; +import java.util.ArrayList; +import java.util.List; public class NsdManagerServiceResolver implements ServiceResolver { private static final String TAG = NsdManagerServiceResolver.class.getSimpleName(); @@ -33,7 +34,7 @@ public class NsdManagerServiceResolver implements ServiceResolver { private final NsdManager nsdManager; private MulticastLock multicastLock; private Handler mainThreadHandler; - private List mRegistrationListeners = new ArrayList<>(); + private List registrationListeners = new ArrayList<>(); public NsdManagerServiceResolver(Context context) { this.nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE); @@ -116,7 +117,7 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) { } @Override - public boolean publish( + public void publish( String serviceName, String hostName, String type, @@ -156,19 +157,15 @@ public void onServiceUnregistered(NsdServiceInfo serviceInfo) { Log.i(TAG, "service " + serviceInfo.getServiceName() + " onServiceRegistered:"); } }; - mRegistrationListeners.add(registrationListener); + registrationListeners.add(registrationListener); nsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener); - - return true; } @Override - public boolean remove() { - for (NsdManager.RegistrationListener l : mRegistrationListeners) { + public void removeServices() { + for (NsdManager.RegistrationListener l : registrationListeners) { nsdManager.unregisterService(l); } - - return true; } } diff --git a/src/platform/android/java/chip/platform/ServiceResolver.java b/src/platform/android/java/chip/platform/ServiceResolver.java index 5a34cc8ad8f57d..f8a50c6e37f0ec 100644 --- a/src/platform/android/java/chip/platform/ServiceResolver.java +++ b/src/platform/android/java/chip/platform/ServiceResolver.java @@ -35,10 +35,9 @@ void resolve( * Publishes a service via DNS-SD. * *

Calling the function again with the same service name, type, protocol, interface and port - * but different text will update the text published. This function will NOT take the ownership of - * service->mTextEntries memory. + * but different text will update the text published. */ - boolean publish( + void publish( String serviceName, String hostName, String type, @@ -48,5 +47,5 @@ boolean publish( String[] subTypes); /** Removes or marks all services being advertised for removal. */ - boolean remove(); + void removeServices(); }