查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * A {@link AddressResolverGroup} of {@link DnsNameResolver}s.
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          // we don't really need to pass channelFactory and nameServerProvider separately,
75          // but still keep this to ensure backward compatibility with (potentially) override methods
76          EventLoop loop = dnsResolverBuilder.eventLoop;
77          return newResolver(loop == null ? (EventLoop) executor : loop,
78                  dnsResolverBuilder.channelFactory(),
79                  dnsResolverBuilder.nameServerProvider());
80      }
81  
82      /**
83       * @deprecated Override {@link #newNameResolver(EventLoop, ChannelFactory, DnsServerAddressStreamProvider)}.
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      * Creates a new {@link NameResolver}. Override this method to create an alternative {@link NameResolver}
101      * implementation or override the default configuration.
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         // once again, channelFactory and nameServerProvider are most probably set in builder already,
110         // but I do reassign them again to avoid corner cases with override methods
111         return builder.eventLoop(eventLoop)
112                 .channelFactory(channelFactory)
113                 .nameServerProvider(nameServerProvider)
114                 .build();
115     }
116 
117     /**
118      * Creates a new {@link AddressResolver}. Override this method to create an alternative {@link AddressResolver}
119      * implementation or override the default configuration.
120      */
121     protected AddressResolver<InetSocketAddress> newAddressResolver(EventLoop eventLoop,
122                                                                     NameResolver<InetAddress> resolver)
123             throws Exception {
124         return new InetSocketAddressResolver(eventLoop, resolver);
125     }
126 }