2014-09-19 15:36:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.netty.resolver.dns;
|
|
|
|
|
|
|
|
import io.netty.channel.ChannelFactory;
|
|
|
|
import io.netty.channel.EventLoop;
|
|
|
|
import io.netty.channel.ReflectiveChannelFactory;
|
|
|
|
import io.netty.channel.socket.DatagramChannel;
|
2015-12-13 00:11:59 +01:00
|
|
|
import io.netty.resolver.AddressResolver;
|
|
|
|
import io.netty.resolver.AddressResolverGroup;
|
2016-05-17 12:58:08 +02:00
|
|
|
import io.netty.resolver.InetSocketAddressResolver;
|
|
|
|
import io.netty.resolver.NameResolver;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.util.concurrent.EventExecutor;
|
2016-05-17 12:58:08 +02:00
|
|
|
import io.netty.util.concurrent.Promise;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.util.internal.StringUtil;
|
2016-04-12 14:22:41 +02:00
|
|
|
import io.netty.util.internal.UnstableApi;
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2016-05-17 12:58:08 +02:00
|
|
|
import java.net.InetAddress;
|
2014-09-19 15:36:32 +02:00
|
|
|
import java.net.InetSocketAddress;
|
2016-05-17 12:58:08 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.ConcurrentMap;
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2016-05-17 12:58:08 +02:00
|
|
|
import static io.netty.util.internal.PlatformDependent.newConcurrentHashMap;
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
/**
|
2015-12-13 00:11:59 +01:00
|
|
|
* A {@link AddressResolverGroup} of {@link DnsNameResolver}s.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2016-04-12 14:22:41 +02:00
|
|
|
@UnstableApi
|
2015-12-13 00:11:59 +01:00
|
|
|
public class DnsAddressResolverGroup extends AddressResolverGroup<InetSocketAddress> {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
private final ChannelFactory<? extends DatagramChannel> channelFactory;
|
2015-08-19 04:51:15 +02:00
|
|
|
private final DnsServerAddresses nameServerAddresses;
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2016-05-17 12:58:08 +02:00
|
|
|
private final ConcurrentMap<String, Promise<InetAddress>> resolvesInProgress = newConcurrentHashMap();
|
|
|
|
private final ConcurrentMap<String, Promise<List<InetAddress>>> resolveAllsInProgress = newConcurrentHashMap();
|
|
|
|
|
2015-12-13 00:11:59 +01:00
|
|
|
public DnsAddressResolverGroup(
|
2014-09-19 15:36:32 +02:00
|
|
|
Class<? extends DatagramChannel> channelType,
|
2016-06-28 11:44:04 +02:00
|
|
|
DnsServerAddresses nameServerAddresses) {
|
|
|
|
this(new ReflectiveChannelFactory<DatagramChannel>(channelType), nameServerAddresses);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:11:59 +01:00
|
|
|
public DnsAddressResolverGroup(
|
2014-09-19 15:36:32 +02:00
|
|
|
ChannelFactory<? extends DatagramChannel> channelFactory,
|
2016-06-28 11:44:04 +02:00
|
|
|
DnsServerAddresses nameServerAddresses) {
|
2014-09-19 15:36:32 +02:00
|
|
|
this.channelFactory = channelFactory;
|
|
|
|
this.nameServerAddresses = nameServerAddresses;
|
|
|
|
}
|
|
|
|
|
2016-05-31 08:39:28 +02:00
|
|
|
@SuppressWarnings("deprecation")
|
2014-09-19 15:36:32 +02:00
|
|
|
@Override
|
2015-12-13 00:11:59 +01:00
|
|
|
protected final AddressResolver<InetSocketAddress> newResolver(EventExecutor executor) throws Exception {
|
2014-09-19 15:36:32 +02:00
|
|
|
if (!(executor instanceof EventLoop)) {
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"unsupported executor type: " + StringUtil.simpleClassName(executor) +
|
|
|
|
" (expected: " + StringUtil.simpleClassName(EventLoop.class));
|
|
|
|
}
|
|
|
|
|
2016-06-28 11:44:04 +02:00
|
|
|
return newResolver((EventLoop) executor, channelFactory, nameServerAddresses);
|
2015-12-07 10:43:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-28 11:44:04 +02:00
|
|
|
* @deprecated Override {@link #newNameResolver(EventLoop, ChannelFactory, DnsServerAddresses)}.
|
2015-12-07 10:43:32 +01:00
|
|
|
*/
|
2016-05-31 08:39:28 +02:00
|
|
|
@Deprecated
|
2015-12-13 00:11:59 +01:00
|
|
|
protected AddressResolver<InetSocketAddress> newResolver(
|
2015-12-07 10:43:32 +01:00
|
|
|
EventLoop eventLoop, ChannelFactory<? extends DatagramChannel> channelFactory,
|
2016-06-28 11:44:04 +02:00
|
|
|
DnsServerAddresses nameServerAddresses) throws Exception {
|
2015-12-07 10:43:32 +01:00
|
|
|
|
2016-05-17 12:58:08 +02:00
|
|
|
final NameResolver<InetAddress> resolver = new InflightNameResolver<InetAddress>(
|
|
|
|
eventLoop,
|
2016-06-28 11:44:04 +02:00
|
|
|
newNameResolver(eventLoop, channelFactory, nameServerAddresses),
|
2016-05-17 12:58:08 +02:00
|
|
|
resolvesInProgress,
|
|
|
|
resolveAllsInProgress);
|
|
|
|
|
|
|
|
return new InetSocketAddressResolver(eventLoop, resolver);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
2016-05-31 08:39:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new {@link NameResolver}. Override this method to create an alternative {@link NameResolver}
|
|
|
|
* implementation or override the default configuration.
|
|
|
|
*/
|
|
|
|
protected NameResolver<InetAddress> newNameResolver(EventLoop eventLoop,
|
|
|
|
ChannelFactory<? extends DatagramChannel> channelFactory,
|
|
|
|
DnsServerAddresses nameServerAddresses) throws Exception {
|
|
|
|
return new DnsNameResolverBuilder(eventLoop)
|
|
|
|
.channelFactory(channelFactory)
|
|
|
|
.nameServerAddresses(nameServerAddresses)
|
|
|
|
.build();
|
|
|
|
}
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|