1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty5.resolver.dns;
18
19 import io.netty5.channel.ChannelFactory;
20 import io.netty5.channel.EventLoop;
21 import io.netty5.channel.socket.DatagramChannel;
22 import io.netty5.resolver.AddressResolver;
23 import io.netty5.resolver.AddressResolverGroup;
24 import io.netty5.resolver.InetSocketAddressResolver;
25 import io.netty5.resolver.NameResolver;
26 import io.netty5.util.concurrent.EventExecutor;
27 import io.netty5.util.concurrent.Promise;
28 import io.netty5.util.internal.StringUtil;
29
30 import java.net.InetAddress;
31 import java.net.InetSocketAddress;
32 import java.util.List;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.ConcurrentMap;
35
36
37
38
39 public class DnsAddressResolverGroup extends AddressResolverGroup<InetSocketAddress> {
40
41 private final DnsNameResolverBuilder dnsResolverBuilder;
42
43 private final ConcurrentMap<String, Promise<InetAddress>> resolvesInProgress = new ConcurrentHashMap<>();
44 private final ConcurrentMap<String, Promise<List<InetAddress>>> resolveAllsInProgress = new ConcurrentHashMap<>();
45
46 public DnsAddressResolverGroup(DnsNameResolverBuilder dnsResolverBuilder) {
47 this.dnsResolverBuilder = dnsResolverBuilder.copy();
48 }
49
50 public DnsAddressResolverGroup(
51 Class<? extends DatagramChannel> channelType,
52 DnsServerAddressStreamProvider nameServerProvider) {
53 this.dnsResolverBuilder = new DnsNameResolverBuilder();
54 dnsResolverBuilder.channelType(channelType).nameServerProvider(nameServerProvider);
55 }
56
57 public DnsAddressResolverGroup(
58 ChannelFactory<? extends DatagramChannel> channelFactory,
59 DnsServerAddressStreamProvider nameServerProvider) {
60 this.dnsResolverBuilder = new DnsNameResolverBuilder();
61 dnsResolverBuilder.channelFactory(channelFactory).nameServerProvider(nameServerProvider);
62 }
63
64 @SuppressWarnings("deprecation")
65 @Override
66 protected final AddressResolver<InetSocketAddress> newResolver(EventExecutor executor) throws Exception {
67 if (!(executor instanceof EventLoop)) {
68 throw new IllegalStateException(
69 "unsupported executor type: " + StringUtil.simpleClassName(executor) +
70 " (expected: " + StringUtil.simpleClassName(EventLoop.class));
71 }
72
73
74
75 EventLoop loop = dnsResolverBuilder.eventLoop;
76 return newResolver(loop == null ? (EventLoop) executor : loop,
77 dnsResolverBuilder.channelFactory(),
78 dnsResolverBuilder.nameServerProvider());
79 }
80
81
82
83
84 @Deprecated
85 protected AddressResolver<InetSocketAddress> newResolver(
86 EventLoop eventLoop, ChannelFactory<? extends DatagramChannel> channelFactory,
87 DnsServerAddressStreamProvider nameServerProvider) throws Exception {
88
89 final NameResolver<InetAddress> resolver = new InflightNameResolver<>(
90 eventLoop,
91 newNameResolver(eventLoop, channelFactory, nameServerProvider),
92 resolvesInProgress,
93 resolveAllsInProgress);
94
95 return newAddressResolver(eventLoop, resolver);
96 }
97
98
99
100
101
102 protected NameResolver<InetAddress> newNameResolver(EventLoop eventLoop,
103 ChannelFactory<? extends DatagramChannel> channelFactory,
104 DnsServerAddressStreamProvider nameServerProvider)
105 throws Exception {
106 DnsNameResolverBuilder builder = dnsResolverBuilder.copy();
107
108
109
110 return builder.eventLoop(eventLoop)
111 .channelFactory(channelFactory)
112 .nameServerProvider(nameServerProvider)
113 .build();
114 }
115
116
117
118
119
120 protected AddressResolver<InetSocketAddress> newAddressResolver(EventLoop eventLoop,
121 NameResolver<InetAddress> resolver)
122 throws Exception {
123 return new InetSocketAddressResolver(eventLoop, resolver);
124 }
125 }