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 = dnsResolverBuilder.copy();
49 }
50
51 public DnsAddressResolverGroup(
52 Class<? extends DatagramChannel> channelType,
53 DnsServerAddressStreamProvider nameServerProvider) {
54 this.dnsResolverBuilder = new DnsNameResolverBuilder();
55 dnsResolverBuilder.channelType(channelType).nameServerProvider(nameServerProvider);
56 }
57
58 public DnsAddressResolverGroup(
59 ChannelFactory<? extends DatagramChannel> channelFactory,
60 DnsServerAddressStreamProvider nameServerProvider) {
61 this.dnsResolverBuilder = new DnsNameResolverBuilder();
62 dnsResolverBuilder.channelFactory(channelFactory).nameServerProvider(nameServerProvider);
63 }
64
65 @SuppressWarnings("deprecation")
66 @Override
67 protected final AddressResolver<InetSocketAddress> newResolver(EventExecutor executor) throws Exception {
68 if (!(executor instanceof EventLoop)) {
69 throw new IllegalStateException(
70 "unsupported executor type: " + StringUtil.simpleClassName(executor) +
71 " (expected: " + StringUtil.simpleClassName(EventLoop.class));
72 }
73
74
75
76 EventLoop loop = dnsResolverBuilder.eventLoop;
77 return newResolver(loop == null ? (EventLoop) executor : loop,
78 dnsResolverBuilder.channelFactory(),
79 dnsResolverBuilder.nameServerProvider());
80 }
81
82
83
84
85 @Deprecated
86 protected AddressResolver<InetSocketAddress> newResolver(
87 EventLoop eventLoop, ChannelFactory<? extends DatagramChannel> channelFactory,
88 DnsServerAddressStreamProvider nameServerProvider) throws Exception {
89
90 final NameResolver<InetAddress> resolver = new InflightNameResolver<InetAddress>(
91 eventLoop,
92 newNameResolver(eventLoop, channelFactory, nameServerProvider),
93 resolvesInProgress,
94 resolveAllsInProgress);
95
96 return newAddressResolver(eventLoop, resolver);
97 }
98
99
100
101
102
103 protected NameResolver<InetAddress> newNameResolver(EventLoop eventLoop,
104 ChannelFactory<? extends DatagramChannel> channelFactory,
105 DnsServerAddressStreamProvider nameServerProvider)
106 throws Exception {
107 DnsNameResolverBuilder builder = dnsResolverBuilder.copy();
108
109
110
111 return builder.eventLoop(eventLoop)
112 .channelFactory(channelFactory)
113 .nameServerProvider(nameServerProvider)
114 .build();
115 }
116
117
118
119
120
121 protected AddressResolver<InetSocketAddress> newAddressResolver(EventLoop eventLoop,
122 NameResolver<InetAddress> resolver)
123 throws Exception {
124 return new InetSocketAddressResolver(eventLoop, resolver);
125 }
126 }