Search in sources :

Example 21 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project LanternServer by LanternPowered.

the class NetworkSession method send.

/**
 * Sends a iterable of {@link Message}s.
 *
 * @param messages The messages
 */
public void send(Iterable<Message> messages) {
    checkNotNull(messages, "messages");
    final Iterator<Message> it = messages.iterator();
    checkArgument(it.hasNext(), "messages cannot be empty");
    Message message = it.next();
    // Don't bother checking if we are in the event loop,
    // there is only one message.
    final ChannelPromise voidPromise = this.channel.voidPromise();
    if (!it.hasNext()) {
        this.channel.writeAndFlush(message, voidPromise);
    } else {
        final EventLoop eventLoop = this.channel.eventLoop();
        if (eventLoop.inEventLoop()) {
            for (Message message0 : messages) {
                this.channel.writeAndFlush(message0, voidPromise);
            }
        } else {
            // If there are more then one message, combine them inside the
            // event loop to reduce overhead of wakeup calls and object creation
            // Create a copy of the list, to avoid concurrent modifications
            final List<Message> messages0 = ImmutableList.copyOf(messages);
            eventLoop.submit(() -> {
                for (Message message0 : messages0) {
                    this.channel.writeAndFlush(message0, voidPromise);
                }
            });
        }
    }
}
Also used : EventLoop(io.netty.channel.EventLoop) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) ChannelPromise(io.netty.channel.ChannelPromise)

Example 22 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project LanternServer by LanternPowered.

the class NetworkSession method sendWithFuture.

/**
 * Sends a iterable of {@link Message}s.
 *
 * @param messages The messages
 */
public ChannelFuture sendWithFuture(Iterable<Message> messages) {
    checkNotNull(messages, "messages");
    final Iterator<Message> it = messages.iterator();
    checkArgument(it.hasNext(), "messages cannot be empty");
    final ChannelPromise promise = this.channel.newPromise();
    if (!this.channel.isActive()) {
        return promise;
    }
    promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
    Message message = it.next();
    // there is only one message.
    if (!it.hasNext()) {
        this.channel.writeAndFlush(message, promise);
    } else {
        final EventLoop eventLoop = this.channel.eventLoop();
        final ChannelPromise voidPromise = this.channel.voidPromise();
        if (eventLoop.inEventLoop()) {
            while (true) {
                final boolean next = it.hasNext();
                // Only use a normal channel promise for the last message
                this.channel.writeAndFlush(message, next ? voidPromise : promise);
                if (!next) {
                    break;
                }
                message = it.next();
                ReferenceCountUtil.retain(message);
            }
        } else {
            // If there are more then one message, combine them inside the
            // event loop to reduce overhead of wakeup calls and object creation
            // Create a copy of the list, to avoid concurrent modifications
            final List<Message> messages0 = ImmutableList.copyOf(messages);
            messages0.forEach(ReferenceCountUtil::retain);
            eventLoop.submit(() -> {
                final Iterator<Message> it0 = messages0.iterator();
                do {
                    final Message message0 = it0.next();
                    // Only use a normal channel promise for the last message
                    this.channel.writeAndFlush(message0, it0.hasNext() ? voidPromise : promise);
                } while (it0.hasNext());
            });
        }
    }
    return promise;
}
Also used : EventLoop(io.netty.channel.EventLoop) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) ChannelPromise(io.netty.channel.ChannelPromise) ReferenceCountUtil(io.netty.util.ReferenceCountUtil)

Example 23 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project LanternServer by LanternPowered.

the class NetworkSession method send.

/**
 * Sends a array of {@link Message}s.
 *
 * @param messages The messages
 */
public void send(Message... messages) {
    checkNotNull(messages, "messages");
    checkArgument(messages.length != 0, "messages cannot be empty");
    if (!this.channel.isActive()) {
        return;
    }
    final ChannelPromise voidPromise = this.channel.voidPromise();
    if (messages.length == 1) {
        this.channel.writeAndFlush(messages[0], voidPromise);
    } else {
        final EventLoop eventLoop = this.channel.eventLoop();
        if (eventLoop.inEventLoop()) {
            for (Message message : messages) {
                ReferenceCountUtil.retain(message);
                this.channel.writeAndFlush(message, voidPromise);
            }
        } else {
            // If there are more then one message, combine them inside the
            // event loop to reduce overhead of wakeup calls and object creation
            // Create a copy of the list, to avoid concurrent modifications
            final List<Message> messages0 = ImmutableList.copyOf(messages);
            messages0.forEach(ReferenceCountUtil::retain);
            eventLoop.submit(() -> {
                for (Message message0 : messages0) {
                    this.channel.writeAndFlush(message0, voidPromise);
                }
            });
        }
    }
}
Also used : EventLoop(io.netty.channel.EventLoop) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) ChannelPromise(io.netty.channel.ChannelPromise) ReferenceCountUtil(io.netty.util.ReferenceCountUtil)

Example 24 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project drill by axbaretto.

the class WebSessionResourcesTest method testChannelPromiseWithValidExecutor.

/**
 * Validates successful {@link WebSessionResources#close()} with valid CloseFuture and other parameters.
 * @throws Exception
 */
@Test
public void testChannelPromiseWithValidExecutor() throws Exception {
    try {
        EventExecutor mockExecutor = mock(EventExecutor.class);
        ChannelPromise closeFuture = new DefaultChannelPromise(null, mockExecutor);
        webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class), mock(UserSession.class), closeFuture);
        webSessionResources.close();
        verify(webSessionResources.getAllocator()).close();
        verify(webSessionResources.getSession()).close();
        verify(mockExecutor).inEventLoop();
        verify(mockExecutor).execute(any(Runnable.class));
        assertTrue(webSessionResources.getCloseFuture() == null);
        assertTrue(!listenerComplete);
    } catch (Exception e) {
        fail();
    }
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.Test)

Example 25 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project drill by axbaretto.

the class WebSessionResourcesTest method testCloseWithListener.

/**
 * Validates successful {@link WebSessionResources#close()} with valid CloseFuture and {@link TestClosedListener}
 * getting invoked which is added to the close future.
 * @throws Exception
 */
@Test
public void testCloseWithListener() throws Exception {
    try {
        // Assign latch, executor and closeListener for this test case
        GenericFutureListener<Future<Void>> closeListener = new TestClosedListener();
        latch = new CountDownLatch(1);
        executor = TransportCheck.createEventLoopGroup(1, "Test-Thread").next();
        ChannelPromise closeFuture = new DefaultChannelPromise(null, executor);
        // create WebSessionResources with above ChannelPromise to notify listener
        webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class), mock(UserSession.class), closeFuture);
        // Add the Test Listener to close future
        assertTrue(!listenerComplete);
        closeFuture.addListener(closeListener);
        // Close the WebSessionResources
        webSessionResources.close();
        // Verify the states
        verify(webSessionResources.getAllocator()).close();
        verify(webSessionResources.getSession()).close();
        assertTrue(webSessionResources.getCloseFuture() == null);
        // Since listener will be invoked so test should not wait forever
        latch.await();
        assertTrue(listenerComplete);
    } catch (Exception e) {
        fail();
    } finally {
        listenerComplete = false;
        executor.shutdownGracefully();
    }
}
Also used : DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Future(io.netty.util.concurrent.Future) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)223 Test (org.junit.jupiter.api.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)63 ChannelFuture (io.netty.channel.ChannelFuture)62 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)58 ByteBuf (io.netty.buffer.ByteBuf)56 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)30 Test (org.junit.Test)25 Channel (io.netty.channel.Channel)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 ClosedChannelException (java.nio.channels.ClosedChannelException)20 ChannelFutureListener (io.netty.channel.ChannelFutureListener)19 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)18 InvocationOnMock (org.mockito.invocation.InvocationOnMock)18 AsciiString (io.netty.util.AsciiString)15 IOException (java.io.IOException)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 Bootstrap (io.netty.bootstrap.Bootstrap)12