use of io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse-vertx.
the class HttpServerWorker method handle.
@Override
public void handle(Channel ch) {
if (HAProxyMessageCompletionHandler.canUseProxyProtocol(options.isUseProxyProtocol())) {
IdleStateHandler idle;
io.netty.util.concurrent.Promise<Channel> p = ch.eventLoop().newPromise();
ch.pipeline().addLast(new HAProxyMessageDecoder());
if (options.getProxyProtocolTimeout() > 0) {
ch.pipeline().addLast("idle", idle = new IdleStateHandler(0, 0, options.getProxyProtocolTimeout(), options.getProxyProtocolTimeoutUnit()));
} else {
idle = null;
}
ch.pipeline().addLast(new HAProxyMessageCompletionHandler(p));
p.addListener((GenericFutureListener<Future<Channel>>) future -> {
if (future.isSuccess()) {
if (idle != null) {
ch.pipeline().remove(idle);
}
configurePipeline(future.getNow());
} else {
handleException(future.cause());
}
});
} else {
configurePipeline(ch);
}
}
use of io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse-vertx.
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();
}
use of io.netty.util.concurrent.GenericFutureListener in project netconf by opendaylight.
the class DefaultCloseSessionTest method testDefaultCloseSession.
@Test
public void testDefaultCloseSession() throws Exception {
AutoCloseable res = mock(AutoCloseable.class);
doNothing().when(res).close();
DefaultCloseSession close = new DefaultCloseSession("", res);
final Document doc = XmlUtil.newDocument();
final XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
final Channel channel = mock(Channel.class);
doReturn("channel").when(channel).toString();
mockEventLoop(channel);
final ChannelFuture channelFuture = mock(ChannelFuture.class);
doReturn(channelFuture).when(channel).close();
doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
final ChannelPromise sendFuture = mock(ChannelPromise.class);
doAnswer(invocation -> {
invocation.<GenericFutureListener>getArgument(0).operationComplete(sendFuture);
return null;
}).when(sendFuture).addListener(any(GenericFutureListener.class));
doReturn(sendFuture).when(channel).newPromise();
doReturn(sendFuture).when(channel).writeAndFlush(any(), any());
doReturn(true).when(sendFuture).isSuccess();
final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
doNothing().when(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
final NetconfServerSession session = new NetconfServerSession(listener, channel, 1L, NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
close.setNetconfSession(session);
close.handleWithNoSubsequentOperations(doc, elem);
// Fake close response to trigger delayed close
session.sendMessage(new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" + "<ok/>\n" + "</rpc-reply>")));
verify(channel).close();
verify(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
}
use of io.netty.util.concurrent.GenericFutureListener in project netconf by opendaylight.
the class NetconfDeviceCommunicatorTest method testSendRequest.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testSendRequest() throws Exception {
setupSession();
NetconfMessage message = new NetconfMessage(UntrustedXML.newDocumentBuilder().newDocument());
QName rpc = QName.create("", "mockRpc");
ArgumentCaptor<GenericFutureListener> futureListener = ArgumentCaptor.forClass(GenericFutureListener.class);
ChannelFuture mockChannelFuture = mock(ChannelFuture.class);
doReturn(mockChannelFuture).when(mockChannelFuture).addListener(futureListener.capture());
doReturn(mockChannelFuture).when(mockSession).sendMessage(same(message));
ListenableFuture<RpcResult<NetconfMessage>> resultFuture = communicator.sendRequest(message, rpc);
verify(mockSession).sendMessage(same(message));
assertNotNull("ListenableFuture is null", resultFuture);
verify(mockChannelFuture).addListener(futureListener.capture());
Future<Void> operationFuture = mock(Future.class);
doReturn(true).when(operationFuture).isSuccess();
futureListener.getValue().operationComplete(operationFuture);
try {
// verify it's not cancelled or has an error set
resultFuture.get(1, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
LOG.info("Operation failed due timeout.");
}
// expected
}
use of io.netty.util.concurrent.GenericFutureListener in project cdap by cdapio.
the class AbstractSocksServerConnectHandler method handleConnectRequest.
/**
* Handles the CONNECT socks request.
*
* @param ctx the context object for the channel
* @param destAddress the destination address
* @param destPort the destination port that the remote {@code localhost} is listening to
* @param responseFunc a {@link Function} for creating a {@link SocksMessage} to send back to client
* once the relay channel has been established
*/
private void handleConnectRequest(ChannelHandlerContext ctx, String destAddress, int destPort, Function<InetSocketAddress, SocksMessage> responseFunc, Supplier<SocksMessage> failureResponseSupplier) {
// Create a forwarding channel handler, which returns a Future.
// When that handler future completed successfully, responds with a Socks success response.
// After the success response completed successfully, add the relay handler to the pipeline.
Channel inboundChannel = ctx.channel();
createForwardingChannelHandler(inboundChannel, destAddress, destPort).addListener((GenericFutureListener<Future<RelayChannelHandler>>) handlerFuture -> {
if (handlerFuture.isSuccess()) {
RelayChannelHandler handler = handlerFuture.get();
SocketAddress relayAddress = handler.getRelayAddress();
if (!(relayAddress instanceof InetSocketAddress)) {
LOG.warn("Relay address is not InetSocketAddress: {} {}", relayAddress.getClass(), relayAddress);
inboundChannel.writeAndFlush(failureResponseSupplier.get()).addListener(channelFuture -> Channels.closeOnFlush(inboundChannel));
} else {
inboundChannel.writeAndFlush(responseFunc.apply((InetSocketAddress) relayAddress)).addListener(channelFuture -> {
if (channelFuture.isSuccess()) {
ctx.pipeline().remove(AbstractSocksServerConnectHandler.this);
ctx.pipeline().addLast(handler);
} else {
Channels.closeOnFlush(inboundChannel);
}
});
}
} else {
Channels.closeOnFlush(inboundChannel);
}
});
}
Aggregations