Move 'isProbing' field from RecordInfo to ServiceRegistration

Move the 'isProbing' field from RecordInfo to ServiceRegistration class.
The 'isProbing' state is tightly coupled to each registration, rather
than each record. This refactor simplifies the RecordInfo class, which
is necessary for future updates to support updating RecordInfo in place.

Bug: 300560526
Test: TH
Change-Id: Ic7a5514fe5ce3262896ef34a6b629780da022f30
This commit is contained in:
Yuyang Huang
2023-11-07 14:39:49 +09:00
parent 9f76411b63
commit d1f779d88f

View File

@@ -132,11 +132,6 @@ public class MdnsRecordRepository {
*/ */
public final boolean isSharedName; public final boolean isSharedName;
/**
* Whether probing is still in progress for the record.
*/
public boolean isProbing;
/** /**
* Last time (as per SystemClock.elapsedRealtime) when advertised via multicast, 0 if never * Last time (as per SystemClock.elapsedRealtime) when advertised via multicast, 0 if never
*/ */
@@ -148,12 +143,10 @@ public class MdnsRecordRepository {
*/ */
public long lastSentTimeMs; public long lastSentTimeMs;
RecordInfo(NsdServiceInfo serviceInfo, T record, boolean sharedName, RecordInfo(NsdServiceInfo serviceInfo, T record, boolean sharedName) {
boolean probing) {
this.serviceInfo = serviceInfo; this.serviceInfo = serviceInfo;
this.record = record; this.record = record;
this.isSharedName = sharedName; this.isSharedName = sharedName;
this.isProbing = probing;
} }
} }
@@ -186,6 +179,11 @@ public class MdnsRecordRepository {
*/ */
public int sentPacketCount = NO_PACKET; public int sentPacketCount = NO_PACKET;
/**
* Whether probing is still in progress.
*/
private boolean isProbing;
/** /**
* Create a ServiceRegistration for dns-sd service registration (RFC6763). * Create a ServiceRegistration for dns-sd service registration (RFC6763).
* *
@@ -209,7 +207,7 @@ public class MdnsRecordRepository {
false /* cacheFlush */, false /* cacheFlush */,
NON_NAME_RECORDS_TTL_MILLIS, NON_NAME_RECORDS_TTL_MILLIS,
serviceName), serviceName),
true /* sharedName */, true /* probing */); true /* sharedName */);
if (subtype == null) { if (subtype == null) {
this.ptrRecords = Collections.singletonList(ptrRecord); this.ptrRecords = Collections.singletonList(ptrRecord);
@@ -226,7 +224,7 @@ public class MdnsRecordRepository {
false /* cacheFlush */, false /* cacheFlush */,
NON_NAME_RECORDS_TTL_MILLIS, NON_NAME_RECORDS_TTL_MILLIS,
serviceName), serviceName),
true /* sharedName */, true /* probing */); true /* sharedName */);
this.ptrRecords = List.of(ptrRecord, subtypeRecord); this.ptrRecords = List.of(ptrRecord, subtypeRecord);
} }
@@ -239,7 +237,7 @@ public class MdnsRecordRepository {
NAME_RECORDS_TTL_MILLIS, 0 /* servicePriority */, 0 /* serviceWeight */, NAME_RECORDS_TTL_MILLIS, 0 /* servicePriority */, 0 /* serviceWeight */,
serviceInfo.getPort(), serviceInfo.getPort(),
deviceHostname), deviceHostname),
false /* sharedName */, true /* probing */); false /* sharedName */);
txtRecord = new RecordInfo<>( txtRecord = new RecordInfo<>(
serviceInfo, serviceInfo,
@@ -248,7 +246,7 @@ public class MdnsRecordRepository {
true /* cacheFlush */, // Service name is verified unique after probing true /* cacheFlush */, // Service name is verified unique after probing
NON_NAME_RECORDS_TTL_MILLIS, NON_NAME_RECORDS_TTL_MILLIS,
attrsToTextEntries(serviceInfo.getAttributes())), attrsToTextEntries(serviceInfo.getAttributes())),
false /* sharedName */, true /* probing */); false /* sharedName */);
final ArrayList<RecordInfo<?>> allRecords = new ArrayList<>(5); final ArrayList<RecordInfo<?>> allRecords = new ArrayList<>(5);
allRecords.addAll(ptrRecords); allRecords.addAll(ptrRecords);
@@ -263,18 +261,18 @@ public class MdnsRecordRepository {
false /* cacheFlush */, false /* cacheFlush */,
NON_NAME_RECORDS_TTL_MILLIS, NON_NAME_RECORDS_TTL_MILLIS,
serviceType), serviceType),
true /* sharedName */, true /* probing */)); true /* sharedName */));
this.allRecords = Collections.unmodifiableList(allRecords); this.allRecords = Collections.unmodifiableList(allRecords);
this.repliedServiceCount = repliedServiceCount; this.repliedServiceCount = repliedServiceCount;
this.sentPacketCount = sentPacketCount; this.sentPacketCount = sentPacketCount;
this.isProbing = true;
} }
void setProbing(boolean probing) { void setProbing(boolean probing) {
for (RecordInfo<?> info : allRecords) { this.isProbing = probing;
info.isProbing = probing;
}
} }
} }
/** /**
@@ -292,7 +290,7 @@ public class MdnsRecordRepository {
true /* cacheFlush */, true /* cacheFlush */,
NAME_RECORDS_TTL_MILLIS, NAME_RECORDS_TTL_MILLIS,
mDeviceHostname), mDeviceHostname),
false /* sharedName */, false /* probing */)); false /* sharedName */));
mGeneralRecords.add(new RecordInfo<>( mGeneralRecords.add(new RecordInfo<>(
null /* serviceInfo */, null /* serviceInfo */,
@@ -302,7 +300,7 @@ public class MdnsRecordRepository {
true /* cacheFlush */, true /* cacheFlush */,
NAME_RECORDS_TTL_MILLIS, NAME_RECORDS_TTL_MILLIS,
addr.getAddress()), addr.getAddress()),
false /* sharedName */, false /* probing */)); false /* sharedName */));
} }
} }
@@ -485,7 +483,7 @@ public class MdnsRecordRepository {
// Add answers from each service // Add answers from each service
for (int i = 0; i < mServices.size(); i++) { for (int i = 0; i < mServices.size(); i++) {
final ServiceRegistration registration = mServices.valueAt(i); final ServiceRegistration registration = mServices.valueAt(i);
if (registration.exiting) continue; if (registration.exiting || registration.isProbing) continue;
if (addReplyFromService(question, registration.allRecords, registration.ptrRecords, if (addReplyFromService(question, registration.allRecords, registration.ptrRecords,
registration.srvRecord, registration.txtRecord, replyUnicast, now, registration.srvRecord, registration.txtRecord, replyUnicast, now,
answerInfo, additionalAnswerRecords)) { answerInfo, additionalAnswerRecords)) {
@@ -558,7 +556,6 @@ public class MdnsRecordRepository {
final int answersStartIndex = answerInfo.size(); final int answersStartIndex = answerInfo.size();
for (RecordInfo<?> info : serviceRecords) { for (RecordInfo<?> info : serviceRecords) {
if (info.isProbing) continue;
/* RFC6762 6.: the record name must match the question name, the record rrtype /* RFC6762 6.: the record name must match the question name, the record rrtype
must match the question qtype unless the qtype is "ANY" (255) or the rrtype is must match the question qtype unless the qtype is "ANY" (255) or the rrtype is
@@ -870,7 +867,7 @@ public class MdnsRecordRepository {
final ServiceRegistration registration = mServices.get(serviceId); final ServiceRegistration registration = mServices.get(serviceId);
if (registration == null) return false; if (registration == null) return false;
return registration.srvRecord.isProbing; return registration.isProbing;
} }
/** /**