Factor out adding records to a generic
- Make a method addOrReplaceRecord() that can support point, inet4Address and inet6Address records adding. - Also fix some leftover comments in aosp/2431933 Bug: 268586836 Test: atest FrameworksNetTests Change-Id: I01462a967a04e88f14dcbb92ca207deb4268612c
This commit is contained in:
@@ -81,6 +81,21 @@ public class MdnsResponse {
|
|||||||
return a == null || a.getTtl() == b.getTtl();
|
return a == null || a.getTtl() == b.getTtl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <T extends MdnsRecord> boolean addOrReplaceRecord(@NonNull T record,
|
||||||
|
@NonNull List<T> recordsList) {
|
||||||
|
final int existing = recordsList.indexOf(record);
|
||||||
|
if (existing >= 0) {
|
||||||
|
if (recordsAreSame(record, recordsList.get(existing))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final MdnsRecord existedRecord = recordsList.remove(existing);
|
||||||
|
records.remove(existedRecord);
|
||||||
|
}
|
||||||
|
recordsList.add(record);
|
||||||
|
records.add(record);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a pointer record.
|
* Adds a pointer record.
|
||||||
*
|
*
|
||||||
@@ -92,17 +107,7 @@ public class MdnsResponse {
|
|||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Pointer records for different service names cannot be added");
|
"Pointer records for different service names cannot be added");
|
||||||
}
|
}
|
||||||
final int existing = pointerRecords.indexOf(pointerRecord);
|
return addOrReplaceRecord(pointerRecord, pointerRecords);
|
||||||
if (existing >= 0) {
|
|
||||||
if (recordsAreSame(pointerRecord, pointerRecords.get(existing))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
final MdnsRecord record = pointerRecords.remove(existing);
|
|
||||||
records.remove(record);
|
|
||||||
}
|
|
||||||
pointerRecords.add(pointerRecord);
|
|
||||||
records.add(pointerRecord);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets the pointer records. */
|
/** Gets the pointer records. */
|
||||||
@@ -207,17 +212,7 @@ public class MdnsResponse {
|
|||||||
/** Add the IPv4 address record. */
|
/** Add the IPv4 address record. */
|
||||||
public synchronized boolean addInet4AddressRecord(
|
public synchronized boolean addInet4AddressRecord(
|
||||||
@NonNull MdnsInetAddressRecord newInet4AddressRecord) {
|
@NonNull MdnsInetAddressRecord newInet4AddressRecord) {
|
||||||
final int existing = inet4AddressRecords.indexOf(newInet4AddressRecord);
|
return addOrReplaceRecord(newInet4AddressRecord, inet4AddressRecords);
|
||||||
if (existing >= 0) {
|
|
||||||
if (recordsAreSame(newInet4AddressRecord, inet4AddressRecords.get(existing))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
final MdnsRecord record = inet4AddressRecords.remove(existing);
|
|
||||||
records.remove(record);
|
|
||||||
}
|
|
||||||
inet4AddressRecords.add(newInet4AddressRecord);
|
|
||||||
records.add(newInet4AddressRecord);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets the IPv4 address records. */
|
/** Gets the IPv4 address records. */
|
||||||
@@ -248,17 +243,7 @@ public class MdnsResponse {
|
|||||||
/** Sets the IPv6 address records. */
|
/** Sets the IPv6 address records. */
|
||||||
public synchronized boolean addInet6AddressRecord(
|
public synchronized boolean addInet6AddressRecord(
|
||||||
@NonNull MdnsInetAddressRecord newInet6AddressRecord) {
|
@NonNull MdnsInetAddressRecord newInet6AddressRecord) {
|
||||||
final int existing = inet6AddressRecords.indexOf(newInet6AddressRecord);
|
return addOrReplaceRecord(newInet6AddressRecord, inet6AddressRecords);
|
||||||
if (existing >= 0) {
|
|
||||||
if (recordsAreSame(newInet6AddressRecord, inet6AddressRecords.get(existing))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
final MdnsRecord record = inet6AddressRecords.remove(existing);
|
|
||||||
records.remove(record);
|
|
||||||
}
|
|
||||||
inet6AddressRecords.add(newInet6AddressRecord);
|
|
||||||
records.add(newInet6AddressRecord);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -220,9 +220,10 @@ public class MdnsResponseDecoder {
|
|||||||
// This bit, the cache-flush bit, tells neighboring hosts
|
// This bit, the cache-flush bit, tells neighboring hosts
|
||||||
// that this is not a shared record type. Instead of merging this new
|
// that this is not a shared record type. Instead of merging this new
|
||||||
// record additively into the cache in addition to any previous records with
|
// record additively into the cache in addition to any previous records with
|
||||||
// the same name, rrtype, and rrclass, all old records with that name,
|
// the same name, rrtype, and rrclass.
|
||||||
// rrtype, and rrclass that were received more than one second ago are
|
// TODO: All old records with that name, rrtype, and rrclass that were
|
||||||
// declared invalid, and marked to expire from the cache in one second.
|
// received more than one second ago are declared invalid, and marked
|
||||||
|
// to expire from the cache in one second.
|
||||||
if (inetRecord.getCacheFlush()) {
|
if (inetRecord.getCacheFlush()) {
|
||||||
response.clearInet4AddressRecords();
|
response.clearInet4AddressRecords();
|
||||||
response.clearInet6AddressRecords();
|
response.clearInet6AddressRecords();
|
||||||
@@ -236,9 +237,10 @@ public class MdnsResponseDecoder {
|
|||||||
// This bit, the cache-flush bit, tells neighboring hosts
|
// This bit, the cache-flush bit, tells neighboring hosts
|
||||||
// that this is not a shared record type. Instead of merging this new
|
// that this is not a shared record type. Instead of merging this new
|
||||||
// record additively into the cache in addition to any previous records with
|
// record additively into the cache in addition to any previous records with
|
||||||
// the same name, rrtype, and rrclass, all old records with that name,
|
// the same name, rrtype, and rrclass.
|
||||||
// rrtype, and rrclass that were received more than one second ago are
|
// TODO: All old records with that name, rrtype, and rrclass that were
|
||||||
// declared invalid, and marked to expire from the cache in one second.
|
// received more than one second ago are declared invalid, and marked
|
||||||
|
// to expire from the cache in one second.
|
||||||
if (inetRecord.getCacheFlush()) {
|
if (inetRecord.getCacheFlush()) {
|
||||||
response.clearInet4AddressRecords();
|
response.clearInet4AddressRecords();
|
||||||
response.clearInet6AddressRecords();
|
response.clearInet6AddressRecords();
|
||||||
|
|||||||
@@ -156,16 +156,20 @@ public class MdnsResponseDecoderTests {
|
|||||||
+ "010001000000780004C0A8018A0000000000000000000000000000"
|
+ "010001000000780004C0A8018A0000000000000000000000000000"
|
||||||
+ "000000");
|
+ "000000");
|
||||||
|
|
||||||
// MDNS record for name "testhost1" with an IPv4 address of 10.1.2.3
|
// MDNS record for name "testhost1" with an IPv4 address of 10.1.2.3. Also set cache flush bit
|
||||||
|
// for the records changed.
|
||||||
private static final byte[] DATAIN_IPV4_1 = HexDump.hexStringToByteArray(
|
private static final byte[] DATAIN_IPV4_1 = HexDump.hexStringToByteArray(
|
||||||
"0974657374686f73743100000180010000007800040a010203");
|
"0974657374686f73743100000180010000007800040a010203");
|
||||||
// MDNS record for name "testhost1" with an IPv4 address of 10.1.2.4
|
// MDNS record for name "testhost1" with an IPv4 address of 10.1.2.4. Also set cache flush bit
|
||||||
|
// for the records changed.
|
||||||
private static final byte[] DATAIN_IPV4_2 = HexDump.hexStringToByteArray(
|
private static final byte[] DATAIN_IPV4_2 = HexDump.hexStringToByteArray(
|
||||||
"0974657374686f73743100000180010000007800040a010204");
|
"0974657374686f73743100000180010000007800040a010204");
|
||||||
// MDNS record w/name "testhost1" & IPv6 address of aabb:ccdd:1122:3344:a0b0:c0d0:1020:3040
|
// MDNS record w/name "testhost1" & IPv6 address of aabb:ccdd:1122:3344:a0b0:c0d0:1020:3040.
|
||||||
|
// Also set cache flush bit for the records changed.
|
||||||
private static final byte[] DATAIN_IPV6_1 = HexDump.hexStringToByteArray(
|
private static final byte[] DATAIN_IPV6_1 = HexDump.hexStringToByteArray(
|
||||||
"0974657374686f73743100001c8001000000780010aabbccdd11223344a0b0c0d010203040");
|
"0974657374686f73743100001c8001000000780010aabbccdd11223344a0b0c0d010203040");
|
||||||
// MDNS record w/name "testhost1" & IPv6 address of aabb:ccdd:1122:3344:a0b0:c0d0:1020:3030
|
// MDNS record w/name "testhost1" & IPv6 address of aabb:ccdd:1122:3344:a0b0:c0d0:1020:3030.
|
||||||
|
// Also set cache flush bit for the records changed.
|
||||||
private static final byte[] DATAIN_IPV6_2 = HexDump.hexStringToByteArray(
|
private static final byte[] DATAIN_IPV6_2 = HexDump.hexStringToByteArray(
|
||||||
"0974657374686f73743100001c8001000000780010aabbccdd11223344a0b0c0d010203030");
|
"0974657374686f73743100001c8001000000780010aabbccdd11223344a0b0c0d010203030");
|
||||||
// MDNS record w/name "test" & PTR to foo.bar.quxx
|
// MDNS record w/name "test" & PTR to foo.bar.quxx
|
||||||
|
|||||||
@@ -449,8 +449,8 @@ public class MdnsServiceTypeClientTests {
|
|||||||
verifyServiceInfo(serviceInfoCaptor.getAllValues().get(0),
|
verifyServiceInfo(serviceInfoCaptor.getAllValues().get(0),
|
||||||
"service-instance-1",
|
"service-instance-1",
|
||||||
SERVICE_TYPE_LABELS,
|
SERVICE_TYPE_LABELS,
|
||||||
/* ipv4Address= */ List.of(),
|
/* ipv4Addresses= */ List.of(),
|
||||||
/* ipv6Address= */ List.of(),
|
/* ipv6Addresses= */ List.of(),
|
||||||
/* port= */ 0,
|
/* port= */ 0,
|
||||||
/* subTypes= */ List.of(),
|
/* subTypes= */ List.of(),
|
||||||
Collections.emptyMap(),
|
Collections.emptyMap(),
|
||||||
|
|||||||
Reference in New Issue
Block a user