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