Skip to content

Commit

Permalink
Android tv-casting lib: Implementing a retry mechanism for commission…
Browse files Browse the repository at this point in the history
…er service resolution using NsdManager
  • Loading branch information
sharadb-amazon committed Nov 22, 2022
1 parent a23303c commit 5bf3aff
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ public void handle(MatterError matterError) {
}
};

Button discoverButton = getView().findViewById(R.id.discoverButton);
discoverButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Discovering on button click");
tvCastingApp.discoverVideoPlayerCommissioners(
DISCOVERY_DURATION_SECS, successCallback, failureCallback);
}
});

Log.d(TAG, "Auto discovering");
tvCastingApp.discoverVideoPlayerCommissioners(
DISCOVERY_DURATION_SECS, successCallback, failureCallback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public void onServiceFound(NsdServiceInfo service) {
preCommissionedVideoPlayers,
successCallback,
failureCallback,
nsdManagerResolverAvailState));
nsdManagerResolverAvailState,
1));
} else {
Log.d(TAG, "Ignoring discovered service: " + service.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,32 @@
import android.util.Log;
import chip.platform.NsdManagerServiceResolver;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class NsdResolveListener implements NsdManager.ResolveListener {

private static final String TAG = NsdResolveListener.class.getSimpleName();

private static final int MAX_RESOLUTION_ATTEMPTS = 5;
private static final int RESOLUTION_ATTEMPT_DELAY_SECS = 1;

private final NsdManager nsdManager;
private final List<Long> deviceTypeFilter;
private final List<VideoPlayer> preCommissionedVideoPlayers;
private final SuccessCallback<DiscoveredNodeData> successCallback;
private final FailureCallback failureCallback;
private final NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState;
private final int resolutionAttemptNumber;

public NsdResolveListener(
NsdManager nsdManager,
List<Long> deviceTypeFilter,
List<VideoPlayer> preCommissionedVideoPlayers,
SuccessCallback<DiscoveredNodeData> successCallback,
FailureCallback failureCallback,
NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState) {
NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState,
int resolutionAttemptNumber) {
this.nsdManager = nsdManager;
this.deviceTypeFilter = deviceTypeFilter;
this.preCommissionedVideoPlayers = preCommissionedVideoPlayers;
Expand All @@ -52,6 +59,7 @@ public NsdResolveListener(
this.successCallback = successCallback;
this.failureCallback = failureCallback;
this.nsdManagerResolverAvailState = nsdManagerResolverAvailState;
this.resolutionAttemptNumber = resolutionAttemptNumber;
}

@Override
Expand All @@ -77,15 +85,41 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) {
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.signalFree();
if (errorCode != NsdManager.FAILURE_ALREADY_ACTIVE
|| resolutionAttemptNumber >= MAX_RESOLUTION_ATTEMPTS) {
nsdManagerResolverAvailState.signalFree();
}
}

switch (errorCode) {
case NsdManager.FAILURE_ALREADY_ACTIVE:
Log.e(TAG, "NsdResolveListener FAILURE_ALREADY_ACTIVE - Service: " + serviceInfo);
failureCallback.handle(
new MatterError(
3, "NsdResolveListener FAILURE_ALREADY_ACTIVE - Service: " + serviceInfo));
if (resolutionAttemptNumber < MAX_RESOLUTION_ATTEMPTS) {
Log.d(TAG, "NsdResolveListener Scheduling a retry to resolve service " + serviceInfo);
Executors.newSingleThreadScheduledExecutor()
.schedule(
new Runnable() {
@Override
public void run() {
nsdManager.resolveService(
serviceInfo,
new NsdResolveListener(
nsdManager,
deviceTypeFilter,
preCommissionedVideoPlayers,
successCallback,
failureCallback,
nsdManagerResolverAvailState,
resolutionAttemptNumber + 1));
}
},
RESOLUTION_ATTEMPT_DELAY_SECS,
TimeUnit.SECONDS);
} else { // giving up
failureCallback.handle(
new MatterError(
3, "NsdResolveListener FAILURE_ALREADY_ACTIVE - Service: " + serviceInfo));
}
break;
case NsdManager.FAILURE_INTERNAL_ERROR:
Log.e(TAG, "NsdResolveListener FAILURE_INTERNAL_ERROR - Service: " + serviceInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
android:layout_height="wrap_content"
android:text="Skip to manual commissioning >>" />

<Button
android:id="@+id/discoverButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Discover>" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 5bf3aff

Please sign in to comment.