1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.connectivity.mdns; 18 19 import static com.android.server.connectivity.mdns.MdnsSearchOptions.AGGRESSIVE_QUERY_MODE; 20 21 import com.android.internal.annotations.VisibleForTesting; 22 23 /** 24 * A configuration for the PeriodicalQueryTask that contains parameters to build a query packet. 25 * Call to getConfigForNextRun returns a config that can be used to build the next query task. 26 */ 27 public class QueryTaskConfig { 28 private static final int UNSIGNED_SHORT_MAX_VALUE = 65536; 29 private final boolean alwaysAskForUnicastResponse = 30 MdnsConfigs.alwaysAskForUnicastResponseInEachBurst(); 31 @VisibleForTesting 32 final boolean expectUnicastResponse; 33 final int queryIndex; 34 final int queryMode; 35 QueryTaskConfig(int queryMode, int queryIndex)36 QueryTaskConfig(int queryMode, int queryIndex) { 37 this.queryMode = queryMode; 38 this.queryIndex = queryIndex; 39 this.expectUnicastResponse = getExpectUnicastResponse(); 40 } 41 QueryTaskConfig(int queryMode)42 QueryTaskConfig(int queryMode) { 43 this(queryMode, 0); 44 } 45 46 /** 47 * Get new QueryTaskConfig for next run. 48 */ getConfigForNextRun(int queryMode)49 public QueryTaskConfig getConfigForNextRun(int queryMode) { 50 final int newQueryIndex = queryIndex + 1; 51 return new QueryTaskConfig(queryMode, newQueryIndex); 52 } 53 getTransactionId()54 public int getTransactionId() { 55 return (queryIndex % (UNSIGNED_SHORT_MAX_VALUE - 1)) + 1; 56 } 57 getExpectUnicastResponse()58 private boolean getExpectUnicastResponse() { 59 if (queryMode == AGGRESSIVE_QUERY_MODE) { 60 if (MdnsQueryScheduler.isFirstQueryInBurst(queryIndex, queryMode)) { 61 return true; 62 } 63 } 64 return queryIndex == 0 || alwaysAskForUnicastResponse; 65 } 66 } 67