Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Synchronizing on the use of NsdManager.resolveService() #23515

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class NsdManagerServiceResolver implements ServiceResolver {
private static final String TAG = NsdManagerServiceResolver.class.getSimpleName();
Expand All @@ -37,15 +40,27 @@ public class NsdManagerServiceResolver implements ServiceResolver {
private Handler mainThreadHandler;
private List<NsdManager.RegistrationListener> registrationListeners = new ArrayList<>();
private final CopyOnWriteArrayList<String> mMFServiceName = new CopyOnWriteArrayList<>();
private final NsdManagerResolverAvailState nsdManagerResolverAvailState;
sharadb-amazon marked this conversation as resolved.
Show resolved Hide resolved

public NsdManagerServiceResolver(Context context) {
/**
* @param context application context
* @param nsdManagerResolverAvailState Passing NsdManagerResolverAvailState allows
* NsdManagerServiceResolver to synchronize on the usage of NsdManager's resolveService() API
*/
public NsdManagerServiceResolver(
Context context, NsdManagerResolverAvailState nsdManagerResolverAvailState) {
this.nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
this.mainThreadHandler = new Handler(Looper.getMainLooper());

this.multicastLock =
((WifiManager) context.getSystemService(Context.WIFI_SERVICE))
.createMulticastLock("chipMulticastLock");
this.multicastLock.setReferenceCounted(true);
this.nsdManagerResolverAvailState = nsdManagerResolverAvailState;
}

public NsdManagerServiceResolver(Context context) {
this(context, null);
}

@Override
Expand Down Expand Up @@ -78,10 +93,18 @@ public void run() {
Log.d(TAG, "resolve: Timing out");
if (multicastLock.isHeld()) {
multicastLock.release();

if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.signalFree();
}
}
}
};

if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.markBusyIfFreeOrWait();
}

this.nsdManager.resolveService(
serviceInfo,
new NsdManager.ResolveListener() {
Expand All @@ -95,6 +118,10 @@ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {

if (multicastLock.isHeld()) {
multicastLock.release();

if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.signalFree();
}
}
mainThreadHandler.removeCallbacks(timeoutRunnable);
}
Expand All @@ -120,10 +147,15 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) {

if (multicastLock.isHeld()) {
multicastLock.release();

if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.signalFree();
}
}
mainThreadHandler.removeCallbacks(timeoutRunnable);
}
});

mainThreadHandler.postDelayed(timeoutRunnable, RESOLVE_SERVICE_TIMEOUT);
}

Expand Down Expand Up @@ -223,4 +255,47 @@ public void removeServices() {
registrationListeners.clear();
mMFServiceName.clear();
}

/**
* The Android NsdManager calls back on the NsdManager.ResolveListener with a
* FAILURE_ALREADY_ACTIVE(3) if any application code calls resolveService() on it while the
* resolve operation is already active (from another call made previously). An object of
* NsdManagerResolverAvailState allows NsdManagerServiceResolver to synchronize on the usage of
* NsdManager's resolveService() API
*/
public static class NsdManagerResolverAvailState {
private static final String TAG = NsdManagerResolverAvailState.class.getSimpleName();

private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private boolean busy = false;

/**
* Waits if the NsdManager is already busy with resolving a service. Otherwise, it marks it as
* busy and returns
*/
public void markBusyIfFreeOrWait() {
sharadb-amazon marked this conversation as resolved.
Show resolved Hide resolved
lock.lock();
try {
while (busy) {
Log.d(TAG, "Found NsdManager Resolver busy, waiting");
condition.await();
}
Log.d(TAG, "Found NsdManager Resolver free, using it");
busy = true;
} catch (InterruptedException e) {
Log.e(TAG, "Failure while waiting for condition: " + e);
}
lock.unlock();
}

/** Signals the NsdManager resolver as free */
public void signalFree() {
lock.lock();
Log.d(TAG, "Signaling NsdManager Resolver as free");
busy = false;
condition.signalAll();
sharadb-amazon marked this conversation as resolved.
Show resolved Hide resolved
lock.unlock();
}
}
}