From 044af2c506deecbaae214e211239e44335504b09 Mon Sep 17 00:00:00 2001 From: Joonhaeng Heo Date: Thu, 23 May 2024 15:58:43 +0900 Subject: [PATCH] Add Compare IPAddress in discoveredNode --- .../AbstractDnssdDiscoveryController.cpp | 28 +++++++- .../platform/NsdManagerServiceBrowser.java | 4 +- .../platform/NsdServiceFinderAndResolver.java | 68 +++++++++++-------- 3 files changed, 66 insertions(+), 34 deletions(-) diff --git a/src/controller/AbstractDnssdDiscoveryController.cpp b/src/controller/AbstractDnssdDiscoveryController.cpp index 3d3ca4c8097955..8b5f2c58c630a1 100644 --- a/src/controller/AbstractDnssdDiscoveryController.cpp +++ b/src/controller/AbstractDnssdDiscoveryController.cpp @@ -25,6 +25,30 @@ namespace chip { namespace Controller { +bool compareIpAddresses(const size_t sourceNumIPs, const size_t destinationNumIPs, const Inet::IPAddress *source, const Inet::IPAddress *destination) +{ + size_t sameIpAddress = 0; + bool addressUsed[chip::Dnssd::CommonResolutionData::kMaxIPAddresses] = {false}; + if (sourceNumIPs != destinationNumIPs) + { + return false; + } + + for (size_t s = 0 ; s < sourceNumIPs ; s++) + { + for (size_t d = 0 ; d < destinationNumIPs ; d++) + { + if (!addressUsed[d] && source[d] == destination[d]) + { + addressUsed[d] = true; + sameIpAddress++; + break; + } + } + } + return sameIpAddress == destinationNumIPs; +} + void AbstractDnssdDiscoveryController::OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & discNodeData) { VerifyOrReturn(discNodeData.Is()); @@ -38,10 +62,8 @@ void AbstractDnssdDiscoveryController::OnNodeDiscovered(const chip::Dnssd::Disco { continue; } - // TODO(#32576) Check if IP address are the same. Must account for `numIPs` in the list of `ipAddress`. - // Additionally, must NOT assume that the ordering is consistent. if (strcmp(discoveredNode.hostName, nodeData.hostName) == 0 && discoveredNode.port == nodeData.port && - discoveredNode.numIPs == nodeData.numIPs) + compareIpAddresses(discoveredNode.numIPs, nodeData.numIPs, discoveredNode.ipAddress, nodeData.ipAddress)) { discoveredNode = nodeData; if (mDeviceDiscoveryDelegate != nullptr) diff --git a/src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java b/src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java index 28fc6988a9dbbd..5b015c33176ed6 100644 --- a/src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java +++ b/src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java @@ -172,7 +172,9 @@ public void onStopDiscoveryFailed(String serviceType, int errorCode) { @Override public void onDiscoveryStopped(String serviceType) { Log.w(TAG, "Successfully stopped discovery service '" + serviceType); - this.handleServiceBrowse(chipMdnsCallback); + new Handler(Looper.getMainLooper()).post(() -> { + this.handleServiceBrowse(chipMdnsCallback); + }); } public void handleServiceBrowse(ChipMdnsCallback chipMdnsCallback) { diff --git a/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java b/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java index 70ffdbc0102a62..0679eaa361320a 100644 --- a/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java +++ b/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java @@ -21,6 +21,8 @@ import android.net.nsd.NsdManager; import android.net.nsd.NsdServiceInfo; import android.net.wifi.WifiManager.MulticastLock; +import android.os.Handler; +import android.os.Looper; import android.util.Log; import androidx.annotation.Nullable; import java.util.concurrent.Executors; @@ -118,21 +120,22 @@ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { Log.w( TAG, "Failed to resolve service '" + serviceInfo.getServiceName() + "': " + errorCode); - chipMdnsCallback.handleServiceResolve( - serviceInfo.getServiceName(), - // Use the target service info since the resolved service info sometimes appends a - // "." at the front likely because it is trying to strip the service name out of it - // and something is missed. - // The target service info service type should be effectively the same as the - // resolved service info. - NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(), - null, - null, - 0, - null, - callbackHandle, - contextHandle); - + new Handler(Looper.getMainLooper()).post(() -> { + chipMdnsCallback.handleServiceResolve( + serviceInfo.getServiceName(), + // Use the target service info since the resolved service info sometimes appends a + // "." at the front likely because it is trying to strip the service name out of it + // and something is missed. + // The target service info service type should be effectively the same as the + // resolved service info. + NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(), + null, + null, + 0, + null, + callbackHandle, + contextHandle); + }); if (multicastLock.isHeld()) { multicastLock.release(); @@ -153,22 +156,27 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) { + serviceInfo.getHost() + ", type : " + serviceInfo.getServiceType()); - // TODO: Find out if DNS-SD results for Android should contain interface ID - chipMdnsCallback.handleServiceResolve( - serviceInfo.getServiceName(), - // Use the target service info since the resolved service info sometimes appends a - // "." at the front likely because it is trying to strip the service name out of it - // and something is missed. - // The target service info service type should be effectively the same as the - // resolved service info. - NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(), - serviceInfo.getHost().getHostName(), - serviceInfo.getHost().getHostAddress(), - serviceInfo.getPort(), - serviceInfo.getAttributes(), - callbackHandle, - contextHandle); + final String hostName = serviceInfo.getHost().getHostName(); + final String address = serviceInfo.getHost().getHostAddress(); + final int port = serviceInfo.getPort(); + new Handler(Looper.getMainLooper()).post(() -> { + // TODO: Find out if DNS-SD results for Android should contain interface ID + chipMdnsCallback.handleServiceResolve( + serviceInfo.getServiceName(), + // Use the target service info since the resolved service info sometimes appends a + // "." at the front likely because it is trying to strip the service name out of it + // and something is missed. + // The target service info service type should be effectively the same as the + // resolved service info. + NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(), + hostName, + address, + port, + serviceInfo.getAttributes(), + callbackHandle, + contextHandle); + }); if (multicastLock.isHeld()) { multicastLock.release();