Search in sources :

Example 1 with AddressResolver

use of io.vertx.core.impl.AddressResolver in project vert.x by eclipse.

the class DatagramSocketImpl method listen.

private Future<DatagramSocket> listen(SocketAddress local) {
    AddressResolver resolver = context.owner().addressResolver();
    PromiseInternal<Void> promise = context.promise();
    io.netty.util.concurrent.Future<InetSocketAddress> f1 = resolver.resolveHostname(context.nettyEventLoop(), local.host());
    f1.addListener((GenericFutureListener<io.netty.util.concurrent.Future<InetSocketAddress>>) res1 -> {
        if (res1.isSuccess()) {
            ChannelFuture f2 = channel.bind(new InetSocketAddress(res1.getNow().getAddress(), local.port()));
            if (metrics != null) {
                f2.addListener((GenericFutureListener<io.netty.util.concurrent.Future<Void>>) res2 -> {
                    if (res2.isSuccess()) {
                        metrics.listening(local.host(), localAddress());
                    }
                });
            }
            f2.addListener(promise);
        } else {
            promise.fail(res1.cause());
        }
    });
    return promise.future().map(this);
}
Also used : ChannelOption(io.netty.channel.ChannelOption) LoggingHandler(io.netty.handler.logging.LoggingHandler) DatagramSocket(io.vertx.core.datagram.DatagramSocket) Arguments(io.vertx.core.impl.Arguments) ContextInternal(io.vertx.core.impl.ContextInternal) ConnectionBase(io.vertx.core.net.impl.ConnectionBase) InetAddress(java.net.InetAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DatagramChannel(io.netty.channel.socket.DatagramChannel) ByteBuf(io.netty.buffer.ByteBuf) WriteStream(io.vertx.core.streams.WriteStream) DatagramPacket(io.netty.channel.socket.DatagramPacket) InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) AsyncResult(io.vertx.core.AsyncResult) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MaxMessagesRecvByteBufAllocator(io.netty.channel.MaxMessagesRecvByteBufAllocator) SocketAddress(io.vertx.core.net.SocketAddress) VertxHandler(io.vertx.core.net.impl.VertxHandler) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) VertxInternal(io.vertx.core.impl.VertxInternal) Transport(io.vertx.core.net.impl.transport.Transport) AddressResolver(io.vertx.core.impl.AddressResolver) NetworkInterface(java.net.NetworkInterface) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Future(io.vertx.core.Future) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) ChannelFuture(io.netty.channel.ChannelFuture) Nullable(io.vertx.codegen.annotations.Nullable) Objects(java.util.Objects) Buffer(io.vertx.core.buffer.Buffer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) Handler(io.vertx.core.Handler) ChannelFuture(io.netty.channel.ChannelFuture) AddressResolver(io.vertx.core.impl.AddressResolver) InetSocketAddress(java.net.InetSocketAddress) Future(io.vertx.core.Future) ChannelFuture(io.netty.channel.ChannelFuture) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener)

Example 2 with AddressResolver

use of io.vertx.core.impl.AddressResolver in project vert.x by eclipse.

the class HostnameResolutionTest method testServerSelection.

private void testServerSelection(boolean rotateServers, boolean cache) throws Exception {
    int num = VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE + 4;
    List<FakeDNSServer> dnsServers = new ArrayList<>();
    try {
        for (int index = 1; index <= num; index++) {
            FakeDNSServer server = new FakeDNSServer().store(FakeDNSServer.A_store(Collections.singletonMap("vertx.io", "127.0.0." + index)));
            server.port(FakeDNSServer.PORT + index);
            server.start();
            dnsServers.add(server);
        }
        AddressResolverOptions options = new AddressResolverOptions();
        options.setRotateServers(rotateServers);
        options.setOptResourceEnabled(false);
        if (!cache) {
            options.setCacheMaxTimeToLive(0);
        }
        for (int i = 0; i < num; i++) {
            InetSocketAddress dnsServerAddress = dnsServers.get(i).localAddress();
            options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort());
        }
        AddressResolver resolver = new AddressResolver(vertx, options);
        for (int i = 0; i < num; i++) {
            CompletableFuture<InetAddress> result = new CompletableFuture<>();
            resolver.resolveHostname("vertx.io", ar -> {
                if (ar.succeeded()) {
                    result.complete(ar.result());
                } else {
                    result.completeExceptionally(ar.cause());
                }
            });
            String resolved = result.get(10, TimeUnit.SECONDS).getHostAddress();
            int expected;
            if (rotateServers && !cache) {
                expected = 1 + i;
            } else {
                expected = 1;
            }
            assertEquals("127.0.0." + expected, resolved);
        }
    } finally {
        dnsServers.forEach(FakeDNSServer::stop);
    }
}
Also used : AddressResolver(io.vertx.core.impl.AddressResolver) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer)

Example 3 with AddressResolver

use of io.vertx.core.impl.AddressResolver in project vert.x by eclipse.

the class HostnameResolutionTest method testResolveLocalhost.

@Test
public void testResolveLocalhost() {
    AddressResolver resolver = new AddressResolver((VertxImpl) vertx, new AddressResolverOptions());
    resolver.resolveHostname("LOCALHOST", res -> {
        if (res.succeeded()) {
            assertEquals("localhost", res.result().getHostName().toLowerCase(Locale.ENGLISH));
            resolver.resolveHostname("LocalHost", res2 -> {
                if (res2.succeeded()) {
                    assertEquals("localhost", res2.result().getHostName().toLowerCase(Locale.ENGLISH));
                    resolver.resolveHostname("localhost", res3 -> {
                        if (res3.succeeded()) {
                            assertEquals("localhost", res3.result().getHostName().toLowerCase(Locale.ENGLISH));
                            testComplete();
                        } else {
                            fail(res3.cause());
                        }
                    });
                } else {
                    fail(res2.cause());
                }
            });
        } else {
            fail(res.cause());
        }
    });
    await();
}
Also used : AddressResolver(io.vertx.core.impl.AddressResolver) Test(org.junit.Test)

Example 4 with AddressResolver

use of io.vertx.core.impl.AddressResolver in project vert.x by eclipse.

the class HostnameResolutionTest method testServerFailover.

@Test
public void testServerFailover() throws Exception {
    FakeDNSServer server = new FakeDNSServer().store(FakeDNSServer.A_store(Collections.singletonMap("vertx.io", "127.0.0.1"))).port(FakeDNSServer.PORT + 2);
    try {
        AddressResolverOptions options = new AddressResolverOptions();
        options.setOptResourceEnabled(false);
        // 2 + 2
        options.setMaxQueries(4);
        server.start();
        InetSocketAddress dnsServerAddress = server.localAddress();
        // First server is unreachable
        options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + (FakeDNSServer.PORT + 1));
        // Second server is the failed over server
        options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort());
        AddressResolver resolver = new AddressResolver((VertxImpl) vertx, options);
        CompletableFuture<InetAddress> result = new CompletableFuture<>();
        resolver.resolveHostname("vertx.io", ar -> {
            if (ar.succeeded()) {
                result.complete(ar.result());
            } else {
                result.completeExceptionally(ar.cause());
            }
        });
        String resolved = result.get(10, TimeUnit.SECONDS).getHostAddress();
        assertEquals("127.0.0.1", resolved);
    } finally {
        server.stop();
    }
}
Also used : AddressResolver(io.vertx.core.impl.AddressResolver) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) Test(org.junit.Test)

Example 5 with AddressResolver

use of io.vertx.core.impl.AddressResolver in project vert.x by eclipse.

the class HostnameResolutionTest method testServerSelection.

private void testServerSelection(boolean rotateServers) throws Exception {
    int num = VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE + 4;
    List<FakeDNSServer> dnsServers = new ArrayList<>();
    try {
        for (int index = 1; index <= num; index++) {
            FakeDNSServer server = new FakeDNSServer(FakeDNSServer.A_store(Collections.singletonMap("vertx.io", "127.0.0." + index)));
            server.port(FakeDNSServer.PORT + index);
            server.start();
            dnsServers.add(server);
        }
        AddressResolverOptions options = new AddressResolverOptions();
        options.setRotateServers(rotateServers);
        options.setOptResourceEnabled(false);
        for (int i = 0; i < num; i++) {
            InetSocketAddress dnsServerAddress = dnsServers.get(i).localAddress();
            options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort());
        }
        AddressResolver resolver = new AddressResolver((VertxImpl) vertx, options);
        for (int i = 0; i < num; i++) {
            CompletableFuture<InetAddress> result = new CompletableFuture<>();
            resolver.resolveHostname("vertx.io", ar -> {
                if (ar.succeeded()) {
                    result.complete(ar.result());
                } else {
                    result.completeExceptionally(ar.cause());
                }
            });
            String resolved = result.get(10, TimeUnit.SECONDS).getHostAddress();
            int expected;
            if (rotateServers) {
                // Expect from [1,...,DEFAULT_EVENT_LOOP_POOL_SIZE[ as we round robin but then we cache the result
                // so it checks that round robin cross event loops
                expected = 1 + i % VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE;
            } else {
                expected = 1;
            }
            assertEquals("127.0.0." + expected, resolved);
        }
    } finally {
        dnsServers.forEach(FakeDNSServer::stop);
    }
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AddressResolver(io.vertx.core.impl.AddressResolver) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer)

Aggregations

AddressResolver (io.vertx.core.impl.AddressResolver)9 InetAddress (java.net.InetAddress)7 InetSocketAddress (java.net.InetSocketAddress)7 FakeDNSServer (io.vertx.test.fakedns.FakeDNSServer)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 Test (org.junit.Test)5 ChannelFuture (io.netty.channel.ChannelFuture)3 Buffer (io.vertx.core.buffer.Buffer)3 AddressResolverOptions (io.vertx.core.dns.AddressResolverOptions)3 VertxInternal (io.vertx.core.impl.VertxInternal)3 UnknownHostException (java.net.UnknownHostException)3 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelOption (io.netty.channel.ChannelOption)2 MaxMessagesRecvByteBufAllocator (io.netty.channel.MaxMessagesRecvByteBufAllocator)2 DatagramChannel (io.netty.channel.socket.DatagramChannel)2 DatagramPacket (io.netty.channel.socket.DatagramPacket)2 InternetProtocolFamily (io.netty.channel.socket.InternetProtocolFamily)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)2