Skip to content

Commit

Permalink
Incorporating comments from andy31415@
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Nov 7, 2022
1 parent 181a9e4 commit 04ce083
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
Expand All @@ -40,15 +41,15 @@ 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;
@Nullable private final NsdManagerResolverAvailState nsdManagerResolverAvailState;

/**
* @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) {
Context context, @Nullable NsdManagerResolverAvailState nsdManagerResolverAvailState) {
this.nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
this.mainThreadHandler = new Handler(Looper.getMainLooper());

Expand Down Expand Up @@ -102,7 +103,7 @@ public void run() {
};

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

this.nsdManager.resolveService(
Expand Down Expand Up @@ -274,28 +275,32 @@ public static class NsdManagerResolverAvailState {
* Waits if the NsdManager is already busy with resolving a service. Otherwise, it marks it as
* busy and returns
*/
public void markBusyIfFreeOrWait() {
public void acquireResolver() {
lock.lock();
try {
while (busy) {
Log.d(TAG, "Found NsdManager Resolver busy, waiting");
condition.await();
}
Log.d(TAG, "Found NsdManager Resolver free, using it");
Log.d(TAG, "Found NsdManager Resolver free, using it and marking it as busy");
busy = true;
} catch (InterruptedException e) {
Log.e(TAG, "Failure while waiting for condition: " + e);
} finally {
lock.unlock();
}
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();
lock.unlock();
try {
Log.d(TAG, "Signaling NsdManager Resolver as free");
busy = false;
condition.signal();
} finally {
lock.unlock();
}
}
}
}

0 comments on commit 04ce083

Please sign in to comment.