Example usage for io.netty.resolver.dns DnsAddressResolverGroup DnsAddressResolverGroup

List of usage examples for io.netty.resolver.dns DnsAddressResolverGroup DnsAddressResolverGroup

Introduction

In this page you can find the example usage for io.netty.resolver.dns DnsAddressResolverGroup DnsAddressResolverGroup.

Prototype

public DnsAddressResolverGroup(ChannelFactory<? extends DatagramChannel> channelFactory,
            DnsServerAddressStreamProvider nameServerProvider) 

Source Link

Usage

From source file:com.linecorp.armeria.client.RemoteInvokerFactory.java

License:Apache License

private RemoteInvokerFactory(RemoteInvokerOptions options,
        Function<TransportType, ThreadFactory> threadFactoryFactory) {

    requireNonNull(options, "options");
    requireNonNull(threadFactoryFactory, "threadFactoryFactory");

    final Bootstrap baseBootstrap = new Bootstrap();

    baseBootstrap.channel(channelType());
    baseBootstrap.resolver(options.addressResolverGroup().orElseGet(
            () -> new DnsAddressResolverGroup(datagramChannelType(), DnsServerAddresses.defaultAddresses())));

    baseBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            ConvertUtils.safeLongToInt(options.connectTimeoutMillis()));
    baseBootstrap.option(ChannelOption.SO_KEEPALIVE, true);

    final Optional<EventLoopGroup> eventLoopOption = options.eventLoopGroup();
    if (eventLoopOption.isPresent()) {
        eventLoopGroup = eventLoopOption.get();
        closeEventLoopGroup = false;/*from   w  ww.j  ava2  s .c  om*/
    } else {
        eventLoopGroup = createGroup(threadFactoryFactory);
        closeEventLoopGroup = true;
    }

    final EnumMap<SessionProtocol, RemoteInvoker> remoteInvokers = new EnumMap<>(SessionProtocol.class);
    final HttpRemoteInvoker remoteInvoker = new HttpRemoteInvoker(baseBootstrap, options);

    SessionProtocol.ofHttp().forEach(protocol -> remoteInvokers.put(protocol, remoteInvoker));

    this.remoteInvokers = Collections.unmodifiableMap(remoteInvokers);
}

From source file:io.vertx.core.impl.resolver.DnsResolverProvider.java

License:Open Source License

public DnsResolverProvider(VertxImpl vertx, AddressResolverOptions options) {
    List<String> dnsServers = options.getServers();
    List<InetSocketAddress> serverList = new ArrayList<>();
    if (dnsServers != null && dnsServers.size() > 0) {
        for (String dnsServer : dnsServers) {
            int sep = dnsServer.indexOf(':');
            String ipAddress;//from w  w w.ja v a 2 s.  c  o m
            int port;
            if (sep != -1) {
                ipAddress = dnsServer.substring(0, sep);
                port = Integer.parseInt(dnsServer.substring(sep + 1));
            } else {
                ipAddress = dnsServer;
                port = 53;
            }
            try {
                serverList.add(new InetSocketAddress(
                        InetAddress.getByAddress(NetUtil.createByteArrayFromIpAddressString(ipAddress)), port));
            } catch (UnknownHostException e) {
                throw new VertxException(e);
            }
        }
    } else {
        DnsServerAddressStream stream = DnsServerAddresses.defaultAddresses().stream();
        Set<InetSocketAddress> all = new HashSet<>();
        while (true) {
            InetSocketAddress address = stream.next();
            if (all.contains(address)) {
                break;
            }
            serverList.add(address);
            all.add(address);
        }
    }
    DnsServerAddresses nameServerAddresses = options.isRotateServers()
            ? DnsServerAddresses.rotational(serverList)
            : DnsServerAddresses.sequential(serverList);

    Map<String, InetAddress> entries;
    if (options.getHostsPath() != null) {
        File file = vertx.resolveFile(options.getHostsPath()).getAbsoluteFile();
        try {
            if (!file.exists() || !file.isFile()) {
                throw new IOException();
            }
            entries = HostsFileParser.parse(file);
        } catch (IOException e) {
            throw new VertxException("Cannot read hosts file " + file.getAbsolutePath());
        }
    } else if (options.getHostsValue() != null) {
        try {
            entries = HostsFileParser.parse(new StringReader(options.getHostsValue().toString()));
        } catch (IOException e) {
            throw new VertxException("Cannot read hosts config ", e);
        }
    } else {
        entries = HostsFileParser.parseSilently();
    }

    this.vertx = vertx;
    this.resolverGroup = new AddressResolverGroup<InetSocketAddress>() {

        @Override
        protected io.netty.resolver.AddressResolver<InetSocketAddress> newResolver(EventExecutor executor)
                throws Exception {

            DnsAddressResolverGroup group = new DnsAddressResolverGroup(NioDatagramChannel.class,
                    nameServerAddresses) {
                @Override
                protected NameResolver<InetAddress> newNameResolver(EventLoop eventLoop,
                        ChannelFactory<? extends DatagramChannel> channelFactory,
                        DnsServerAddresses nameServerAddresses) throws Exception {
                    DnsNameResolverBuilder builder = new DnsNameResolverBuilder((EventLoop) executor);
                    builder.hostsFileEntriesResolver(inetHost -> {
                        InetAddress addr = entries.get(inetHost);
                        if (addr == null) {
                            addr = entries.get(inetHost.toLowerCase(Locale.ENGLISH));
                        }
                        return addr;
                    });
                    builder.channelType(NioDatagramChannel.class);
                    builder.nameServerAddresses(nameServerAddresses);
                    builder.optResourceEnabled(options.isOptResourceEnabled());
                    builder.ttl(options.getCacheMinTimeToLive(), options.getCacheMaxTimeToLive());
                    builder.negativeTtl(options.getCacheNegativeTimeToLive());
                    builder.queryTimeoutMillis(options.getQueryTimeout());
                    builder.maxQueriesPerResolve(options.getMaxQueries());
                    builder.recursionDesired(options.getRdFlag());
                    if (options.getSearchDomains() != null) {
                        builder.searchDomains(options.getSearchDomains());
                        int ndots = options.getNdots();
                        if (ndots == -1) {
                            ndots = AddressResolver.DEFAULT_NDOTS_RESOLV_OPTION;
                        }
                        builder.ndots(ndots);
                    }
                    return builder.build();
                }
            };

            io.netty.resolver.AddressResolver<InetSocketAddress> resolver = group.getResolver(executor);
            resolvers.add(new ResolverRegistration(resolver, (EventLoop) executor));

            return resolver;
        }
    };
}

From source file:org.redisson.connection.DnsAddressResolverGroupFactory.java

License:Apache License

@Override
public DnsAddressResolverGroup create(Class<? extends DatagramChannel> channelType,
        DnsServerAddressStreamProvider nameServerProvider) {
    return new DnsAddressResolverGroup(channelType, nameServerProvider);
}