Skip to content

Commit

Permalink
Addressed comments by bzbarsky-apple now using browseIdentifier to st…
Browse files Browse the repository at this point in the history
…op DNS-SD browse
  • Loading branch information
pgregorr-amazon committed Apr 3, 2024
1 parent 557a1ea commit ba9073d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/lib/dnssd/platform/Dnssd.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi
*
* @param browseIdentifier an identifier for a browse operation that came from
* ChipDnssdBrowse.
*
*/
CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier);

Expand Down
11 changes: 8 additions & 3 deletions src/platform/android/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,20 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine
return CHIP_JNI_ERROR_EXCEPTION_THROWN;
}

*browseIdentifier = reinterpret_cast<intptr_t>(nullptr);
auto sdCtx = chip::Platform::New<BrowseContext>(callback);
*browseIdentifier = reinterpret_cast<intptr_t>(sdCtx);

return CHIP_NO_ERROR;
}

CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
{
VerifyOrReturnError(sMdnsCallbackObject.HasValidObjectRef(), CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(sBrowserObject.HasValidObjectRef() && sStopBrowseMethod != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
auto ctx = reinterpret_cast<BrowseContext *>(browseIdentifier);

env->CallVoidMethod(sBrowserObject.ObjectRef(), sStopBrowseMethod, sMdnsCallbackObject.ObjectRef());
env->CallVoidMethod(sBrowserObject.ObjectRef(), sStopBrowseMethod, reinterpret_cast<jlong>(ctx->callback));

if (env->ExceptionCheck())
{
Expand All @@ -223,6 +226,8 @@ CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
env->ExceptionClear();
return CHIP_JNI_ERROR_EXCEPTION_THROWN;
}
chip::Platform::Delete(ctx);

return CHIP_NO_ERROR;
}

Expand Down
8 changes: 8 additions & 0 deletions src/platform/android/DnssdImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <jni.h>
#include <lib/dnssd/platform/Dnssd.h>

namespace chip {
namespace Dnssd {
Expand All @@ -37,5 +38,12 @@ void HandleResolve(jstring instanceName, jstring serviceType, jstring hostName,

void HandleBrowse(jobjectArray instanceName, jstring serviceType, jlong callbackHandle, jlong contextHandle);

struct BrowseContext
{
DnssdBrowseCallback callback;

BrowseContext(DnssdBrowseCallback cb) { callback = cb; };
};

} // namespace Dnssd
} // namespace chip
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public void browse(
new Runnable() {
@Override
public void run() {
Log.i(
TAG,
"Browse for service '"
+ serviceType
+ "' expired after timeout: "
+ timeout
+ " ms");
stopDiscover(callbackHandle);
}
};
Expand All @@ -82,7 +89,7 @@ public void startDiscover(
final long contextHandle,
final ChipMdnsCallback chipMdnsCallback) {
if (callbackMap.containsKey(callbackHandle)) {
Log.d(TAG, "Invalid callbackHandle");
Log.w(TAG, "Starting service discovering failed. Invalid callback ID: " + callbackHandle);
return;
}

Expand All @@ -91,28 +98,35 @@ public void startDiscover(
multicastLock.acquire();
discovery.setChipMdnsCallback(chipMdnsCallback);

Log.d(TAG, "Starting service discovering for '" + serviceType + "'");
Log.d(
TAG,
"Starting service discovering for '"
+ serviceType
+ "' with callback ID: "
+ callbackHandle);

this.nsdManager.discoverServices(serviceType, NsdManager.PROTOCOL_DNS_SD, discovery);
callbackMap.put(callbackHandle, discovery);
}

public void stopDiscover(final long callbackHandle) {
Log.d(TAG, "Stopping service discovering with callback ID: " + callbackHandle);
if (!callbackMap.containsKey(callbackHandle)) {
Log.w(TAG, "Stopping service discovering failed. Callback handle not found.");
return;
}

MessageQueue queue = mainThreadHandler.getLooper().getQueue();
if (!queue.isIdle()) {
Log.i(TAG, "canceling scheduled browse timeout runnable");
mainThreadHandler.removeCallbacksAndMessages(null);
}

NsdManagerDiscovery discovery = callbackMap.remove(callbackHandle);
if (multicastLock.isHeld()) {
multicastLock.release();
}

MessageQueue queue = mainThreadHandler.getLooper().getQueue();
if (!queue.isIdle()) {
Log.d(TAG, "Canceling scheduled browse timeout runnable for '" + discovery.serviceType + "'");
mainThreadHandler.removeCallbacksAndMessages(null);
}

this.nsdManager.stopServiceDiscovery(discovery);
}

Expand Down

0 comments on commit ba9073d

Please sign in to comment.