Search in sources :

Example 76 with VertxInternal

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

the class ClusteredEventBusTest method testSubsRemovedForKilledNode.

@Test
public void testSubsRemovedForKilledNode() throws Exception {
    testSubsRemoved(latch -> {
        VertxInternal vi = (VertxInternal) vertices[1];
        Promise<Void> promise = vi.getOrCreateContext().promise();
        vi.getClusterManager().leave(promise);
        promise.future().onComplete(onSuccess(v -> {
            latch.countDown();
        }));
    });
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) NodeSelector(io.vertx.core.spi.cluster.NodeSelector) WrappedClusterManager(io.vertx.core.spi.cluster.WrappedClusterManager) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Stream(java.util.stream.Stream) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RegistrationUpdateEvent(io.vertx.core.spi.cluster.RegistrationUpdateEvent) Cert(io.vertx.test.tls.Cert) WrappedNodeSelector(io.vertx.core.spi.cluster.WrappedNodeSelector) VertxInternal(io.vertx.core.impl.VertxInternal) Test(org.junit.Test)

Example 77 with VertxInternal

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

the class LocalEventBusTest method testContextsSend.

@Test
public void testContextsSend() throws Exception {
    Set<ContextInternal> contexts = new ConcurrentHashSet<>();
    CountDownLatch latch = new CountDownLatch(2);
    vertx.eventBus().consumer(ADDRESS1).handler(msg -> {
        msg.reply("bar");
        contexts.add(((VertxInternal) vertx).getContext());
        latch.countDown();
    });
    vertx.eventBus().request(ADDRESS1, "foo", onSuccess((Message<Object> reply) -> {
        assertEquals("bar", reply.body());
        contexts.add(((VertxInternal) vertx).getContext());
        latch.countDown();
    }));
    awaitLatch(latch);
    assertEquals(2, contexts.size());
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 78 with VertxInternal

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

the class ChannelProvider method handleProxyConnect.

/**
 * A channel provider that connects via a Proxy : HTTP or SOCKS
 */
private void handleProxyConnect(Handler<Channel> handler, SocketAddress remoteAddress, SocketAddress peerAddress, String serverName, boolean ssl, boolean useAlpn, Promise<Channel> channelHandler) {
    final VertxInternal vertx = context.owner();
    final String proxyHost = proxyOptions.getHost();
    final int proxyPort = proxyOptions.getPort();
    final String proxyUsername = proxyOptions.getUsername();
    final String proxyPassword = proxyOptions.getPassword();
    final ProxyType proxyType = proxyOptions.getType();
    vertx.resolveAddress(proxyHost, dnsRes -> {
        if (dnsRes.succeeded()) {
            InetAddress address = dnsRes.result();
            InetSocketAddress proxyAddr = new InetSocketAddress(address, proxyPort);
            ProxyHandler proxy;
            switch(proxyType) {
                default:
                case HTTP:
                    proxy = proxyUsername != null && proxyPassword != null ? new HttpProxyHandler(proxyAddr, proxyUsername, proxyPassword) : new HttpProxyHandler(proxyAddr);
                    break;
                case SOCKS5:
                    proxy = proxyUsername != null && proxyPassword != null ? new Socks5ProxyHandler(proxyAddr, proxyUsername, proxyPassword) : new Socks5ProxyHandler(proxyAddr);
                    break;
                case SOCKS4:
                    // SOCKS4 only supports a username and could authenticate the user via Ident
                    proxy = proxyUsername != null ? new Socks4ProxyHandler(proxyAddr, proxyUsername) : new Socks4ProxyHandler(proxyAddr);
                    break;
            }
            bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
            java.net.SocketAddress targetAddress = vertx.transport().convert(remoteAddress);
            bootstrap.handler(new ChannelInitializer<Channel>() {

                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addFirst("proxy", proxy);
                    pipeline.addLast(new ChannelInboundHandlerAdapter() {

                        @Override
                        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                            if (evt instanceof ProxyConnectionEvent) {
                                pipeline.remove(proxy);
                                pipeline.remove(this);
                                initSSL(handler, peerAddress, serverName, ssl, useAlpn, ch, channelHandler);
                                connected(handler, ch, ssl, channelHandler);
                            }
                            ctx.fireUserEventTriggered(evt);
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                            channelHandler.setFailure(cause);
                        }
                    });
                }
            });
            ChannelFuture future = bootstrap.connect(targetAddress);
            future.addListener(res -> {
                if (!res.isSuccess()) {
                    channelHandler.setFailure(res.cause());
                }
            });
        } else {
            channelHandler.setFailure(dnsRes.cause());
        }
    });
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) InetSocketAddress(java.net.InetSocketAddress) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ProxyType(io.vertx.core.net.ProxyType) InetAddress(java.net.InetAddress)

Example 79 with VertxInternal

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

the class AsyncResolveConnectHelper method doBind.

public static io.netty.util.concurrent.Future<Channel> doBind(VertxInternal vertx, SocketAddress socketAddress, ServerBootstrap bootstrap) {
    Promise<Channel> promise = vertx.getAcceptorEventLoopGroup().next().newPromise();
    try {
        bootstrap.channelFactory(vertx.transport().serverChannelFactory(socketAddress.isDomainSocket()));
    } catch (Exception e) {
        promise.setFailure(e);
        return promise;
    }
    if (socketAddress.isDomainSocket()) {
        java.net.SocketAddress converted = vertx.transport().convert(socketAddress);
        ChannelFuture future = bootstrap.bind(converted);
        future.addListener(f -> {
            if (f.isSuccess()) {
                promise.setSuccess(future.channel());
            } else {
                promise.setFailure(f.cause());
            }
        });
    } else {
        SocketAddressImpl impl = (SocketAddressImpl) socketAddress;
        Handler<AsyncResult<InetAddress>> cont = res -> {
            if (res.succeeded()) {
                // At this point the name is an IP address so there will be no resolve hit
                InetSocketAddress t = new InetSocketAddress(res.result(), socketAddress.port());
                ChannelFuture future = bootstrap.bind(t);
                future.addListener(f -> {
                    if (f.isSuccess()) {
                        promise.setSuccess(future.channel());
                    } else {
                        promise.setFailure(f.cause());
                    }
                });
            } else {
                promise.setFailure(res.cause());
            }
        };
        if (impl.ipAddress() != null) {
            cont.handle(Future.succeededFuture(impl.ipAddress()));
        } else {
            vertx.resolveAddress(socketAddress.host(), cont);
        }
    }
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) InetAddress(java.net.InetAddress) VertxInternal(io.vertx.core.impl.VertxInternal) Promise(io.netty.util.concurrent.Promise) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) Future(io.vertx.core.Future) InetSocketAddress(java.net.InetSocketAddress) ChannelFuture(io.netty.channel.ChannelFuture) SocketAddress(io.vertx.core.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) AsyncResult(io.vertx.core.AsyncResult)

Example 80 with VertxInternal

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

the class ComplexHATest method checkDeployments.

protected void checkDeployments() {
    int totalDeployed = 0;
    for (int i = 0; i < vertices.length; i++) {
        VertxInternal v = (VertxInternal) vertices[i];
        if (!v.isKilled()) {
            totalDeployed += checkHasDeployments(i, i);
        }
    }
    assertEquals(totDeployed, totalDeployed);
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal)

Aggregations

VertxInternal (io.vertx.core.impl.VertxInternal)102 Test (org.junit.Test)73 CountDownLatch (java.util.concurrent.CountDownLatch)46 VertxOptions (io.vertx.core.VertxOptions)30 Buffer (io.vertx.core.buffer.Buffer)29 JsonObject (io.vertx.core.json.JsonObject)29 File (java.io.File)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)27 VertxException (io.vertx.core.VertxException)24 HttpClient (io.vertx.core.http.HttpClient)24 NetClient (io.vertx.core.net.NetClient)24 TimeUnit (java.util.concurrent.TimeUnit)24 NetServerOptions (io.vertx.core.net.NetServerOptions)23 InetAddress (java.net.InetAddress)23 Channel (io.netty.channel.Channel)22 InetSocketAddress (java.net.InetSocketAddress)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 NetServer (io.vertx.core.net.NetServer)21 ChannelFuture (io.netty.channel.ChannelFuture)20 Bootstrap (io.netty.bootstrap.Bootstrap)19