Search in sources :

Example 71 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse-vertx.

the class TCPServerBase method listen.

private synchronized io.netty.util.concurrent.Future<Channel> listen(SocketAddress localAddress, ContextInternal context) {
    if (listening) {
        throw new IllegalStateException("Listen already called");
    }
    this.listenContext = context;
    this.listening = true;
    this.eventLoop = context.nettyEventLoop();
    SocketAddress bindAddress;
    Map<ServerID, TCPServerBase> sharedNetServers = vertx.sharedTCPServers((Class<TCPServerBase>) getClass());
    synchronized (sharedNetServers) {
        actualPort = localAddress.port();
        String hostOrPath = localAddress.isInetSocket() ? localAddress.host() : localAddress.path();
        TCPServerBase main;
        boolean shared;
        ServerID id;
        if (actualPort > 0 || localAddress.isDomainSocket()) {
            id = new ServerID(actualPort, hostOrPath);
            main = sharedNetServers.get(id);
            shared = true;
            bindAddress = localAddress;
        } else {
            if (actualPort < 0) {
                id = new ServerID(actualPort, hostOrPath + "/" + -actualPort);
                main = sharedNetServers.get(id);
                shared = true;
                bindAddress = SocketAddress.inetSocketAddress(0, localAddress.host());
            } else {
                id = new ServerID(actualPort, hostOrPath);
                main = null;
                shared = false;
                bindAddress = localAddress;
            }
        }
        if (main == null) {
            try {
                sslHelper = createSSLHelper();
                sslHelper.validate(vertx);
                worker = childHandler(listenContext, localAddress, sslHelper);
                servers = new HashSet<>();
                servers.add(this);
                channelBalancer = new ServerChannelLoadBalancer(vertx.getAcceptorEventLoopGroup().next());
                channelBalancer.addWorker(eventLoop, worker);
                ServerBootstrap bootstrap = new ServerBootstrap();
                bootstrap.group(vertx.getAcceptorEventLoopGroup(), channelBalancer.workers());
                if (sslHelper.isSSL()) {
                    bootstrap.childOption(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
                } else {
                    bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
                }
                bootstrap.childHandler(channelBalancer);
                applyConnectionOptions(localAddress.isDomainSocket(), bootstrap);
                bindFuture = AsyncResolveConnectHelper.doBind(vertx, bindAddress, bootstrap);
                bindFuture.addListener((GenericFutureListener<io.netty.util.concurrent.Future<Channel>>) res -> {
                    if (res.isSuccess()) {
                        Channel ch = res.getNow();
                        log.trace("Net server listening on " + hostOrPath + ":" + ch.localAddress());
                        if (shared) {
                            ch.closeFuture().addListener((ChannelFutureListener) channelFuture -> {
                                synchronized (sharedNetServers) {
                                    sharedNetServers.remove(id);
                                }
                            });
                        }
                        if (bindAddress.isInetSocket()) {
                            actualPort = ((InetSocketAddress) ch.localAddress()).getPort();
                        }
                        listenContext.addCloseHook(this);
                        metrics = createMetrics(localAddress);
                    } else {
                        if (shared) {
                            synchronized (sharedNetServers) {
                                sharedNetServers.remove(id);
                            }
                        }
                        listening = false;
                    }
                });
            } catch (Throwable t) {
                listening = false;
                return vertx.getAcceptorEventLoopGroup().next().newFailedFuture(t);
            }
            if (shared) {
                sharedNetServers.put(id, this);
            }
            actualServer = this;
        } else {
            // Server already exists with that host/port - we will use that
            actualServer = main;
            metrics = main.metrics;
            sslHelper = main.sslHelper;
            worker = childHandler(listenContext, localAddress, sslHelper);
            actualServer.servers.add(this);
            actualServer.channelBalancer.addWorker(eventLoop, worker);
            listenContext.addCloseHook(this);
        }
    }
    return actualServer.bindFuture;
}
Also used : ChannelOption(io.netty.channel.ChannelOption) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) ContextInternal(io.vertx.core.impl.ContextInternal) TCPMetrics(io.vertx.core.spi.metrics.TCPMetrics) Context(io.vertx.core.Context) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) SocketAddress(io.vertx.core.net.SocketAddress) Logger(io.vertx.core.impl.logging.Logger) Closeable(io.vertx.core.Closeable) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) VertxInternal(io.vertx.core.impl.VertxInternal) Promise(io.vertx.core.Promise) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Set(java.util.Set) MetricsProvider(io.vertx.core.spi.metrics.MetricsProvider) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) EventLoop(io.netty.channel.EventLoop) PartialPooledByteBufAllocator(io.vertx.core.buffer.impl.PartialPooledByteBufAllocator) Future(io.vertx.core.Future) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Handler(io.vertx.core.Handler) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) ChannelFuture(io.netty.channel.ChannelFuture) SocketAddress(io.vertx.core.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 72 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse-vertx.

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) Closeable(io.vertx.core.Closeable) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) VertxInternal(io.vertx.core.impl.VertxInternal) Transport(io.vertx.core.net.impl.transport.Transport) Promise(io.vertx.core.Promise) 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) CloseFuture(io.vertx.core.impl.CloseFuture) 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) CloseFuture(io.vertx.core.impl.CloseFuture) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener)

Example 73 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse-vertx.

the class VertxImpl method deleteCacheDirAndShutdown.

@SuppressWarnings("unchecked")
private void deleteCacheDirAndShutdown(Handler<AsyncResult<Void>> completionHandler) {
    executeBlockingInternal(fut -> {
        try {
            fileResolver.close();
            fut.complete();
        } catch (IOException e) {
            fut.tryFail(e);
        }
    }, ar -> {
        workerPool.close();
        internalWorkerPool.close();
        new ArrayList<>(namedWorkerPools.values()).forEach(WorkerPool::close);
        acceptorEventLoopGroup.shutdownGracefully(0, 10, TimeUnit.SECONDS).addListener(new GenericFutureListener() {

            @Override
            public void operationComplete(io.netty.util.concurrent.Future future) throws Exception {
                if (!future.isSuccess()) {
                    log.warn("Failure in shutting down acceptor event loop group", future.cause());
                }
                eventLoopGroup.shutdownGracefully(0, 10, TimeUnit.SECONDS).addListener(new GenericFutureListener() {

                    @Override
                    public void operationComplete(io.netty.util.concurrent.Future future) throws Exception {
                        if (!future.isSuccess()) {
                            log.warn("Failure in shutting down event loop group", future.cause());
                        }
                        if (metrics != null) {
                            metrics.close();
                        }
                        if (tracer != null) {
                            tracer.close();
                        }
                        checker.close();
                        if (completionHandler != null) {
                            eventLoopThreadFactory.newThread(() -> {
                                completionHandler.handle(Future.succeededFuture());
                            }).start();
                        }
                    }
                });
            }
        });
    });
}
Also used : java.util.concurrent(java.util.concurrent) Future(io.vertx.core.Future) IOException(java.io.IOException) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) IOException(java.io.IOException)

Aggregations

GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)70 Future (io.netty.util.concurrent.Future)44 ChannelFuture (io.netty.channel.ChannelFuture)32 Channel (io.netty.channel.Channel)19 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)18 IOException (java.io.IOException)18 List (java.util.List)15 InetSocketAddress (java.net.InetSocketAddress)13 ArrayList (java.util.ArrayList)13 Map (java.util.Map)12 ChannelOption (io.netty.channel.ChannelOption)10 Future (io.vertx.core.Future)10 Handler (io.vertx.core.Handler)10 ContextInternal (io.vertx.core.impl.ContextInternal)10 VertxInternal (io.vertx.core.impl.VertxInternal)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 LoggingHandler (io.netty.handler.logging.LoggingHandler)8 AsyncResult (io.vertx.core.AsyncResult)8