List of usage examples for io.netty.channel ChannelFuture addListener
@Override ChannelFuture addListener(GenericFutureListener<? extends Future<? super Void>> listener);
From source file:net.tomp2p.connection.TestReservation.java
License:Apache License
/** * Test an unclean shutdown, that means the reservation is shutdown, but the * connectioncreation is not.// ww w . j ava2 s . c om * * @throws InterruptedException . */ @Test public void testReservationTCPNonCleanShutdown() throws InterruptedException { long start = System.currentTimeMillis(); final int round = 100; final int inner = 100; final int conn = 5; final int tcpMax = 500; for (int i = 0; i < round; i++) { ChannelClientConfiguration c = PeerBuilder.createDefaultChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); c.maxPermitsTCP(tcpMax); Reservation r = new Reservation(workerGroup, c); List<FutureChannelCreator> fcc = new ArrayList<FutureChannelCreator>(); for (int j = 0; j < inner; j++) { FutureChannelCreator fc = r.create(0, conn); fc.addListener(new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isFailed()) { return; } final ChannelCreator cc = future.channelCreator(); final int timeout = 2000; for (int k = 0; k < conn; k++) { ChannelFuture channelFuture = cc.createTCP(SOCKET_ADDRESS, timeout, new HashMap<String, Pair<EventExecutorGroup, ChannelHandler>>() { }, new FutureResponse(null)); if (channelFuture == null) { return; } channelFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { future.channel().close(); } }); } } }); fcc.add(fc); } for (FutureChannelCreator fcc1 : fcc) { fcc1.awaitListeners(); } r.shutdown().awaitListenersUninterruptibly(); } long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("BENCHMARK: opened and closed " + round + " x " + inner + " x " + conn + " TCP connections to localhost in " + time + " ms. STAT: TCP unclean open/close per sec:" + ((round * inner * conn * mil) / time)); }
From source file:net.tomp2p.connection.TestReservation.java
License:Apache License
/** * Unclean shutdown of pending connections. * /*w w w . j a v a2 s . c o m*/ * @throws InterruptedException . */ @Test public void testReservationTCPNonCleanShutdown2() throws InterruptedException { EventLoopGroup ev = new NioEventLoopGroup(); final int round = 100; final int inner = 100; final int conn = 5; final int tcpMax = 500; for (int i = 0; i < round; i++) { ChannelClientConfiguration c = PeerBuilder.createDefaultChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); c.maxPermitsTCP(tcpMax); Reservation r = new Reservation(ev, c); List<FutureChannelCreator> fcc = new ArrayList<FutureChannelCreator>(); for (int j = 0; j < inner; j++) { FutureChannelCreator fc = r.create(0, conn); fc.addListener(new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isFailed()) { return; } final ChannelCreator cc = future.channelCreator(); final int timeout = 2000; for (int k = 0; k < conn; k++) { ChannelFuture channelFuture = cc.createTCP(SOCKET_ADDRESS, timeout, new HashMap<String, Pair<EventExecutorGroup, ChannelHandler>>() { }, new FutureResponse(null)); if (channelFuture == null) { return; } channelFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { future.channel().close(); } }); } } }); fcc.add(fc); } r.shutdown().awaitListenersUninterruptibly(); } Future<?> f = ev.shutdownGracefully().awaitUninterruptibly(); f.awaitUninterruptibly(); }
From source file:net.tomp2p.connection2.RequestHandler.java
License:Apache License
/** * Report a successful response after the channel was closed. * // w w w . j av a 2 s.c o m * @param close * The close future * @param responseMessage * The response message */ private void reportMessage(final ChannelFuture close, final Message2 responseMessage) { close.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture arg0) throws Exception { LOG.debug("report success {}", responseMessage); //trigger the notify when we closed the channel. futureResponse.setResponseNow(); } }); }
From source file:net.tomp2p.connection2.RequestHandler.java
License:Apache License
/** * Report a failure after the channel was closed. * /*from w w w.ja v a 2 s . c o m*/ * @param close * The close future */ private void reportFailed(final ChannelFuture close) { close.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture arg0) throws Exception { futureResponse.setResponseNow(); } }); }
From source file:net.tomp2p.connection2.Sender.java
License:Apache License
/** * After connecting, we check if the connect was successful. * //w w w . j a va 2 s . c om * @param futureResponse * The future to set the response * @param message * The message to send * @param channelFuture * the future of the connect * @param fireAndForget * True, if we don't expect a message */ private void afterConnect(final FutureResponse futureResponse, final Message2 message, final ChannelFuture channelFuture, final boolean fireAndForget) { final Cancel connectCancel = createCancel(channelFuture); futureResponse.addCancel(connectCancel); channelFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { futureResponse.removeCancel(connectCancel); if (future.isSuccess()) { futureResponse.setProgressHandler(new ProgresHandler() { @Override public void progres() { final ChannelFuture writeFuture = future.channel().writeAndFlush(message); afterSend(writeFuture, futureResponse, fireAndForget); } }); // this needs to be called first before all other progress futureResponse.progressFirst(); } else { futureResponse.setFailed("Channel creation failed " + future.cause()); LOG.warn("Channel creation failed ", future.cause()); } } }); }
From source file:net.tomp2p.connection2.Sender.java
License:Apache License
/** * After sending, we check if the write was successful or if it was a fire and forget. * /*w w w .ja v a2s. c o m*/ * @param writeFuture * The future of the write operation. Can be UDP or TCP * @param futureResponse * The future to set the response * @param fireAndForget * True, if we don't expect a message */ private void afterSend(final ChannelFuture writeFuture, final FutureResponse futureResponse, final boolean fireAndForget) { final Cancel writeCancel = createCancel(writeFuture); writeFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { futureResponse.removeCancel(writeCancel); if (!future.isSuccess()) { futureResponse.setFailedLater(future.cause()); reportFailed(futureResponse, future.channel().close()); LOG.warn("Failed to write channel the request {}", futureResponse.getRequest(), future.cause()); } if (fireAndForget) { futureResponse.setResponseLater(null); LOG.debug("fire and forget, close channel now"); reportMessage(futureResponse, future.channel().close()); } } }); }
From source file:net.tomp2p.connection2.Sender.java
License:Apache License
/** * Report a failure after the channel was closed. * /*from w w w .ja v a 2 s .com*/ * @param futureResponse * The future to set the response * @param close * The close future * @param cause * The response message */ private void reportFailed(final FutureResponse futureResponse, final ChannelFuture close) { close.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture arg0) throws Exception { futureResponse.setResponseNow(); } }); }
From source file:net.tomp2p.connection2.Sender.java
License:Apache License
/** * Report a successful response after the channel was closed. * /*w ww. ja v a 2s.c o m*/ * @param futureResponse * The future to set the response * @param close * The close future * @param responseMessage * The response message */ private void reportMessage(final FutureResponse futureResponse, final ChannelFuture close) { close.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture arg0) throws Exception { futureResponse.setResponseNow(); } }); }
From source file:net.tomp2p.connection2.TestChannelCreator.java
License:Apache License
/** * This test only works if the server calls "nc -lk 4000", that is done in @Before. It will open and close 1000 * * 1000 connections to localhost./*from w w w . j a v a2 s .c o m*/ * * @throws InterruptedException . */ @Test @Ignore public void testCreatorTCP() throws InterruptedException { long start = System.currentTimeMillis(); final int rounds = 10000; final int connections = 20; final int printOut = 100; EventLoopGroup ev = new NioEventLoopGroup(); final int timeout = 4000; for (int j = 0; j < rounds; j++) { ChannelClientConfiguration c = new ChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); final ChannelCreator channelCreator2 = new ChannelCreator(ev, new FutureDone<Void>(), 0, connections, c); if (j % printOut == 0) { System.out.print(j + " "); } final CountDownLatch countDownLatch = new CountDownLatch(connections); final Map<String, ChannelHandler> tmp = new HashMap<String, ChannelHandler>(); final GenericFutureListener<ChannelFuture> handler = new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().close(); countDownLatch.countDown(); } else { future.cause().printStackTrace(); } } }; for (int i = 0; i < connections; i++) { final ChannelFuture channelFuture = channelCreator2.createTCP(SOCKET_ADDRESS, timeout, tmp); channelFuture.addListener(handler); } countDownLatch.await(); channelCreator2.shutdown().awaitUninterruptibly(); } ev.shutdownGracefully().awaitUninterruptibly(); long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("\nBENCHMARK: opened and closed " + connections + " x " + rounds + " TCP connections to localhost in " + time + " ms. STAT: TCP open/close per sec:" + ((connections * rounds * mil) / time)); }
From source file:net.tomp2p.connection2.TestChannelCreator.java
License:Apache License
/** * This test only works if the server calls "nc -lku 4000", that is done in @Before. * /*from w w w .j a v a 2 s .c om*/ * @throws InterruptedException . */ @Test @Ignore public void testCreatorUDP() throws InterruptedException { long start = System.currentTimeMillis(); final int rounds = 10000; final int connections = 20; final int printOut = 100; EventLoopGroup ev = new NioEventLoopGroup(); for (int j = 0; j < rounds; j++) { ChannelClientConfiguration c = new ChannelClientConfiguration(); c.pipelineFilter(new MyPipeLine()); final ChannelCreator channelCreator2 = new ChannelCreator(ev, new FutureDone<Void>(), connections, 0, c); if (j % printOut == 0) { System.out.print(j + " "); } final CountDownLatch countDownLatch = new CountDownLatch(connections); final Map<String, ChannelHandler> tmp = new HashMap<String, ChannelHandler>(); GenericFutureListener<ChannelFuture> handler = new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().writeAndFlush(Unpooled.wrappedBuffer(new byte[1])); future.channel().close(); countDownLatch.countDown(); } else { future.cause().printStackTrace(); } } }; for (int i = 0; i < connections; i++) { final ChannelFuture channelFuture = channelCreator2.createUDP(SOCKET_ADDRESS, false, tmp); channelFuture.addListener(handler); } countDownLatch.await(); channelCreator2.shutdown().awaitUninterruptibly(); } ev.shutdownGracefully().awaitUninterruptibly(); long time = System.currentTimeMillis() - start; long mil = TimeUnit.SECONDS.toMillis(1); System.err.println("\nBENCHMARK: opened and closed " + connections + " x " + rounds + " UDP connections to localhost in " + time + " ms. STAT: UDP open/close per sec:" + ((connections * rounds * mil) / time)); }