1717import java .util .concurrent .CompletableFuture ;
1818import java .util .concurrent .ConcurrentLinkedQueue ;
1919import lombok .RequiredArgsConstructor ;
20- import lombok .experimental .UtilityClass ;
2120import lombok .extern .slf4j .Slf4j ;
21+ import org .xbill .DNS .io .UdpIoClient ;
2222
2323@ Slf4j
24- @ UtilityClass
25- final class NioUdpClient extends NioClient {
26- private static final int EPHEMERAL_START ;
27- private static final int EPHEMERAL_RANGE ;
24+ final class NioUdpClient extends NioClient implements UdpIoClient {
25+ private final int ephemeralStart ;
26+ private final int ephemeralRange ;
2827
29- private static final SecureRandom prng ;
30- private static final Queue <Transaction > registrationQueue = new ConcurrentLinkedQueue <>();
31- private static final Queue <Transaction > pendingTransactions = new ConcurrentLinkedQueue <>();
28+ private final SecureRandom prng ;
29+ private final Queue <Transaction > registrationQueue = new ConcurrentLinkedQueue <>();
30+ private final Queue <Transaction > pendingTransactions = new ConcurrentLinkedQueue <>();
3231
33- static {
32+ NioUdpClient () {
3433 // https://tools.ietf.org/html/rfc6335#section-6
3534 int ephemeralStartDefault = 49152 ;
3635 int ephemeralEndDefault = 65535 ;
@@ -41,21 +40,21 @@ final class NioUdpClient extends NioClient {
4140 ephemeralEndDefault = 60999 ;
4241 }
4342
44- EPHEMERAL_START = Integer .getInteger ("dnsjava.udp.ephemeral.start" , ephemeralStartDefault );
43+ ephemeralStart = Integer .getInteger ("dnsjava.udp.ephemeral.start" , ephemeralStartDefault );
4544 int ephemeralEnd = Integer .getInteger ("dnsjava.udp.ephemeral.end" , ephemeralEndDefault );
46- EPHEMERAL_RANGE = ephemeralEnd - EPHEMERAL_START ;
45+ ephemeralRange = ephemeralEnd - ephemeralStart ;
4746
4847 if (Boolean .getBoolean ("dnsjava.udp.ephemeral.use_ephemeral_port" )) {
4948 prng = null ;
5049 } else {
5150 prng = new SecureRandom ();
5251 }
53- setRegistrationsTask (NioUdpClient ::processPendingRegistrations , false );
54- setTimeoutTask (NioUdpClient ::checkTransactionTimeouts , false );
55- setCloseTask (NioUdpClient ::closeUdp , false );
52+ setRegistrationsTask (this ::processPendingRegistrations , false );
53+ setTimeoutTask (this ::checkTransactionTimeouts , false );
54+ setCloseTask (this ::closeUdp , false );
5655 }
5756
58- private static void processPendingRegistrations () {
57+ private void processPendingRegistrations () {
5958 while (!registrationQueue .isEmpty ()) {
6059 Transaction t = registrationQueue .remove ();
6160 try {
@@ -68,7 +67,7 @@ private static void processPendingRegistrations() {
6867 }
6968 }
7069
71- private static void checkTransactionTimeouts () {
70+ private void checkTransactionTimeouts () {
7271 for (Iterator <Transaction > it = pendingTransactions .iterator (); it .hasNext (); ) {
7372 Transaction t = it .next ();
7473 if (t .endTime - System .nanoTime () < 0 ) {
@@ -79,7 +78,7 @@ private static void checkTransactionTimeouts() {
7978 }
8079
8180 @ RequiredArgsConstructor
82- private static class Transaction implements KeyProcessor {
81+ private class Transaction implements KeyProcessor {
8382 private final int id ;
8483 private final byte [] data ;
8584 private final int max ;
@@ -159,7 +158,8 @@ private void silentCloseChannel() {
159158 }
160159 }
161160
162- static CompletableFuture <byte []> sendrecv (
161+ @ Override
162+ public CompletableFuture <byte []> sendAndReceiveUdp (
163163 InetSocketAddress local ,
164164 InetSocketAddress remote ,
165165 Message query ,
@@ -182,12 +182,12 @@ static CompletableFuture<byte[]> sendrecv(
182182 InetSocketAddress addr = null ;
183183 if (local == null ) {
184184 if (prng != null ) {
185- addr = new InetSocketAddress (prng .nextInt (EPHEMERAL_RANGE ) + EPHEMERAL_START );
185+ addr = new InetSocketAddress (prng .nextInt (ephemeralRange ) + ephemeralStart );
186186 }
187187 } else {
188188 int port = local .getPort ();
189189 if (port == 0 && prng != null ) {
190- port = prng .nextInt (EPHEMERAL_RANGE ) + EPHEMERAL_START ;
190+ port = prng .nextInt (ephemeralRange ) + ephemeralStart ;
191191 }
192192
193193 addr = new InetSocketAddress (local .getAddress (), port );
@@ -225,7 +225,7 @@ static CompletableFuture<byte[]> sendrecv(
225225 return f ;
226226 }
227227
228- private static void closeUdp () {
228+ private void closeUdp () {
229229 registrationQueue .clear ();
230230 EOFException closing = new EOFException ("Client is closing" );
231231 pendingTransactions .forEach (t -> t .completeExceptionally (closing ));
0 commit comments