Merge "Use an integer for query mode" into main

This commit is contained in:
Paul Hu 2024-01-29 08:08:58 +00:00 • committed by Gerrit Code Review
commit 22827c3814
5 changed files with 63 additions and 53 deletions

View file

@ -35,6 +35,7 @@ import static com.android.networkstack.apishim.ConstantsShim.REGISTER_NSD_OFFLOA
import static com.android.server.connectivity.mdns.MdnsAdvertiser.AdvertiserMetrics;
import static com.android.server.connectivity.mdns.MdnsConstants.NO_PACKET;
import static com.android.server.connectivity.mdns.MdnsRecord.MAX_LABEL_LENGTH;
import static com.android.server.connectivity.mdns.MdnsSearchOptions.PASSIVE_QUERY_MODE;
import static com.android.server.connectivity.mdns.util.MdnsUtils.Clock;
import android.annotation.NonNull;
@ -793,8 +794,7 @@ public class NsdService extends INsdManager.Stub {
MdnsSearchOptions.newBuilder()
.setNetwork(discoveryRequest.getNetwork())
.setRemoveExpiredService(true)
.setIsPassiveMode(true);
.setQueryMode(PASSIVE_QUERY_MODE);
if (subtype != null) {
// checkSubtypeLabels() ensures that subtypes start with '_' but
// MdnsSearchOptions expects the underscore to not be present.
@ -1044,7 +1044,7 @@ public class NsdService extends INsdManager.Stub {
transactionId, resolveServiceType);
final MdnsSearchOptions options = MdnsSearchOptions.newBuilder()
.setNetwork(info.getNetwork())
.setIsPassiveMode(true)
.setQueryMode(PASSIVE_QUERY_MODE)
.setResolveInstanceName(info.getServiceName())
.setRemoveExpiredService(true)
.build();
@ -1142,7 +1142,7 @@ public class NsdService extends INsdManager.Stub {
transactionId, resolveServiceType);
final MdnsSearchOptions options = MdnsSearchOptions.newBuilder()
.setNetwork(info.getNetwork())
.setIsPassiveMode(true)
.setQueryMode(PASSIVE_QUERY_MODE)
.setResolveInstanceName(info.getServiceName())
.setRemoveExpiredService(true)
.build();

View file

@ -39,6 +39,11 @@ import java.util.Set;
* @hide
*/
public class MdnsSearchOptions implements Parcelable {
// Passive query mode scans less frequently in order to conserve battery and produce less
// network traffic.
public static final int PASSIVE_QUERY_MODE = 0;
// Active query mode scans frequently.
public static final int ACTIVE_QUERY_MODE = 1;
/** @hide */
public static final Parcelable.Creator<MdnsSearchOptions> CREATOR =
@ -47,7 +52,7 @@ public class MdnsSearchOptions implements Parcelable {
public MdnsSearchOptions createFromParcel(Parcel source) {
return new MdnsSearchOptions(
source.createStringArrayList(),
source.readInt() == 1,
source.readInt(),
source.readInt() == 1,
source.readParcelable(null),
source.readString(),
@ -64,7 +69,7 @@ public class MdnsSearchOptions implements Parcelable {
private final List<String> subtypes;
@Nullable
private final String resolveInstanceName;
private final boolean isPassiveMode;
private final int queryMode;
private final boolean onlyUseIpv6OnIpv6OnlyNetworks;
private final int numOfQueriesBeforeBackoff;
private final boolean removeExpiredService;
@ -74,7 +79,7 @@ public class MdnsSearchOptions implements Parcelable {
/** Parcelable constructs for a {@link MdnsSearchOptions}. */
MdnsSearchOptions(
List<String> subtypes,
boolean isPassiveMode,
int queryMode,
boolean removeExpiredService,
@Nullable Network network,
@Nullable String resolveInstanceName,
@ -84,7 +89,7 @@ public class MdnsSearchOptions implements Parcelable {
if (subtypes != null) {
this.subtypes.addAll(subtypes);
}
this.isPassiveMode = isPassiveMode;
this.queryMode = queryMode;
this.onlyUseIpv6OnIpv6OnlyNetworks = onlyUseIpv6OnIpv6OnlyNetworks;
this.numOfQueriesBeforeBackoff = numOfQueriesBeforeBackoff;
this.removeExpiredService = removeExpiredService;
@ -111,11 +116,10 @@ public class MdnsSearchOptions implements Parcelable {
}
/**
* @return {@code true} if the passive mode is used. The passive mode scans less frequently in
* order to conserve battery and produce less network traffic.
* @return the current query mode.
*/
public boolean isPassiveMode() {
return isPassiveMode;
public int getQueryMode() {
return queryMode;
}
/**
@ -166,7 +170,7 @@ public class MdnsSearchOptions implements Parcelable {
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeStringList(subtypes);
out.writeInt(isPassiveMode ? 1 : 0);
out.writeInt(queryMode);
out.writeInt(removeExpiredService ? 1 : 0);
out.writeParcelable(mNetwork, 0);
out.writeString(resolveInstanceName);
@ -177,7 +181,7 @@ public class MdnsSearchOptions implements Parcelable {
/** A builder to create {@link MdnsSearchOptions}. */
public static final class Builder {
private final Set<String> subtypes;
private boolean isPassiveMode = true;
private int queryMode = PASSIVE_QUERY_MODE;
private boolean onlyUseIpv6OnIpv6OnlyNetworks = false;
private int numOfQueriesBeforeBackoff = 3;
private boolean removeExpiredService;
@ -212,14 +216,12 @@ public class MdnsSearchOptions implements Parcelable {
}
/**
* Sets if the passive mode scan should be used. The passive mode scans less frequently in
* order to conserve battery and produce less network traffic.
* Sets which query mode should be used.
*
* @param isPassiveMode If set to {@code true}, passive mode will be used. If set to {@code
* false}, active mode will be used.
* @param queryMode the query mode should be used.
*/
public Builder setIsPassiveMode(boolean isPassiveMode) {
this.isPassiveMode = isPassiveMode;
public Builder setQueryMode(int queryMode) {
this.queryMode = queryMode;
return this;
}
@ -276,7 +278,7 @@ public class MdnsSearchOptions implements Parcelable {
public MdnsSearchOptions build() {
return new MdnsSearchOptions(
new ArrayList<>(subtypes),
isPassiveMode,
queryMode,
removeExpiredService,
mNetwork,
resolveInstanceName,

View file

@ -330,7 +330,7 @@ public class MdnsServiceTypeClient {
// interested anymore.
final QueryTaskConfig taskConfig = new QueryTaskConfig(
searchOptions.getSubtypes(),
searchOptions.isPassiveMode(),
searchOptions.getQueryMode(),
searchOptions.onlyUseIpv6OnIpv6OnlyNetworks(),
searchOptions.numOfQueriesBeforeBackoff(),
socketKey);

View file

@ -16,6 +16,8 @@
package com.android.server.connectivity.mdns;
import static com.android.server.connectivity.mdns.MdnsSearchOptions.PASSIVE_QUERY_MODE;
import android.annotation.NonNull;
import android.annotation.Nullable;
@ -45,7 +47,7 @@ public class QueryTaskConfig {
final List<String> subtypes;
private final boolean alwaysAskForUnicastResponse =
MdnsConfigs.alwaysAskForUnicastResponseInEachBurst();
private final boolean usePassiveMode;
private final int queryMode;
final boolean onlyUseIpv6OnIpv6OnlyNetworks;
private final int numOfQueriesBeforeBackoff;
@VisibleForTesting
@ -66,7 +68,7 @@ public class QueryTaskConfig {
int queriesPerBurst, int timeBetweenBurstsInMs,
long delayUntilNextTaskWithoutBackoffMs) {
this.subtypes = new ArrayList<>(other.subtypes);
this.usePassiveMode = other.usePassiveMode;
this.queryMode = other.queryMode;
this.onlyUseIpv6OnIpv6OnlyNetworks = other.onlyUseIpv6OnIpv6OnlyNetworks;
this.numOfQueriesBeforeBackoff = other.numOfQueriesBeforeBackoff;
this.transactionId = transactionId;
@ -80,11 +82,11 @@ public class QueryTaskConfig {
this.socketKey = other.socketKey;
}
QueryTaskConfig(@NonNull Collection<String> subtypes,
boolean usePassiveMode,
int queryMode,
boolean onlyUseIpv6OnIpv6OnlyNetworks,
int numOfQueriesBeforeBackoff,
@Nullable SocketKey socketKey) {
this.usePassiveMode = usePassiveMode;
this.queryMode = queryMode;
this.onlyUseIpv6OnIpv6OnlyNetworks = onlyUseIpv6OnIpv6OnlyNetworks;
this.numOfQueriesBeforeBackoff = numOfQueriesBeforeBackoff;
this.subtypes = new ArrayList<>(subtypes);
@ -94,7 +96,7 @@ public class QueryTaskConfig {
this.expectUnicastResponse = true;
this.isFirstBurst = true;
// Config the scan frequency based on the scan mode.
if (this.usePassiveMode) {
if (this.queryMode == PASSIVE_QUERY_MODE) {
// In passive scan mode, sends a single burst of QUERIES_PER_BURST queries, and then
// in each TIME_BETWEEN_BURSTS interval, sends QUERIES_PER_BURST_PASSIVE_MODE
// queries.
@ -138,7 +140,7 @@ public class QueryTaskConfig {
// queries.
if (isFirstBurst) {
newIsFirstBurst = false;
if (usePassiveMode) {
if (queryMode == PASSIVE_QUERY_MODE) {
newQueriesPerBurst = QUERIES_PER_BURST_PASSIVE_MODE;
}
}

View file

@ -16,6 +16,8 @@
package com.android.server.connectivity.mdns;
import static com.android.server.connectivity.mdns.MdnsSearchOptions.ACTIVE_QUERY_MODE;
import static com.android.server.connectivity.mdns.MdnsSearchOptions.PASSIVE_QUERY_MODE;
import static com.android.server.connectivity.mdns.MdnsServiceTypeClient.EVENT_START_QUERYTASK;
import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2;
@ -267,8 +269,8 @@ public class MdnsServiceTypeClientTests {
@Test
public void sendQueries_activeScanMode() {
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(false).build();
MdnsSearchOptions searchOptions = MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE).setQueryMode(ACTIVE_QUERY_MODE).build();
startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@ -319,8 +321,8 @@ public class MdnsServiceTypeClientTests {
@Test
public void sendQueries_reentry_activeScanMode() {
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(false).build();
MdnsSearchOptions searchOptions = MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE).setQueryMode(ACTIVE_QUERY_MODE).build();
startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@ -333,7 +335,7 @@ public class MdnsServiceTypeClientTests {
MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE)
.addSubtype("_subtype2")
.setIsPassiveMode(false)
.setQueryMode(ACTIVE_QUERY_MODE)
.build();
startSendAndReceive(mockListenerOne, searchOptions);
// The previous scheduled task should be canceled.
@ -353,8 +355,8 @@ public class MdnsServiceTypeClientTests {
@Test
public void sendQueries_passiveScanMode() {
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(true).build();
MdnsSearchOptions searchOptions = MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE).setQueryMode(PASSIVE_QUERY_MODE).build();
startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@ -380,8 +382,10 @@ public class MdnsServiceTypeClientTests {
@Test
public void sendQueries_activeScanWithQueryBackoff() {
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(
false).setNumOfQueriesBeforeBackoff(11).build();
MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE)
.setQueryMode(ACTIVE_QUERY_MODE)
.setNumOfQueriesBeforeBackoff(11).build();
startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@ -439,8 +443,10 @@ public class MdnsServiceTypeClientTests {
@Test
public void sendQueries_passiveScanWithQueryBackoff() {
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(
true).setNumOfQueriesBeforeBackoff(3).build();
MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE)
.setQueryMode(PASSIVE_QUERY_MODE)
.setNumOfQueriesBeforeBackoff(3).build();
startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@ -497,8 +503,8 @@ public class MdnsServiceTypeClientTests {
@Test
public void sendQueries_reentry_passiveScanMode() {
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(true).build();
MdnsSearchOptions searchOptions = MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE).setQueryMode(PASSIVE_QUERY_MODE).build();
startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@ -511,7 +517,7 @@ public class MdnsServiceTypeClientTests {
MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE)
.addSubtype("_subtype2")
.setIsPassiveMode(true)
.setQueryMode(PASSIVE_QUERY_MODE)
.build();
startSendAndReceive(mockListenerOne, searchOptions);
// The previous scheduled task should be canceled.
@ -533,10 +539,10 @@ public class MdnsServiceTypeClientTests {
@Ignore("MdnsConfigs is not configurable currently.")
public void testQueryTaskConfig_alwaysAskForUnicastResponse() {
//MdnsConfigsFlagsImpl.alwaysAskForUnicastResponseInEachBurst.override(true);
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(false).build();
MdnsSearchOptions searchOptions = MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE).setQueryMode(ACTIVE_QUERY_MODE).build();
QueryTaskConfig config = new QueryTaskConfig(
searchOptions.getSubtypes(), searchOptions.isPassiveMode(),
searchOptions.getSubtypes(), searchOptions.getQueryMode(),
false /* onlyUseIpv6OnIpv6OnlyNetworks */, 3 /* numOfQueriesBeforeBackoff */,
socketKey);
@ -564,10 +570,10 @@ public class MdnsServiceTypeClientTests {
@Test
public void testQueryTaskConfig_askForUnicastInFirstQuery() {
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(false).build();
MdnsSearchOptions searchOptions = MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE).setQueryMode(ACTIVE_QUERY_MODE).build();
QueryTaskConfig config = new QueryTaskConfig(
searchOptions.getSubtypes(), searchOptions.isPassiveMode(),
searchOptions.getSubtypes(), searchOptions.getQueryMode(),
false /* onlyUseIpv6OnIpv6OnlyNetworks */, 3 /* numOfQueriesBeforeBackoff */,
socketKey);
@ -595,8 +601,8 @@ public class MdnsServiceTypeClientTests {
@Test
public void testIfPreviousTaskIsCanceledWhenNewSessionStarts() {
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(true).build();
MdnsSearchOptions searchOptions = MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE).setQueryMode(PASSIVE_QUERY_MODE).build();
startSendAndReceive(mockListenerOne, searchOptions);
Runnable firstMdnsTask = currentThreadExecutor.getAndClearSubmittedRunnable();
@ -605,7 +611,7 @@ public class MdnsServiceTypeClientTests {
MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE)
.addSubtype("_subtype2")
.setIsPassiveMode(true)
.setQueryMode(PASSIVE_QUERY_MODE)
.build();
startSendAndReceive(mockListenerOne, searchOptions);
@ -624,8 +630,8 @@ public class MdnsServiceTypeClientTests {
@Ignore("MdnsConfigs is not configurable currently.")
public void testIfPreviousTaskIsCanceledWhenSessionStops() {
//MdnsConfigsFlagsImpl.shouldCancelScanTaskWhenFutureIsNull.override(true);
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(true).build();
MdnsSearchOptions searchOptions = MdnsSearchOptions.newBuilder()
.addSubtype(SUBTYPE).setQueryMode(PASSIVE_QUERY_MODE).build();
startSendAndReceive(mockListenerOne, searchOptions);
// Change the sutypes and start a new session.
stopSendAndReceive(mockListenerOne);