Search in sources :

Example 1 with PromiseInternal

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

the class Http2ConnectionBase method close.

@Override
public Future<Void> close() {
    PromiseInternal<Void> promise = context.promise();
    ChannelPromise pr = chctx.newPromise();
    ChannelPromise channelPromise = pr.addListener(promise);
    handlerContext.writeAndFlush(Unpooled.EMPTY_BUFFER, pr);
    channelPromise.addListener((ChannelFutureListener) future -> shutdown(0L));
    return promise.future();
}
Also used : VertxException(io.vertx.core.VertxException) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) ConnectionBase(io.vertx.core.net.impl.ConnectionBase) ArrayList(java.util.ArrayList) VertxByteBufAllocator(io.vertx.core.buffer.impl.VertxByteBufAllocator) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Map(java.util.Map) Http2Stream(io.netty.handler.codec.http2.Http2Stream) AsyncResult(io.vertx.core.AsyncResult) HttpConnection(io.vertx.core.http.HttpConnection) Logger(io.vertx.core.impl.logging.Logger) io.netty.buffer(io.netty.buffer) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) VertxInternal(io.vertx.core.impl.VertxInternal) Http2Flags(io.netty.handler.codec.http2.Http2Flags) StreamPriority(io.vertx.core.http.StreamPriority) Promise(io.vertx.core.Promise) GoAway(io.vertx.core.http.GoAway) Http2FrameListener(io.netty.handler.codec.http2.Http2FrameListener) Future(io.vertx.core.Future) ChannelFuture(io.netty.channel.ChannelFuture) Http2Settings(io.netty.handler.codec.http2.Http2Settings) Nullable(io.vertx.codegen.annotations.Nullable) Objects(java.util.Objects) EventLoopContext(io.vertx.core.impl.EventLoopContext) Http2Connection(io.netty.handler.codec.http2.Http2Connection) Buffer(io.vertx.core.buffer.Buffer) Http2Headers(io.netty.handler.codec.http2.Http2Headers) Handler(io.vertx.core.Handler) ArrayDeque(java.util.ArrayDeque) ChannelPromise(io.netty.channel.ChannelPromise)

Example 2 with PromiseInternal

use of io.vertx.core.impl.future.PromiseInternal 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 3 with PromiseInternal

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

the class DatagramSocketImpl method send.

@Override
public Future<Void> send(Buffer packet, int port, String host) {
    Objects.requireNonNull(packet, "no null packet accepted");
    Objects.requireNonNull(host, "no null host accepted");
    if (port < 0 || port > 65535) {
        throw new IllegalArgumentException("port out of range:" + port);
    }
    AddressResolver resolver = context.owner().addressResolver();
    PromiseInternal<Void> promise = context.promise();
    io.netty.util.concurrent.Future<InetSocketAddress> f1 = resolver.resolveHostname(context.nettyEventLoop(), host);
    f1.addListener((GenericFutureListener<io.netty.util.concurrent.Future<InetSocketAddress>>) res1 -> {
        if (res1.isSuccess()) {
            ChannelFuture f2 = channel.writeAndFlush(new DatagramPacket(packet.getByteBuf(), new InetSocketAddress(f1.getNow().getAddress(), port)));
            if (metrics != null) {
                f2.addListener(fut -> {
                    if (fut.isSuccess()) {
                        metrics.bytesWritten(null, SocketAddress.inetSocketAddress(port, host), packet.length());
                    }
                });
            }
            f2.addListener(promise);
        } else {
            promise.fail(res1.cause());
        }
    });
    return promise.future();
}
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) DatagramPacket(io.netty.channel.socket.DatagramPacket) Future(io.vertx.core.Future) ChannelFuture(io.netty.channel.ChannelFuture)

Example 4 with PromiseInternal

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

the class NetClientImpl method close.

@Override
public void close(Promise<Void> completion) {
    ChannelGroupFuture fut = channelGroup.close();
    if (metrics != null) {
        PromiseInternal<Void> p = (PromiseInternal) Promise.promise();
        fut.addListener(p);
        p.future().<Void>compose(v -> {
            metrics.close();
            return Future.succeededFuture();
        }).onComplete(completion);
    } else {
        fut.addListener((PromiseInternal) completion);
    }
}
Also used : ChannelOption(io.netty.channel.ChannelOption) LoggingHandler(io.netty.handler.logging.LoggingHandler) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ContextInternal(io.vertx.core.impl.ContextInternal) TCPMetrics(io.vertx.core.spi.metrics.TCPMetrics) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) ConnectException(java.net.ConnectException) AsyncResult(io.vertx.core.AsyncResult) NetClient(io.vertx.core.net.NetClient) Metrics(io.vertx.core.spi.metrics.Metrics) SocketAddress(io.vertx.core.net.SocketAddress) Logger(io.vertx.core.impl.logging.Logger) Closeable(io.vertx.core.Closeable) ProxyOptions(io.vertx.core.net.ProxyOptions) ChannelGroup(io.netty.channel.group.ChannelGroup) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) VertxInternal(io.vertx.core.impl.VertxInternal) Predicate(java.util.function.Predicate) Promise(io.vertx.core.Promise) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) MetricsProvider(io.vertx.core.spi.metrics.MetricsProvider) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoop(io.netty.channel.EventLoop) PartialPooledByteBufAllocator(io.vertx.core.buffer.impl.PartialPooledByteBufAllocator) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) NetClientOptions(io.vertx.core.net.NetClientOptions) Channel(io.netty.channel.Channel) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) CloseFuture(io.vertx.core.impl.CloseFuture) ChannelGroupFuture(io.netty.channel.group.ChannelGroupFuture) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) ChannelGroupFuture(io.netty.channel.group.ChannelGroupFuture)

Example 5 with PromiseInternal

use of io.vertx.core.impl.future.PromiseInternal in project hono by eclipse.

the class CachingKafkaProducerFactoryTest method setUp.

@BeforeEach
void setUp() {
    final VertxInternal vertxMock = mock(VertxInternal.class);
    final ContextInternal context = VertxMockSupport.mockContextInternal(vertxMock);
    final PromiseInternal<Void> promiseInternal = VertxMockSupport.promiseInternal();
    when(promiseInternal.future()).thenReturn(Future.succeededFuture());
    doAnswer(invocation -> {
        return promiseInternal;
    }).when(context).promise();
    when(vertxMock.getOrCreateContext()).thenReturn(context);
    doAnswer(invocation -> {
        final Promise<Object> result = Promise.promise();
        final Handler<Future<Object>> blockingCode = invocation.getArgument(0);
        final Handler<AsyncResult<Object>> resultHandler = invocation.getArgument(1);
        result.future().onComplete(resultHandler);
        blockingCode.handle(result.future());
        return null;
    }).when(context).executeBlocking(VertxMockSupport.anyHandler(), VertxMockSupport.anyHandler());
    final BiFunction<String, Map<String, String>, KafkaProducer<String, Buffer>> instanceSupplier = (n, c) -> {
        final MockProducer<String, Buffer> mockProducer = new MockProducer<>(true, new StringSerializer(), new BufferSerializer());
        return KafkaProducer.create(vertxMock, mockProducer);
    };
    factory = CachingKafkaProducerFactory.testFactory(vertxMock, instanceSupplier);
    configProperties.setProducerConfig(Map.of("bootstrap.servers", "localhost:9092"));
}
Also used : KafkaProducer(io.vertx.kafka.client.producer.KafkaProducer) BeforeEach(org.junit.jupiter.api.BeforeEach) BufferSerializer(io.vertx.kafka.client.serialization.BufferSerializer) BiFunction(java.util.function.BiFunction) KafkaException(org.apache.kafka.common.KafkaException) ContextInternal(io.vertx.core.impl.ContextInternal) KafkaProducer(io.vertx.kafka.client.producer.KafkaProducer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) AsyncResult(io.vertx.core.AsyncResult) VertxInternal(io.vertx.core.impl.VertxInternal) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) AuthorizationException(org.apache.kafka.common.errors.AuthorizationException) Promise(io.vertx.core.Promise) OutOfOrderSequenceException(org.apache.kafka.common.errors.OutOfOrderSequenceException) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) UnsupportedForMessageFormatException(org.apache.kafka.common.errors.UnsupportedForMessageFormatException) Optional(java.util.Optional) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) Handler(io.vertx.core.Handler) MockProducer(org.apache.kafka.clients.producer.MockProducer) Mockito.mock(org.mockito.Mockito.mock) BufferSerializer(io.vertx.kafka.client.serialization.BufferSerializer) VertxInternal(io.vertx.core.impl.VertxInternal) MockProducer(org.apache.kafka.clients.producer.MockProducer) ContextInternal(io.vertx.core.impl.ContextInternal) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult) Map(java.util.Map) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)5 Future (io.vertx.core.Future)5 Handler (io.vertx.core.Handler)5 VertxInternal (io.vertx.core.impl.VertxInternal)5 PromiseInternal (io.vertx.core.impl.future.PromiseInternal)5 Buffer (io.vertx.core.buffer.Buffer)4 Objects (java.util.Objects)4 ChannelFuture (io.netty.channel.ChannelFuture)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 ChannelOption (io.netty.channel.ChannelOption)3 LoggingHandler (io.netty.handler.logging.LoggingHandler)3 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)3 Nullable (io.vertx.codegen.annotations.Nullable)3 Promise (io.vertx.core.Promise)3 ContextInternal (io.vertx.core.impl.ContextInternal)3 ByteBuf (io.netty.buffer.ByteBuf)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