Skip to content
Prev Previous commit
Next Next commit
reduce duplicate code in mDNS support changes
Refactor the mDNS changes to reduce duplicate code.
  • Loading branch information
ka2ddo authored Jan 7, 2021
commit 855b7be395ffb5e6a65ef29e78ca2f1d0cfef1b3
20 changes: 11 additions & 9 deletions src/main/java/org/xbill/DNS/NioUdpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import java.security.SecureRandom;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand Down Expand Up @@ -86,7 +88,7 @@ private static class Transaction implements KeyProcessor {
private final long endTime;
private final DatagramChannel channel;
private final SocketAddress remoteSocketAddress;
final CompletableFuture<Object> f;
final CompletableFuture<List<byte[]>> f;

void send() throws IOException {
ByteBuffer buffer = ByteBuffer.wrap(data);
Expand Down Expand Up @@ -132,10 +134,10 @@ public void processReadyKey(SelectionKey key) {
verboseLog(
"UDP read",
channel.socket().getLocalSocketAddress(),
remoteSocketAddress,
source,
data);
silentCloseChannel();
f.complete(data);
f.complete(Collections.singletonList(data));
pendingTransactions.remove(this);
}

Expand All @@ -147,7 +149,7 @@ void silentCloseChannel() {
// ignore, we either already have everything we need or can't do anything
}
}

void closeTransaction() {
silentCloseChannel();
f.completeExceptionally(new SocketTimeoutException("Query timed out"));
Expand All @@ -157,7 +159,7 @@ void closeTransaction() {
private static class MultiAnswerTransaction extends Transaction {
MultiAnswerTransaction(byte[] query, int max, long endTime, DatagramChannel channel,
SocketAddress remoteSocketAddress,
CompletableFuture<Object> f) {
CompletableFuture<List<byte[]>> f) {
super(query, max, endTime, channel, remoteSocketAddress, f);
}

Expand Down Expand Up @@ -211,9 +213,9 @@ void closeTransaction() {
}
}

static CompletableFuture<Object> sendrecv(
static CompletableFuture<List<byte[]>> sendrecv(
InetSocketAddress local, InetSocketAddress remote, byte[] data, int max, Duration timeout) {
CompletableFuture<Object> f = new CompletableFuture<>();
CompletableFuture<List<byte[]>> f = new CompletableFuture<>();
try {
final Selector selector = selector();
DatagramChannel channel = DatagramChannel.open();
Expand Down Expand Up @@ -255,10 +257,10 @@ static CompletableFuture<Object> sendrecv(
Transaction t;
if (!remote.getAddress().isMulticastAddress()) {
channel.connect(remote);
t = new Transaction(data, max, endTime, channel, f);
t = new Transaction(data, max, endTime, channel, remote, f);
} else {
// stop this a little before the timeout so we can report what answers we did get
t = new MultiAnswerTransaction(data, max, endTime - 1000000000L, channel, f);
t = new MultiAnswerTransaction(data, max, endTime - 1000000000L, channel, remote, f);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the behavior between DNS and mDNS consistent with this substraction: either substract in both cases, or in neither.

And I think this is a rather high threshold, processing the answers once they're read from the network is really fast (0 to 1ms on my machein), so TimeUnit.MILLISECONDS.toNanos(10) is more apropriate.

}
pendingTransactions.add(t);
registrationQueue.add(t);
Expand Down