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 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);
}
});
}
use of io.netty.util.concurrent.GenericFutureListener in project cdap by cdapio.
the class ServiceSocksServerConnectHandler method createForwardingChannelHandler.
@Override
protected Future<RelayChannelHandler> createForwardingChannelHandler(Channel inboundChannel, String destAddress, int destPort) {
Promise<RelayChannelHandler> promise = new DefaultPromise<>(inboundChannel.eventLoop());
// Creates a bootstrap for connecting to the target service
ChannelGroup channels = new DefaultChannelGroup(inboundChannel.eventLoop());
Bootstrap bootstrap = new Bootstrap().group(inboundChannel.eventLoop()).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) {
channels.add(ctx.channel());
// When the outbound connection is active, adds the relay channel handler for the current pipeline,
// which is for relaying traffic coming back from outbound connection.
// Also complete the relay channel handler future, which is for relaying traffic from inbound to outbound.
ctx.pipeline().addLast(new SimpleRelayChannelHandler(inboundChannel));
promise.setSuccess(new SimpleRelayChannelHandler(ctx.channel()));
}
});
// Discover the target address
Promise<Discoverable> discoverablePromise = new DefaultPromise<>(inboundChannel.eventLoop());
Cancellable cancellable = discoveryServiceClient.discover(destAddress).watchChanges(serviceDiscovered -> {
// If it is discovered, make a connection and complete the channel handler future
Discoverable discoverable = new RandomEndpointStrategy(() -> serviceDiscovered).pick();
if (discoverable != null) {
discoverablePromise.setSuccess(discoverable);
}
}, inboundChannel.eventLoop());
// When discovery completed successfully, connect to the destination
discoverablePromise.addListener((GenericFutureListener<Future<Discoverable>>) discoverableFuture -> {
cancellable.cancel();
if (discoverableFuture.isSuccess()) {
Discoverable discoverable = discoverableFuture.get();
bootstrap.connect(discoverable.getSocketAddress()).addListener((ChannelFutureListener) channelFuture -> {
if (!channelFuture.isSuccess()) {
promise.setFailure(channelFuture.cause());
}
});
} else {
promise.setFailure(discoverableFuture.cause());
}
});
// On inbound channel close, close all outbound channels.
// Also cancel the watch since it is no longer needed.
// This is to handle case where discovery never return an endpoint before client connection timeout
inboundChannel.closeFuture().addListener((ChannelFutureListener) future -> {
cancellable.cancel();
channels.close();
});
return promise;
}
Aggregations