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);
}
});
}
}
}
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;
}
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);
}
});
}
}
}
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();
}
}
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();
}
}
Aggregations